Skip to content

Commit a017757

Browse files
committed
added c/c++ endpoints
1 parent 1eb8f83 commit a017757

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

‎v4-cokapi/cokapi.js‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ var PYTHON3_BIN = '/usr/local/bin/python3.3';
5959

6060
var TIMEOUT_SECS = 3;
6161
var JAVA_TIMEOUT_SECS = 15; // the Java backend is SUPER SLOW :/
62+
var CPP_TIMEOUT_SECS = 15; // the C/C++ backend is also SUPER SLOW :/
6263

6364

6465
// bind() res and useJSONP before using
@@ -241,6 +242,42 @@ function exec_ruby_handler(useJSONP /* use bind first */, req, res) {
241242
postExecHandler.bind(null, res, useJSONP));
242243
}
243244

245+
app.get('/exec_c', exec_cpp_handler.bind(null, false, false));
246+
app.get('/exec_c_jsonp', exec_cpp_handler.bind(null, false, true));
247+
app.get('/exec_cpp', exec_cpp_handler.bind(null, true, false));
248+
app.get('/exec_cpp_jsonp', exec_cpp_handler.bind(null, true, true));
249+
250+
function exec_cpp_handler(useCPP /* use bind first */, useJSONP /* use bind first */, req, res) {
251+
var usrCod = req.query.user_script;
252+
253+
var exeFile;
254+
var args = [];
255+
256+
if (USE_DOCKER_SANDBOX) {
257+
// this needs to match the docker setup in Dockerfile
258+
exeFile = '/usr/bin/docker'; // absolute path to docker executable
259+
args.push('run', '--rm', '--user=netuser', '--net=none', '--cap-drop', 'all', 'pgbovine/opt-cpp-backend:v1',
260+
'/tmp/opt-cpp-backend/run-cpp-backend.sh',
261+
usrCod,
262+
useCPP ? 'cpp' : 'c');
263+
} else {
264+
assert(false);
265+
// must use Docker
266+
}
267+
268+
child_process.execFile(exeFile, args,
269+
{timeout: CPP_TIMEOUT_SECS * 1000 /* milliseconds */,
270+
maxBuffer: 5000 * 1024 /* 5MB data max */,
271+
// make SURE docker gets the kill signal;
272+
// this signal seems to allow docker to clean
273+
// up after itself to --rm the container, but
274+
// double-check with 'docker ps -a'
275+
killSignal: 'SIGINT'},
276+
postExecHandler.bind(null, res, useJSONP));
277+
}
278+
279+
280+
244281
function executePython(pyVer, req, res) {
245282
var parsedOptions = JSON.parse(req.query.options_json);
246283

0 commit comments

Comments
 (0)