piston/api/main.go

118 lines
2.3 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) {
2018-09-20 19:04:34 +02:00
var found = false
2018-09-20 07:49:02 +02:00
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{
"python",
2018-09-20 19:04:34 +02:00
"python2",
2018-09-20 17:21:46 +02:00
"python3",
"ruby",
"javascript",
"js",
"node",
2018-09-20 19:04:34 +02:00
"c",
"cpp",
"c++",
2018-09-21 04:43:25 +02:00
"go",
2018-09-22 08:27:44 +02:00
"r",
"php",
"c#",
"csharp",
"cs",
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-20 17:21:46 +02:00
if !found {
problem := problem{
Code: "unsupported_language",
Message: inbound.Language + " is not supported by Piston",
}
pres, _ := json.Marshal(problem)
res.Write(pres)
return
}
2018-09-20 19:04:34 +02:00
}
2018-09-20 07:49:02 +02:00
2018-09-20 19:04:34 +02:00
func launch(inbound2 inbound, res http.ResponseWriter){
2018-09-21 05:17:46 +02:00
var ext string = "code";
if inbound2.Language == "go" {
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-20 19:04:34 +02:00
ioutil.WriteFile(filename, []byte(inbound2.Source), 0644)
2018-09-20 07:49:02 +02:00
// set up the execution
2018-09-20 19:04:34 +02:00
cmd := exec.Command("../docker/execute", inbound2.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
}