piston/api/main.go

236 lines
5.1 KiB
Go
Raw Normal View History

2018-09-19 19:55:42 +02:00
package main
2018-09-20 07:49:02 +02:00
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os/exec"
"regexp"
"strings"
"time"
2018-09-20 07:49:02 +02:00
)
type Inbound struct {
Language string `json:"language"`
Source string `json:"source"`
Args []string `json:"args"`
2018-09-20 07:49:02 +02:00
}
type Problem struct {
Code string `json:"code"`
Message string `json:"message"`
2018-09-20 17:21:46 +02:00
}
type Outbound struct {
Ran bool `json:"ran"`
Language string `json:"language"`
Version string `json:"version"`
Output string `json:"output"`
}
type Language struct {
Name string `json:"name,omitempty"`
Version string `json:"version,omitempty"`
2018-09-20 07:49:02 +02:00
}
2018-09-19 19:55:42 +02:00
2018-10-08 00:54:29 +02:00
var instance int
var languages []Language
2018-10-08 00:54:29 +02:00
2018-09-19 19:55:42 +02:00
func main() {
port := "2000"
var err error
languages, err = UpdateVersions()
if err != nil {
fmt.Println("could not get version info and therefore couldn't start")
fmt.Println(err)
return
}
2018-10-08 00:54:29 +02:00
fmt.Println("starting api on port", port)
http.HandleFunc("/execute", Execute)
http.HandleFunc("/versions", Versions)
http.ListenAndServe(":"+port, nil)
2018-09-20 07:49:02 +02:00
}
func Execute(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "application/json")
// get json
inbound := Inbound{}
message := json.NewDecoder(req.Body)
message.Decode(&inbound)
whitelist := []string{
"awk",
"bash",
"c",
"cpp", "c++",
"csharp", "cs", "c#",
"elixir", "exs",
"go",
"java",
2020-05-02 06:00:46 +02:00
"julia", "jl",
"nasm", "asm",
"node", "javascript", "js",
"php",
"python2",
"python3", "python",
"ruby",
"rust",
"swift",
"typescript", "ts",
}
// check if the supplied language is supported
// now calls function and returns
for _, lang := range whitelist {
if lang == inbound.Language {
launch(inbound, res)
return
}
}
// now only called when the language is not supported
problem := Problem{
Code: "unsupported_language",
Message: inbound.Language + " is not supported by Piston",
}
pres, _ := json.Marshal(problem)
res.Write(pres)
2018-09-20 19:04:34 +02:00
}
2018-09-20 07:49:02 +02:00
func Versions(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "application/json")
data, _ := json.Marshal(languages)
res.Write(data)
}
func launch(request Inbound, res http.ResponseWriter) {
stamp := time.Now().UnixNano()
// write the code to temp dir
srcfile := fmt.Sprintf("/tmp/%d.code", stamp)
ioutil.WriteFile(srcfile, []byte(request.Source), 0644)
2018-09-21 05:17:46 +02:00
// set up the arguments to send to the execute command
cmd := exec.Command("../lxc/execute", request.Language, srcfile, strings.Join(request.Args, "\n"))
2018-09-20 07:49:02 +02:00
// capture out/err
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
2018-09-20 07:49:02 +02:00
err := cmd.Run()
2018-09-20 07:49:02 +02:00
// get the executing version of the language
execlang := request.Language
switch execlang {
case "c++":
execlang = "cpp"
case "cs", "c#":
execlang = "csharp"
case "asm":
execlang = "nasm"
case "js", "javascript":
execlang = "node"
2020-05-02 06:30:12 +02:00
case "jl":
execlang = "julia"
case "python":
execlang = "python3"
case "ts":
execlang = "typescript"
}
// prepare response
outbound := Outbound{
Ran: err == nil,
Language: request.Language,
Version: "",
Output: strings.TrimSpace(stdout.String()),
}
2018-09-20 07:49:02 +02:00
// retrieve the language version
for _, lang := range languages {
if lang.Name == execlang {
outbound.Version = lang.Version
break
}
}
response, _ := json.Marshal(outbound)
2018-09-20 07:49:02 +02:00
res.Write(response)
}
func UpdateVersions() ([]Language, error) {
langs, err := GetVersions()
if err != nil {
return nil, err
}
return langs, nil
}
2018-09-20 07:49:02 +02:00
// get all the language and their current version
func GetVersions() ([]Language, error) {
var languages []Language
res, err := ExecVersionScript()
if err != nil {
return nil, err
}
info := strings.Split(res, "---")
for _, v := range info {
if len(v) < 2 {
continue
}
name, version := GetVersion(v)
languages = append(languages, Language{
Name: name,
Version: version,
})
}
return languages, nil
}
// run the script that retrieves all the language versions
func ExecVersionScript() (string, error) {
cmd := exec.Command("../lxc/versions")
var stdout bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stdout
err := cmd.Run()
return strings.ToLower(stdout.String()), err
}
2018-09-20 07:49:02 +02:00
// return the language and its version
// most of the time it is easy to get the name and version
// but for some languages helper functions are used
func GetVersion(s string) (string, string) {
lines := strings.Split(s, "\n")
if lines[1] == "java" {
return "java", regexp.MustCompile("([0-9]+)").FindString(lines[2])
}
return lines[1], regexp.MustCompile("([0-9]+\\.[0-9]+\\.[0-9]+)").FindString(s)
}