mirror of
https://github.com/engineer-man/piston.git
synced 2025-04-22 04:56:30 +02:00
mvp done
This commit is contained in:
parent
80aad50223
commit
f719f62f62
6 changed files with 109 additions and 5 deletions
59
api/main.go
59
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)
|
||||
}
|
||||
|
|
3
api/start
Executable file
3
api/start
Executable file
|
@ -0,0 +1,3 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
go run main.go
|
Loading…
Add table
Add a link
Reference in a new issue