63 lines
1.5 KiB
Go
63 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"os/exec"
|
|
"strings"
|
|
|
|
// import file writer
|
|
"os"
|
|
// import websocket
|
|
)
|
|
|
|
func handleCase(caseName string, data string) strings.Builder {
|
|
// get the first line of the message from the client
|
|
// strip : from the caseName
|
|
caseName = strings.Replace(caseName, ":", "", -1)
|
|
returnData := strings.Builder{}
|
|
switch caseName {
|
|
case "py", "python", "python3":
|
|
log.Println("Python is not supported yet")
|
|
returnData = runPython(data)
|
|
case "go", "golang":
|
|
log.Println("Golang is not supported yet")
|
|
// returnData = runGo(data)
|
|
case "js", "javascript":
|
|
log.Println("Javascript is not supported yet")
|
|
// returnData = runJs(data)
|
|
case "cpp", "c++":
|
|
log.Println("C++ is not supported yet")
|
|
}
|
|
return returnData
|
|
}
|
|
|
|
func runPython(data string) strings.Builder {
|
|
// write Data to `./workspace/main.py`
|
|
file, err := os.Create("./workspace/main.py")
|
|
if err != nil {
|
|
log.Println("Error creating file")
|
|
}
|
|
defer file.Close()
|
|
file.WriteString(data)
|
|
|
|
// find python, python3, or py in the system
|
|
fname, err := exec.LookPath("python3")
|
|
if err != nil {
|
|
log.Println("Python not found")
|
|
}
|
|
|
|
// run the python file
|
|
cmd := exec.Command(fname, "./workspace/main.py")
|
|
// write output to value, and then return it
|
|
cmdOut := &strings.Builder{}
|
|
cmd.Stdout = cmdOut
|
|
cmdErr := &strings.Builder{}
|
|
cmd.Stderr = cmdErr
|
|
cmd.Run()
|
|
|
|
// combine the output and error
|
|
cmdOut.WriteString(cmdErr.String())
|
|
// print the output to the console
|
|
log.Println(cmdOut.String())
|
|
return *cmdOut
|
|
}
|