fixed switch case, optimized lxc/execute, optimized json response
This commit is contained in:
parent
9e7e53d45d
commit
d42aa6edb1
42
api/main.go
42
api/main.go
|
@ -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)
|
||||||
|
|
Loading…
Reference in New Issue