diff --git a/.gitignore b/.gitignore index e69de29..b14852c 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1 @@ +api/api diff --git a/api/main.go b/api/main.go index 545f738..726c374 100644 --- a/api/main.go +++ b/api/main.go @@ -1,7 +1,62 @@ package main -import "fmt" +import ( + "bytes" + "fmt" + "time" + "encoding/json" + "net/http" + "io/ioutil" + "os/exec" + "strings" +) + +type script struct { + Language string `json:"language"` + Source string `json:"source"` +} + +type result struct { + Ran bool `json:"ran"` + Output string `json:"output"` +} func main() { - fmt.Println("placeholder") + 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 + script := script{} + message := json.NewDecoder(req.Body) + message.Decode(&script) + + // write the code to temp dir + filename := fmt.Sprintf("/tmp/%d.code", time.Now().UnixNano()) + + ioutil.WriteFile(filename, []byte(script.Source), 0644) + + // set up the execution + cmd := exec.Command("../docker/execute", script.Language, filename) + + // capture out/err + var stdout, stderr bytes.Buffer + cmd.Stdout = &stdout + cmd.Stderr = &stderr + + err := cmd.Run() + + // prepare response + data := result{ + Ran: err == nil, + Output: strings.TrimSpace(stdout.String()), + } + + response, _ := json.Marshal(data) + + res.Write(response) } diff --git a/api/start b/api/start new file mode 100755 index 0000000..10f5da6 --- /dev/null +++ b/api/start @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +go run main.go diff --git a/docker/Dockerfile b/docker/Dockerfile index 462408d..cf8f30a 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,4 +1,11 @@ FROM ubuntu:18.04 RUN apt-get update +RUN apt-get -y install bash-completion +RUN apt-get -y install build-essential +RUN apt-get -y install python RUN apt-get -y install python3 +RUN apt-get -y install ruby +RUN apt-get -y install nodejs + +CMD sleep infinity diff --git a/docker/build_image b/docker/build similarity index 54% rename from docker/build_image rename to docker/build index 36893be..1bbbedc 100755 --- a/docker/build_image +++ b/docker/build @@ -1 +1,3 @@ +#!/usr/bin/env bash + docker build -t piston . diff --git a/docker/execute b/docker/execute index e583cdf..05b155c 100755 --- a/docker/execute +++ b/docker/execute @@ -1,10 +1,46 @@ #!/usr/bin/env bash -abs=$1 +#docker run --rm -it piston /usr/bin/python -V +#docker run --rm -it piston /usr/bin/python3 -V +#docker run --rm -it piston /usr/bin/ruby --version +#docker run --rm -it piston /usr/bin/nodejs --version + +if [ -z "$1" ]; then + echo "invalid args" + exit +fi +if [ -z "$2" ]; then + echo "invalid args" + exit +fi + +lang=$1 +abs=$2 file=$(basename $abs) +bin= +case "$lang" in +"python2") + bin=python2 + ;; +"python" | "python3") + bin=python3 + ;; +"ruby") + bin=ruby + ;; +"javascript" | "js" | "node") + bin=nodejs + ;; +*) + echo "invalid language" + exit +esac + docker run \ + -m 16m \ + --cpus=".5" \ --rm \ -v $abs:/$file \ - -it piston \ - python3 /$file + piston \ + timeout -s HUP 3 $bin /$file 2>&1