piston/api/main.go

108 lines
2.2 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-09-20 07:49:02 +02:00
Language string `json:"language"`
Source string `json:"source"`
}
2018-09-20 17:21:46 +02:00
type problem struct {
Code string `json:"code"`
Message string `json:"message"`
}
type outbound struct {
2018-09-20 07:49:02 +02:00
Ran bool `json:"ran"`
Output string `json:"output"`
}
2018-09-19 19:55:42 +02:00
func main() {
2018-09-20 07:49:02 +02:00
http.HandleFunc("/execute", Execute)
http.ListenAndServe("0.0.0.0:1337", nil)
}
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-28 22:24:24 +02:00
"python", "python2", "python3",
2018-09-20 17:21:46 +02:00
"ruby",
2018-09-28 22:24:24 +02:00
"javascript", "js", "node",
2018-09-20 19:04:34 +02:00
"c",
2018-09-28 22:24:24 +02:00
"cpp", "c++",
2018-09-21 04:43:25 +02:00
"go",
2018-09-22 08:27:44 +02:00
"r",
"php",
2018-09-28 22:24:24 +02:00
"c#", "csharp", "cs",
"nasm", "asm",
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-09-21 05:17:46 +02:00
var ext string = "code";
2018-09-28 22:24:24 +02:00
if request.Language == "go" {
2018-09-21 05:17:46 +02:00
ext = "go"
}
2018-09-20 07:49:02 +02:00
// write the code to temp dir
2018-09-21 05:17:46 +02:00
filename := fmt.Sprintf("/tmp/%d." + ext, time.Now().UnixNano())
2018-09-20 07:49:02 +02:00
2018-09-28 22:24:24 +02:00
ioutil.WriteFile(filename, []byte(request.Source), 0644)
2018-09-20 07:49:02 +02:00
// set up the execution
2018-09-28 22:24:24 +02:00
cmd := exec.Command("../docker/execute", request.Language, filename)
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
}