66 "bytes"
77 "context"
88 "encoding/json"
9+ "errors"
910 "fmt"
1011 "io/ioutil"
1112 "log"
@@ -29,6 +30,10 @@ func (r Response) toJSON() string {
2930 return string (data )
3031}
3132
33+ func (r Response ) Done () {
34+ fmt .Print (r .toJSON ())
35+ }
36+
3237func execSandbox (interpreter string , timeout time.Duration , code []byte ) * Response {
3338 res := Response {}
3439 ApplySyscallRestrictions ()
@@ -67,57 +72,68 @@ func debug(msg string, args ...interface{}) {
6772 }
6873}
6974
70- func main () {
71- debug ("main go code started" )
72- language := os .Getenv ("LANGUAGE" )
73- codeFolder := os .Getenv ("CODEFOLDER" )
74-
75- if codeFolder == "" {
76- codeFolder = "/mnt/code"
75+ func getenv (key string , defaultValue string ) string {
76+ v := os .Getenv (key )
77+ if v == "" {
78+ return defaultValue
7779 }
80+ return v
81+ }
7882
79- filename := path .Join (codeFolder , os .Getenv ("CODEFILE" ))
80- timeoutEnv := os .Getenv ("TIMEOUT" )
83+ func lookupInterpreter (language string ) (string , error ) {
84+ var interpreter string
85+ var err error
8186
82- if timeoutEnv == "" {
83- timeoutEnv = "5"
87+ switch language {
88+ case "python" :
89+ case "py" :
90+ interpreter = "/usr/bin/python3"
91+ case "javascript" :
92+ case "js" :
93+ case "node" :
94+ interpreter = "node"
95+ case "" :
96+ err = errors .New ("language not specified" )
97+ default :
98+ err = errors .New ("unrecognized language: " + language )
8499 }
85100
86- timeout , err := strconv .Atoi (timeoutEnv )
87- duration := time .Duration (timeout ) * time .Second
101+ return interpreter , err
102+ }
103+
104+ func main () {
105+ var (
106+ interpreter string
107+ response * Response
108+ filename string
109+ timeout int
110+ code []byte
111+ )
112+
113+ debug ("main go code started" )
114+
115+ filename = path .Join (getenv ("CODEFOLDER" , "/mnt/code" ), os .Getenv ("CODEFILE" ))
116+ timeout , err := strconv .Atoi (getenv ("TIMEOUT" , "5" ))
88117
89118 if err != nil {
90119 panic (err .Error ())
91120 }
92121
93122 debug ("reading file: %s" , filename )
94- code , err := ioutil .ReadFile (filename )
95- // data := string(code)
123+ code , err = ioutil .ReadFile (filename )
96124
97125 if err != nil {
98126 panic (err .Error ())
99127 }
100128
101- var (
102- interpreter string
103- response * Response
104- )
129+ interpreter , err = lookupInterpreter (os .Getenv ("LANGUAGE" ))
105130
106- switch language {
107- case "python" :
108- case "py" :
109- interpreter = "/usr/bin/python3"
110- case "javascript" :
111- case "js" :
112- case "node" :
113- interpreter = "node"
114- default :
115- response = & Response {Error : "unrecognized language: " + language }
116- fmt .Print (response .toJSON ())
131+ if err != nil {
132+ response = & Response {Error : err .Error ()}
133+ response .Done ()
117134 return
118135 }
119136
120- response = execSandbox (interpreter , duration , code )
121-
122- fmt .Print (response .toJSON ())
137+ response = execSandbox (interpreter , time .Duration (timeout )* time .Second , code )
138+ response .Done ()
123139}
0 commit comments