fixed switch case, optimized lxc/execute, optimized json response

This commit is contained in:
pizalord22 2020-03-31 14:37:23 +02:00
parent 9e7e53d45d
commit d42aa6edb1
1 changed files with 23 additions and 41 deletions

View File

@ -53,7 +53,7 @@ func main() {
fmt.Println("starting api on port", port) fmt.Println("starting api on port", port)
http.HandleFunc("/execute", Execute) http.HandleFunc("/execute", Execute)
http.HandleFunc("/versions", Versions) http.HandleFunc("/versions", Versions)
http.ListenAndServe(":" + port, nil) http.ListenAndServe(":"+port, nil)
} }
func Execute(res http.ResponseWriter, req *http.Request) { func Execute(res http.ResponseWriter, req *http.Request) {
@ -87,7 +87,6 @@ func Execute(res http.ResponseWriter, req *http.Request) {
// now calls function and returns // now calls function and returns
for _, lang := range whitelist { for _, lang := range whitelist {
if lang == inbound.Language { if lang == inbound.Language {
launch(inbound, res) launch(inbound, res)
return return
} }
@ -121,15 +120,7 @@ func launch(request Inbound, res http.ResponseWriter) {
ioutil.WriteFile(srcfile, []byte(request.Source), 0644) ioutil.WriteFile(srcfile, []byte(request.Source), 0644)
// set up the arguments to send to the execute command // set up the arguments to send to the execute command
var args []string cmd := exec.Command("../lxc/execute", request.Language, srcfile, strings.Join(request.Args, "\n"))
args = append(args, request.Language)
args = append(args, srcfile)
args = append(args, strings.Join(request.Args, "\n"))
// set up the execution
cmd := exec.Command("../lxc/execute", args...)
// capture out/err // capture out/err
var stdout, stderr bytes.Buffer var stdout, stderr bytes.Buffer
@ -144,43 +135,34 @@ func launch(request Inbound, res http.ResponseWriter) {
switch execlang { switch execlang {
case "c++": case "c++":
execlang = "cpp" execlang = "cpp"
break case "cs", "c#":
case "cs":
case "c#":
execlang = "csharp" execlang = "csharp"
break
case "asm": case "asm":
execlang = "nasm" execlang = "nasm"
break case "js", "javascript":
case "javascript":
case "js":
execlang = "node" execlang = "node"
break
case "python": case "python":
execlang = "python3" execlang = "python3"
break
case "ts": case "ts":
execlang = "typescript" execlang = "typescript"
break
}
var version string
for _, lang := range languages {
if lang.Name == execlang {
version = lang.Version
break
}
} }
// prepare response // prepare response
outbound := Outbound{ outbound := Outbound{
Ran: err == nil, Ran: err == nil,
Language: request.Language, Language: request.Language,
Version: version, Version: "",
Output: strings.TrimSpace(stdout.String()), Output: strings.TrimSpace(stdout.String()),
} }
// retrieve the language version
for _, lang := range languages {
if lang.Name == execlang {
outbound.Version = lang.Version
break
}
}
response, _ := json.Marshal(outbound) response, _ := json.Marshal(outbound)
res.Write(response) res.Write(response)