piston/api/main.go

124 lines
2.6 KiB
Go
Raw Normal View History

2018-09-19 19:55:42 +02:00
package main
2018-09-20 07:49:02 +02:00
import (
"bytes"
"encoding/json"
2018-09-20 19:04:34 +02:00
"fmt"
2018-09-20 07:49:02 +02:00
"io/ioutil"
2018-09-20 19:04:34 +02:00
"net/http"
2018-09-20 07:49:02 +02:00
"os/exec"
"strings"
2018-09-20 19:04:34 +02:00
"time"
2018-09-20 07:49:02 +02:00
)
2018-09-20 17:21:46 +02:00
type inbound struct {
2018-10-06 18:48:10 +02:00
Language string `json:"language"`
Source string `json:"source"`
Args []string `json:"args"`
2018-09-20 07:49:02 +02:00
}
2018-09-20 17:21:46 +02:00
type problem struct {
Code string `json:"code"`
Message string `json:"message"`
}
type outbound struct {
2018-10-08 22:53:03 +02:00
Ran bool `json:"ran"`
Output string `json:"output"`
2018-09-20 07:49:02 +02:00
}
2018-09-19 19:55:42 +02:00
2018-10-08 00:54:29 +02:00
var instance int
2018-09-19 19:55:42 +02:00
func main() {
2018-10-08 00:54:29 +02:00
port := 2000
fmt.Println("starting api on port", port)
http.HandleFunc("/execute", Execute)
http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), nil)
2018-09-20 07:49:02 +02:00
}
func Execute(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "application/json")
// get json
2018-09-20 17:21:46 +02:00
inbound := inbound{}
2018-09-20 07:49:02 +02:00
message := json.NewDecoder(req.Body)
2018-09-20 17:21:46 +02:00
message.Decode(&inbound)
whitelist := []string{
2018-09-20 19:04:34 +02:00
"c",
2018-09-28 22:24:24 +02:00
"cpp", "c++",
"c#", "csharp", "cs",
2018-10-06 04:11:12 +02:00
"go",
"java",
2018-09-28 22:24:24 +02:00
"nasm", "asm",
2018-10-06 04:11:12 +02:00
"javascript", "js", "node",
2019-08-08 18:42:30 +02:00
"typescript", "ts",
2018-10-06 04:11:12 +02:00
"php",
"python", "python2", "python3",
"r",
"ruby",
2018-10-31 04:51:00 +01:00
"swift",
2018-12-08 18:06:01 +01:00
"brainfuck", "bf",
2019-05-31 23:00:32 +02:00
"rust",
2019-06-11 06:55:53 +02:00
"bash",
2018-09-20 17:21:46 +02:00
}
// check if the supplied language is supported
2018-09-20 19:04:34 +02:00
// now calls function and returns
2018-09-20 17:21:46 +02:00
for _, lang := range whitelist {
if lang == inbound.Language {
2018-09-20 19:04:34 +02:00
launch(inbound, res)
return
2018-09-20 17:21:46 +02:00
}
}
2018-09-20 19:04:34 +02:00
// now only called when the language is not supported
2018-09-28 22:24:24 +02:00
problem := problem{
Code: "unsupported_language",
Message: inbound.Language + " is not supported by Piston",
}
2018-09-20 17:21:46 +02:00
2018-09-28 22:24:24 +02:00
pres, _ := json.Marshal(problem)
2018-09-20 17:21:46 +02:00
2018-09-28 22:24:24 +02:00
res.Write(pres)
2018-09-20 19:04:34 +02:00
}
2018-09-20 07:49:02 +02:00
2018-09-28 22:24:24 +02:00
func launch(request inbound, res http.ResponseWriter) {
2018-10-06 18:48:10 +02:00
stamp := time.Now().UnixNano()
2018-09-21 05:17:46 +02:00
2018-09-20 07:49:02 +02:00
// write the code to temp dir
2018-10-06 18:48:10 +02:00
srcfile := fmt.Sprintf("/tmp/%d.code", stamp)
ioutil.WriteFile(srcfile, []byte(request.Source), 0644)
// set up the arguments to send to the execute command
var args []string
args = append(args, request.Language)
args = append(args, srcfile)
2018-09-20 07:49:02 +02:00
2018-10-06 18:48:10 +02:00
args = append(args, strings.Join(request.Args, "\n"))
2018-09-20 07:49:02 +02:00
// set up the execution
2018-10-23 00:00:12 +02:00
cmd := exec.Command("../lxc/execute", args...)
2018-09-20 07:49:02 +02:00
// capture out/err
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
// prepare response
2018-09-20 17:21:46 +02:00
outbound := outbound{
2018-09-20 07:49:02 +02:00
Ran: err == nil,
Output: strings.TrimSpace(stdout.String()),
}
2018-09-20 17:21:46 +02:00
response, _ := json.Marshal(outbound)
2018-09-20 07:49:02 +02:00
res.Write(response)
2018-09-19 19:55:42 +02:00
}