mirror of
https://github.com/engineer-man/piston.git
synced 2025-09-20 10:50:04 +02:00
Update main.go
This commit is contained in:
parent
a14a810925
commit
478aca1cea
1 changed files with 101 additions and 102 deletions
23
api/main.go
23
api/main.go
|
@ -5,12 +5,11 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -48,9 +47,7 @@ func main() {
|
||||||
fmt.Println("starting api on port", port)
|
fmt.Println("starting api on port", port)
|
||||||
|
|
||||||
http.HandleFunc("/execute", Execute)
|
http.HandleFunc("/execute", Execute)
|
||||||
err := http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), nil); if err != nil{
|
http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), nil)
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Execute(res http.ResponseWriter, req *http.Request) {
|
func Execute(res http.ResponseWriter, req *http.Request) {
|
||||||
|
@ -90,8 +87,9 @@ func Execute(res http.ResponseWriter, req *http.Request) {
|
||||||
Message: inbound.Language + " is not supported by Piston",
|
Message: inbound.Language + " is not supported by Piston",
|
||||||
}
|
}
|
||||||
|
|
||||||
pres, err := json.Marshal(problem); if err != nil{
|
pres, err := json.Marshal(problem)
|
||||||
log.Fatal(err)
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
res.Write(pres)
|
res.Write(pres)
|
||||||
|
@ -126,10 +124,10 @@ func launch(request inbound, res http.ResponseWriter) {
|
||||||
cmd.Stdout = &stdout
|
cmd.Stdout = &stdout
|
||||||
cmd.Stderr = &stderr
|
cmd.Stderr = &stderr
|
||||||
|
|
||||||
err := cmd.Run(); if err != nil{
|
err := cmd.Run()
|
||||||
log.Fatal(err)
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// prepare response
|
// prepare response
|
||||||
outbound := outbound{
|
outbound := outbound{
|
||||||
Generator: fmt.Sprintf("docker-%d", instance),
|
Generator: fmt.Sprintf("docker-%d", instance),
|
||||||
|
@ -137,8 +135,9 @@ func launch(request inbound, res http.ResponseWriter) {
|
||||||
Output: strings.TrimSpace(stdout.String()),
|
Output: strings.TrimSpace(stdout.String()),
|
||||||
}
|
}
|
||||||
|
|
||||||
response, err:= json.Marshal(outbound); if err != nil{
|
response, err := json.Marshal(outbound)
|
||||||
log.Fatal(err)
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
res.Write(response)
|
res.Write(response)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue