added code to write langues and their versions to a json file
This commit is contained in:
parent
bccd8c265f
commit
eb576d9b66
229
api/main.go
229
api/main.go
|
@ -1,121 +1,194 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"log"
|
||||||
"os/exec"
|
"net/http"
|
||||||
"strings"
|
"os"
|
||||||
"time"
|
"os/exec"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type inbound struct {
|
type inbound struct {
|
||||||
Language string `json:"language"`
|
Language string `json:"language"`
|
||||||
Source string `json:"source"`
|
Source string `json:"source"`
|
||||||
Args []string `json:"args"`
|
Args []string `json:"args"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type problem struct {
|
type problem struct {
|
||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type outbound struct {
|
type outbound struct {
|
||||||
Ran bool `json:"ran"`
|
Ran bool `json:"ran"`
|
||||||
Output string `json:"output"`
|
Output string `json:"output"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Language struct {
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Version string `json:"version,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Languages struct {
|
||||||
|
Languages []Language `json:"languages"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var instance int
|
var instance int
|
||||||
|
var versionRegex = regexp.MustCompile("([0-9]+\\.[0-9]+\\.[0-9]+)")
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
port := 2000
|
port := "2000"
|
||||||
|
|
||||||
fmt.Println("starting api on port", port)
|
updateVersions()
|
||||||
|
|
||||||
http.HandleFunc("/execute", Execute)
|
fmt.Println("starting api on port", port)
|
||||||
http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), nil)
|
http.HandleFunc("/execute", Execute)
|
||||||
|
http.ListenAndServe(":"+port, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Execute(res http.ResponseWriter, req *http.Request) {
|
func Execute(res http.ResponseWriter, req *http.Request) {
|
||||||
res.Header().Set("Content-Type", "application/json")
|
res.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
// get json
|
// get json
|
||||||
inbound := inbound{}
|
inbound := inbound{}
|
||||||
message := json.NewDecoder(req.Body)
|
message := json.NewDecoder(req.Body)
|
||||||
message.Decode(&inbound)
|
message.Decode(&inbound)
|
||||||
|
|
||||||
whitelist := []string{
|
whitelist := []string{
|
||||||
"c",
|
"c",
|
||||||
"cpp", "c++",
|
"cpp", "c++",
|
||||||
"c#", "csharp", "cs",
|
"c#", "csharp", "cs",
|
||||||
"go",
|
"go",
|
||||||
"java",
|
"java",
|
||||||
"nasm", "asm",
|
"nasm", "asm",
|
||||||
"javascript", "js", "node",
|
"javascript", "js", "node",
|
||||||
"typescript", "ts",
|
"typescript", "ts",
|
||||||
"php",
|
"php",
|
||||||
"python", "python2", "python3",
|
"python", "python2", "python3",
|
||||||
"ruby",
|
"ruby",
|
||||||
"swift",
|
"swift",
|
||||||
"rust",
|
"rust",
|
||||||
"bash",
|
"bash",
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if the supplied language is supported
|
// check if the supplied language is supported
|
||||||
// now calls function and returns
|
// now calls function and returns
|
||||||
for _, lang := range whitelist {
|
for _, lang := range whitelist {
|
||||||
if lang == inbound.Language {
|
if lang == inbound.Language {
|
||||||
launch(inbound, res)
|
launch(inbound, res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// now only called when the language is not supported
|
// now only called when the language is not supported
|
||||||
problem := problem{
|
problem := problem{
|
||||||
Code: "unsupported_language",
|
Code: "unsupported_language",
|
||||||
Message: inbound.Language + " is not supported by Piston",
|
Message: inbound.Language + " is not supported by Piston",
|
||||||
}
|
}
|
||||||
|
|
||||||
pres, _ := json.Marshal(problem)
|
pres, _ := json.Marshal(problem)
|
||||||
|
|
||||||
res.Write(pres)
|
res.Write(pres)
|
||||||
}
|
}
|
||||||
|
|
||||||
func launch(request inbound, res http.ResponseWriter) {
|
func launch(request inbound, res http.ResponseWriter) {
|
||||||
stamp := time.Now().UnixNano()
|
stamp := time.Now().UnixNano()
|
||||||
|
|
||||||
// write the code to temp dir
|
// write the code to temp dir
|
||||||
srcfile := fmt.Sprintf("/tmp/%d.code", stamp)
|
srcfile := fmt.Sprintf("/tmp/%d.code", stamp)
|
||||||
|
|
||||||
ioutil.WriteFile(srcfile, []byte(request.Source), 0644)
|
ioutil.WriteFile(srcfile, []byte(request.Source), 0644)
|
||||||
|
|
||||||
// set up the arguments to send to the execute command
|
// set up the arguments to send to the execute command
|
||||||
var args []string
|
var args []string
|
||||||
|
|
||||||
args = append(args, request.Language)
|
args = append(args, request.Language)
|
||||||
args = append(args, srcfile)
|
args = append(args, srcfile)
|
||||||
|
|
||||||
args = append(args, strings.Join(request.Args, "\n"))
|
args = append(args, strings.Join(request.Args, "\n"))
|
||||||
|
|
||||||
// set up the execution
|
// set up the execution
|
||||||
cmd := exec.Command("../lxc/execute", args...)
|
cmd := exec.Command("../lxc/execute", args...)
|
||||||
|
|
||||||
// capture out/err
|
// capture out/err
|
||||||
var stdout, stderr bytes.Buffer
|
var stdout, stderr bytes.Buffer
|
||||||
cmd.Stdout = &stdout
|
cmd.Stdout = &stdout
|
||||||
cmd.Stderr = &stderr
|
cmd.Stderr = &stderr
|
||||||
|
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
|
|
||||||
// prepare response
|
// prepare response
|
||||||
outbound := outbound{
|
outbound := outbound{
|
||||||
Ran: err == nil,
|
Ran: err == nil,
|
||||||
Output: strings.TrimSpace(stdout.String()),
|
Output: strings.TrimSpace(stdout.String()),
|
||||||
}
|
}
|
||||||
|
|
||||||
response, _ := json.Marshal(outbound)
|
response, _ := json.Marshal(outbound)
|
||||||
|
|
||||||
res.Write(response)
|
res.Write(response)
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateVersions() {
|
||||||
|
f, err := os.Create("versions.json")
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
langs, err := getVersions()
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
res, err := json.Marshal(langs)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
f.Write(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
// get all the language and their current version
|
||||||
|
func getVersions() ([]Language, error) {
|
||||||
|
var languages []Language
|
||||||
|
res, err := execVersionScript()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
languageInfo := strings.Split(res, "---")
|
||||||
|
for _, v := range languageInfo {
|
||||||
|
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) {
|
||||||
|
fmt.Println("running script")
|
||||||
|
output := bytes.Buffer{}
|
||||||
|
cmd := exec.Command("../lcx/versions")
|
||||||
|
cmd.Stdout = &output
|
||||||
|
err := cmd.Run()
|
||||||
|
return strings.ToLower(output.String()), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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")
|
||||||
|
return lines[1], versionRegex.FindString(lines[2])
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,82 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Language struct {
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Version string `json:"version,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Languages struct {
|
||||||
|
Languages []Language `json:"languages"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var versionRegex = regexp.MustCompile("([0-9]+\\.[0-9]+\\.[0-9]+)")
|
||||||
|
|
||||||
|
func updateVersions(){
|
||||||
|
f, err := os.Create("versions.json")
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
langs, err := getVersions()
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
res, err := json.Marshal(langs)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
f.Write(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
// get all the language and their current version
|
||||||
|
func getVersions() ([]Language, error) {
|
||||||
|
var languages []Language
|
||||||
|
res, err := execVersionScript()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
languageInfo := strings.Split(res, "---")
|
||||||
|
for _, v := range languageInfo {
|
||||||
|
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) {
|
||||||
|
fmt.Println("running script")
|
||||||
|
output := bytes.Buffer{}
|
||||||
|
cmd := exec.Command("../lcx/versions")
|
||||||
|
cmd.Stdout = &output
|
||||||
|
err := cmd.Run()
|
||||||
|
return strings.ToLower(output.String()), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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")
|
||||||
|
return lines[1], versionRegex.FindString(lines[2])
|
||||||
|
}
|
Loading…
Reference in New Issue