Skip to content
This repository was archived by the owner on Jan 31, 2019. It is now read-only.

Commit 5e7c302

Browse files
author
nicekingwei
committed
code runner
1 parent 58eb55b commit 5e7c302

File tree

3 files changed

+111
-68
lines changed

3 files changed

+111
-68
lines changed

.travis.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ services:
55

66
script:
77
- echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
8-
# - mv ./travis.Dockerfile Dockerfile
9-
# - docker build . -t nicekingwei/zju-lambda-web:travis
10-
# - docker push nicekingwei/zju-lambda-web
11-
- mv code-runner/steak.Dockerfile Dockerfile
12-
- docker build . -t nicekingwei/zju-lambda-web:steak
8+
- mv ./travis.Dockerfile Dockerfile
9+
- docker build . -t nicekingwei/zju-lambda-web:travis
1310
- docker push nicekingwei/zju-lambda-web
1411

code-runner/index.js

Lines changed: 109 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,130 @@
1-
var https = require('https');
2-
var http = require('http');
1+
const https = require('https');
2+
const http = require('http');
3+
const fs = require('fs');
4+
const cp = require('child_process');
35

6+
/*
7+
* code runner
8+
*/
49
var token = 'ab4ee798-9a1a-4f86-993e-20c81cfd5857';
510

6-
var glot_langs = [
7-
'c', 'cpp', 'python', 'haskell', 'rust', 'scala', 'javascript', 'lua',
8-
'idris', 'java'
9-
];
10-
var glot_ext = [
11-
'.c', 'cpp', '.py', '.hs', '.rst', '.scala', '.js', '.lua', '.idr', '.java'
12-
];
11+
var glot_langs = {
12+
'c': '.c',
13+
'cpp': '.cpp',
14+
'python': '.py',
15+
'haskell': '.hs',
16+
'rust': '.rst',
17+
'scala': '.scala',
18+
'javascript': '.js',
19+
'lua': '.lua',
20+
'idris': '.idr',
21+
'java': '.java'
22+
};
23+
24+
var my_langs = {
25+
'steak': {
26+
ext: '.stk.cpp',
27+
docker: 'nicekingwei/steak',
28+
cmd: function() {
29+
return 'steak . && g++ -std=c++17 main.cpp && ./a.out';
30+
}
31+
},
32+
'coq': {
33+
ext: '.v',
34+
docker: 'skippa/coq',
35+
cmd: function(filename) {
36+
return 'coqc main.v';
37+
}
38+
},
39+
'agda': {
40+
ext: '.agda',
41+
docker: 'banacorn/agda',
42+
cmd: function(filename) {
43+
var head = 'module main where\n';
44+
var main_code = fs.readFileSync(filename, 'utf8');
45+
fs.writeFileSync(filename, head + main_code);
46+
return 'agda main.agda';
47+
}
48+
}
49+
};
50+
1351

1452
function runcode(lang, content, callback) {
15-
var use_glot = false;
16-
for (var i in glot_langs) {
17-
if (lang == glot_langs[i]) {
18-
use_glot = true;
19-
var options = {
20-
hostname: 'run.glot.io',
21-
path: '/languages/' + lang + '/latest',
22-
method: 'POST',
23-
headers: {
24-
'Content-Type': 'application/json',
25-
'Authorization': 'Token ' + token
26-
}
27-
};
53+
// glot runner
54+
var lang_setting = glot_langs[lang];
55+
56+
if (lang_setting) {
57+
var options = {
58+
hostname: 'run.glot.io',
59+
path: '/languages/' + lang + '/latest',
60+
method: 'POST',
61+
headers: {
62+
'Content-Type': 'application/json',
63+
'Authorization': 'Token ' + token
64+
}
65+
};
2866

29-
var data = {
30-
'files': [{'name': 'main' + glot_ext[i], 'content': content}]
31-
};
67+
var data = {'files': [{'name': 'main' + lang_setting, 'content': content}]};
3268

33-
var req = https.request(options, function(res) {
34-
var status = res.statusCode;
35-
res.setEncoding('utf8');
36-
res.on('data', (chunk) => {
37-
var data = JSON.parse(chunk);
38-
if (data.stdout) {
39-
callback(data.stdout);
40-
} else if (data.stderr) {
41-
callback(data.stderr);
42-
} else {
43-
callback(data.error);
44-
}
45-
});
69+
var req = https.request(options, function(res) {
70+
var status = res.statusCode;
71+
res.setEncoding('utf8');
72+
res.on('data', (chunk) => {
73+
var data = JSON.parse(chunk);
74+
if (data.stdout) {
75+
callback(data.stdout);
76+
} else if (data.stderr) {
77+
callback(data.stderr);
78+
} else {
79+
callback(data.error);
80+
}
4681
});
82+
});
4783

48-
req.write(JSON.stringify(data));
49-
req.end();
50-
break;
51-
}
52-
}
84+
req.write(JSON.stringify(data));
85+
req.end();
86+
} else {
87+
lang_setting = my_langs[lang];
88+
if (lang_setting) {
89+
var rand = Math.floor(Math.random() * 1e9).toString();
90+
cp.execSync('mkdir -p ' + rand);
91+
var filename = rand + '/main' + lang_setting.ext;
92+
fs.writeFileSync(filename, content);
93+
94+
var cmd = 'docker run -v /home/ubuntu/code/' + rand +
95+
':/tmp/code -w /tmp/code ' + lang_setting.docker + ' ' +
96+
lang_setting.cmd();
5397

54-
if (!use_glot) {
55-
callback();
98+
cp.exec(cmd, function(err, stdout, stderr) {
99+
if (stdout) {
100+
callback(stdout);
101+
} else if (stderr) {
102+
callback(stderr);
103+
} else {
104+
callback(err);
105+
}
106+
});
107+
} else {
108+
callback();
109+
}
56110
}
57111
}
58112

59-
http.createServer(function(req, res) {
113+
114+
function server(req, res) {
60115
if (req.method === 'POST') {
61116
var body = '';
62117
req.on('data', function(chunk) {
63118
body += chunk;
64119
});
65120
req.on('end', function() {
66-
67-
res.writeHead(200, {'Content-Type': 'text/html'});
68-
res.end(body);
121+
var data = JSON.parse(body);
122+
res.writeHead(201, {'Content-Type': 'text/html'});
123+
runcode(data.lang, data.code, function(result) {
124+
res.write(result);
125+
res.end();
126+
});
69127
});
70128
}
71-
}).listen(4368);
72-
73-
runcode('python', 'print(42)', console.log);
129+
}
130+
http.createServer(server).listen(4368);

code-runner/steak.Dockerfile

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)