-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhandle_golang.go
49 lines (42 loc) · 1.04 KB
/
handle_golang.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"bytes"
"io/ioutil"
"net/http"
"os/exec"
"github.com/labstack/echo/v4"
)
func copy(src string, dst string) error {
data, err := ioutil.ReadFile(src)
if err != nil {
return err
}
err = ioutil.WriteFile(dst, data, 0644)
return err
}
func golangHandler(c echo.Context, req *runReq) error {
// TODO handle variant
err := copy("/tmp/"+req.ID, "/tmp/"+req.ID+".go")
if err != nil {
return c.JSON(http.StatusInternalServerError, runCRes{
Message: "Failed to copy file",
Error: err.Error(),
})
}
// Compile code
var compileStdOut, compileStdErr bytes.Buffer
compileCmd := exec.Command("go", "build", "-o", "/tmp/"+req.ID+".out", "/tmp/"+req.ID+".go")
compileCmd.Stdout = &compileStdOut
compileCmd.Stderr = &compileStdErr
err = compileCmd.Run()
if err != nil {
return c.JSON(http.StatusBadRequest, runCRes{
Message: "Failed to compile",
Error: err.Error(),
Stdout: compileStdOut.String(),
Stderr: compileStdErr.String(),
})
}
// Run executable
return execCmd(c, "/tmp/"+req.ID+".out")
}