piston/api/main.go

145 lines
2.7 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 (
2018-10-10 12:47:20 +02:00
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"strings"
"strconv"
"time"
2018-09-20 07:49:02 +02:00
)
2018-09-20 17:21:46 +02:00
type inbound struct {
2018-10-10 12:47:20 +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 {
2018-10-10 12:47:20 +02:00
Code string `json:"code"`
Message string `json:"message"`
2018-09-20 17:21:46 +02:00
}
type outbound struct {
2018-10-10 12:47:20 +02:00
Generator string `json:"instance"`
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-10 12:47:20 +02:00
port := 2000
if len(os.Args) > 1 {
if i, err := strconv.Atoi(os.Args[1]); err == nil {
instance = i
port += i
}
} else {
instance = 0
}
fmt.Println("starting api on port", port)
http.HandleFunc("/execute", Execute)
err := http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), nil); if err != nil{
log.Fatal(err)
}
2018-09-20 07:49:02 +02:00
}
func Execute(res http.ResponseWriter, req *http.Request) {
2018-10-10 12:47:20 +02:00
res.Header().Set("Content-Type", "application/json")
// get json
inbound := inbound{}
message := json.NewDecoder(req.Body)
message.Decode(&inbound)
whitelist := []string{
"c",
"cpp", "c++",
"c#", "csharp", "cs",
"go",
"java",
"nasm", "asm",
"javascript", "js", "node",
"php",
"python", "python2", "python3",
"r",
"ruby",
}
// check if the supplied language is supported
// now calls function and returns
for _, lang := range whitelist {
if lang == inbound.Language {
launch(inbound, res)
return
}
}
// now only called when the language is not supported
problem := problem{
Code: "unsupported_language",
Message: inbound.Language + " is not supported by Piston",
}
pres, err := json.Marshal(problem); if err != nil{
log.Fatal(err)
}
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-10 12:47:20 +02:00
stamp := time.Now().UnixNano()
2018-09-21 05:17:46 +02:00
2018-10-10 12:47:20 +02:00
// write the code to temp dir
srcfile := fmt.Sprintf("/tmp/%d.code", stamp)
2018-10-06 18:48:10 +02:00
2018-10-10 12:47:20 +02:00
ioutil.WriteFile(srcfile, []byte(request.Source), 0644)
2018-10-06 18:48:10 +02:00
2018-10-10 12:47:20 +02:00
// set up the arguments to send to the execute command
var args []string
2018-10-06 18:48:10 +02:00
2018-10-10 12:47:20 +02:00
args = append(args, request.Language)
args = append(args, srcfile)
2018-09-20 07:49:02 +02:00
2018-10-10 12:47:20 +02:00
args = append(args, strings.Join(request.Args, "\n"))
2018-09-20 07:49:02 +02:00
2018-10-10 12:47:20 +02:00
// set up the execution
cmd := exec.Command("../docker/execute", args...)
cmd.Env = os.Environ()
2018-10-08 00:54:29 +02:00
2018-10-10 12:47:20 +02:00
if instance > 0 {
cmd.Env = append(cmd.Env, fmt.Sprintf("SOCKET=unix:///var/run/docker-%d.sock", instance))
}
2018-09-20 07:49:02 +02:00
2018-10-10 12:47:20 +02:00
// capture out/err
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
2018-09-20 07:49:02 +02:00
2018-10-10 12:47:20 +02:00
err := cmd.Run(); if err != nil{
log.Fatal(err)
}
2018-09-20 07:49:02 +02:00
2018-10-10 12:47:20 +02:00
// prepare response
outbound := outbound{
Generator: fmt.Sprintf("docker-%d", instance),
Ran: err == nil,
Output: strings.TrimSpace(stdout.String()),
}
2018-09-20 07:49:02 +02:00
2018-10-10 12:47:20 +02:00
response, err:= json.Marshal(outbound); if err != nil{
log.Fatal(err)
}
2018-09-20 07:49:02 +02:00
2018-10-10 12:47:20 +02:00
res.Write(response)
}