This commit is contained in:
Brian Seymour 2018-09-20 00:49:02 -05:00
parent 80aad50223
commit f719f62f62
6 changed files with 109 additions and 5 deletions

1
.gitignore vendored
View File

@ -0,0 +1 @@
api/api

View File

@ -1,7 +1,62 @@
package main 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() { 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)
} }

3
api/start Executable file
View File

@ -0,0 +1,3 @@
#!/usr/bin/env bash
go run main.go

View File

@ -1,4 +1,11 @@
FROM ubuntu:18.04 FROM ubuntu:18.04
RUN apt-get update 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 python3
RUN apt-get -y install ruby
RUN apt-get -y install nodejs
CMD sleep infinity

View File

@ -1 +1,3 @@
#!/usr/bin/env bash
docker build -t piston . docker build -t piston .

View File

@ -1,10 +1,46 @@
#!/usr/bin/env bash #!/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) 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 \ docker run \
-m 16m \
--cpus=".5" \
--rm \ --rm \
-v $abs:/$file \ -v $abs:/$file \
-it piston \ piston \
python3 /$file timeout -s HUP 3 $bin /$file 2>&1