add additional executors
This commit is contained in:
parent
64eda86817
commit
ef9c3ae091
27
api/main.go
27
api/main.go
|
@ -2,13 +2,13 @@ package main
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"time"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type inbound struct {
|
||||
|
@ -33,6 +33,7 @@ func main() {
|
|||
}
|
||||
|
||||
func Execute(res http.ResponseWriter, req *http.Request) {
|
||||
var found = false
|
||||
res.Header().Set("Content-Type", "application/json")
|
||||
|
||||
// get json
|
||||
|
@ -41,26 +42,28 @@ func Execute(res http.ResponseWriter, req *http.Request) {
|
|||
message.Decode(&inbound)
|
||||
|
||||
whitelist := []string{
|
||||
"python2",
|
||||
"python",
|
||||
"python2",
|
||||
"python3",
|
||||
"ruby",
|
||||
"javascript",
|
||||
"js",
|
||||
"node",
|
||||
"c",
|
||||
"cpp",
|
||||
"c++",
|
||||
}
|
||||
|
||||
found := false
|
||||
|
||||
// check if the supplied language is supported
|
||||
// now calls function and returns
|
||||
for _, lang := range whitelist {
|
||||
if lang == inbound.Language {
|
||||
found = true
|
||||
break
|
||||
launch(inbound, res)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// send an error if it isn't
|
||||
// now only called when the language is not supported
|
||||
if !found {
|
||||
problem := problem{
|
||||
Code: "unsupported_language",
|
||||
|
@ -72,14 +75,16 @@ func Execute(res http.ResponseWriter, req *http.Request) {
|
|||
res.Write(pres)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func launch(inbound2 inbound, res http.ResponseWriter){
|
||||
// write the code to temp dir
|
||||
filename := fmt.Sprintf("/tmp/%d.code", time.Now().UnixNano())
|
||||
|
||||
ioutil.WriteFile(filename, []byte(inbound.Source), 0644)
|
||||
ioutil.WriteFile(filename, []byte(inbound2.Source), 0644)
|
||||
|
||||
// set up the execution
|
||||
cmd := exec.Command("../docker/execute", inbound.Language, filename)
|
||||
cmd := exec.Command("../docker/execute", inbound2.Language, filename)
|
||||
|
||||
// capture out/err
|
||||
var stdout, stderr bytes.Buffer
|
||||
|
|
|
@ -7,5 +7,3 @@ 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
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
#docker run --rm -it piston /usr/bin/ruby --version
|
||||
#docker run --rm -it piston /usr/bin/nodejs --version
|
||||
|
||||
dir="$( cd "$( dirname "$0" )" && pwd )"
|
||||
|
||||
if [ -z "$1" ]; then
|
||||
echo "invalid args"
|
||||
exit
|
||||
|
@ -21,10 +23,10 @@ file=$(basename $abs)
|
|||
bin=
|
||||
case "$lang" in
|
||||
"python2")
|
||||
bin=python2
|
||||
bin=executor_python2
|
||||
;;
|
||||
"python" | "python3")
|
||||
bin=python3
|
||||
bin=executor_python3
|
||||
;;
|
||||
"ruby")
|
||||
bin=ruby
|
||||
|
@ -32,6 +34,12 @@ case "$lang" in
|
|||
"javascript" | "js" | "node")
|
||||
bin=nodejs
|
||||
;;
|
||||
"c")
|
||||
bin=executor_c
|
||||
;;
|
||||
"cpp" | "c++")
|
||||
bin=executor_cpp
|
||||
;;
|
||||
*)
|
||||
echo "invalid language"
|
||||
exit
|
||||
|
@ -42,5 +50,9 @@ docker run \
|
|||
--cpus=".5" \
|
||||
--rm \
|
||||
-v $abs:/$file \
|
||||
-v $dir/executors/python2:/executor_python2 \
|
||||
-v $dir/executors/python3:/executor_python3 \
|
||||
-v $dir/executors/c:/executor_c \
|
||||
-v $dir/executors/cpp:/executor_cpp \
|
||||
piston \
|
||||
timeout -s HUP 3 $bin /$file 2>&1
|
||||
timeout -s HUP 3 /$bin /$file $file 2>&1
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
#!/bin/sh
|
||||
|
||||
gcc -o binary -x c $1
|
||||
./binary
|
|
@ -0,0 +1,4 @@
|
|||
#!/bin/sh
|
||||
|
||||
g++ -o binary -x c $1
|
||||
./binary
|
|
@ -0,0 +1,3 @@
|
|||
#!/bin/sh
|
||||
|
||||
python2 $*
|
|
@ -0,0 +1,3 @@
|
|||
#!/bin/sh
|
||||
|
||||
python3.6 $*
|
Loading…
Reference in New Issue