mirror of
https://github.com/engineer-man/piston.git
synced 2025-04-20 20:16:26 +02:00
add additional executors
This commit is contained in:
parent
64eda86817
commit
ef9c3ae091
7 changed files with 45 additions and 16 deletions
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue