piston/api/main.go

261 lines
5.7 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",
2020-10-03 01:51:00 +02:00
"brainfuck", "bf",
"c",
"cpp", "c++",
"csharp", "cs", "c#",
2020-10-17 20:57:26 +02:00
"deno", "denojs", "denots",
"elixir", "exs",
"emacs", "elisp", "el",
"go",
2020-10-17 20:57:26 +02:00
"haskell", "hs",
"java",
2020-10-17 19:56:02 +02:00
"jelly",
2020-05-02 06:00:46 +02:00
"julia", "jl",
2020-06-04 21:01:14 +02:00
"kotlin",
2020-10-07 06:40:14 +02:00
"lua",
"nasm", "asm",
2020-10-07 06:41:13 +02:00
"nasm64", "asm64",
"node", "javascript", "js",
2020-06-05 03:01:21 +02:00
"perl", "pl",
"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 {
2020-10-03 01:51:00 +02:00
case "bf":
execlang = "brainfuck"
case "c++":
execlang = "cpp"
case "cs", "c#":
execlang = "csharp"
2020-10-17 20:57:26 +02:00
case "denojs", "denots":
execlang = "deno"
case "el", "elisp":
execlang = "emacs"
case "exs":
2020-06-04 22:38:12 +02:00
execlang = "elixir"
2020-10-17 20:57:26 +02:00
case "hs":
execlang = "haskell"
case "asm":
execlang = "nasm"
2020-10-07 06:41:13 +02:00
case "asm64":
execlang = "nasm64"
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])
}
if lines[1] == "emacs" {
return "emacs", regexp.MustCompile("([0-9]+\\.[0-9]+)").FindString(lines[2])
}
return lines[1], regexp.MustCompile("([0-9]+\\.[0-9]+\\.[0-9]+)").FindString(s)
}