@@ -29,6 +29,11 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2929// This is a nodejs server based on express that serves the v4-cokapi/ app
3030// To test locally, run 'make' and load http://localhost:3000/
3131
32+ var IS_DEBUG = true ;
33+
34+ var PRODUCTION_PORT = 3000 ;
35+ var DEBUG_PORT = 5001 ;
36+
3237// VERY IMPORTANT - turn on the sandbox when deploying online, or else
3338// you'll be executing untrusted code on your server!
3439var USE_DOCKER_SANDBOX = true ;
@@ -51,6 +56,7 @@ var PYTHON2_BIN = '/usr/bin/python';
5156var PYTHON3_BIN = '/usr/local/bin/python3.3' ;
5257
5358var TIMEOUT_SECS = 3 ;
59+ var JAVA_TIMEOUT_SECS = 6 ; // the Java backend is SUPER SLOW :/
5460
5561
5662// bind() res and useJSONP before using
@@ -128,7 +134,50 @@ function exec_js_handler(useJSONP /* use bind first */, req, res) {
128134
129135 child_process . execFile ( exeFile , args ,
130136 { timeout : TIMEOUT_SECS * 1000 /* milliseconds */ ,
131- maxBuffer : 2000 * 1024 /* 2MB data max */ ,
137+ maxBuffer : 5000 * 1024 /* 5MB data max */ ,
138+ // make SURE docker gets the kill signal;
139+ // this signal seems to allow docker to clean
140+ // up after itself to --rm the container, but
141+ // double-check with 'docker ps -a'
142+ killSignal : 'SIGINT' } ,
143+ postExecHandler . bind ( null , res , useJSONP ) ) ;
144+ }
145+
146+ app . get ( '/exec_java' , exec_java_handler . bind ( null , false ) ) ;
147+ app . get ( '/exec_java_jsonp' , exec_java_handler . bind ( null , true ) ) ;
148+
149+ function exec_java_handler ( useJSONP /* use bind first */ , req , res ) {
150+ var usrCod = req . query . user_script ;
151+
152+ var exeFile ;
153+ var args = [ ] ;
154+
155+ var inputObj = { } ;
156+ inputObj . usercode = usrCod ;
157+ //inputObj.usercode = "public class Testtt { public static void main(String[] args) { int x = 3; x += x; } }"; // for testing
158+ // TODO: add options, arg, and stdin later ...
159+ inputObj . options = { } ;
160+ inputObj . args = [ ] ;
161+ inputObj . stdin = "" ;
162+
163+ var inputObjJSON = JSON . stringify ( inputObj ) ;
164+
165+ if ( USE_DOCKER_SANDBOX ) {
166+ // this needs to match the docker setup in Dockerfile
167+ exeFile = '/usr/bin/docker' ; // absolute path to docker executable
168+ args . push ( 'run' , '--rm' , 'pgbovine/cokapi:v1' ,
169+ '/tmp/run-java-backend.sh' ,
170+ inputObjJSON ) ;
171+
172+ //'{ "usercode": "public class Test \n\n{ public static void main(String[] args) { \n\nint x = 3; \nx += x; } }", "options": {}, "args": [], "stdin": "" }');
173+
174+ } else {
175+ assert ( false ) ;
176+ }
177+
178+ child_process . execFile ( exeFile , args ,
179+ { timeout : JAVA_TIMEOUT_SECS * 1000 /* milliseconds */ ,
180+ maxBuffer : 5000 * 1024 /* 10MB data max */ ,
132181 // make SURE docker gets the kill signal;
133182 // this signal seems to allow docker to clean
134183 // up after itself to --rm the container, but
@@ -172,7 +221,7 @@ function executePython(pyVer, req, res) {
172221
173222 child_process . execFile ( exeFile , args ,
174223 { timeout : TIMEOUT_SECS * 1000 /* milliseconds */ ,
175- maxBuffer : 2000 * 1024 /* 2MB data max */ ,
224+ maxBuffer : 5000 * 1024 /* 5MB data max */ ,
176225 // make SURE docker gets the kill signal;
177226 // this signal seems to allow docker to clean
178227 // up after itself to --rm the container, but
@@ -213,7 +262,7 @@ app.get('/feedback', function(req, res) {
213262} ) ;
214263
215264
216- var server = app . listen ( 3000 , function ( ) {
265+ var server = app . listen ( IS_DEBUG ? DEBUG_PORT : PRODUCTION_PORT , function ( ) {
217266 var host = server . address ( ) . address ;
218267 var port = server . address ( ) . port ;
219268 console . log ( 'Example app listening at http://%s:%s' , host , port ) ;
0 commit comments