-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodellege.js
More file actions
59 lines (51 loc) · 1.64 KB
/
Codellege.js
File metadata and controls
59 lines (51 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var express = require('express'),
bodyParser = require('body-parser'),
fs = require('fs'),
exec = require('child_process').exec,
shortID = require('shortid');
cors = require('cors');
var app = express();
var port = 8081;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
/*app.post('/', function (req, res) {
var name = shortID.generate();
exec('docker run --name=' + name + ' -e "code=' + req.body.code + '" derrickh/codellege', function (err, stdout, stderr) {
res.send(stdout);
});
setTimeout(function() {
exec('docker stop ' + name, function() {
exec('docker rm ' + name);
});
}, 60000);
});*/
app.post('/', function (req, res) {
var id = shortID.generate();
var className = req.body.code.split('class ')[1].split('{')[0].trim(),
path = __dirname + '/' + id + '/',
javaFile = path + className + '.java',
classFile = path + className + '.class',
inputFile = path + 'input';
if ( ! fs.existsSync(path)) {
fs.mkdirSync(path);
}
if (fs.existsSync(javaFile)) {
fs.unlinkSync(javaFile);
}
fs.writeFileSync(javaFile, req.body.code);
fs.writeFileSync(inputFile, req.body.input);
exec('docker run --name=' + id + ' -v ' + path + ':/home derrickh/codellege:mount3', function (err, stdout, stderr) {
res.send(stdout);
});
// REMOVE CONTAINER AFTER 10 SECONDS
setTimeout(function() {
exec('docker stop ' + id, function() {
exec('docker rm ' + id);
});
// REMOVE USER'S CODE ON THE SERVER
exec('rm -rf ' + path);
}, 10000);
});
console.log('port: ' + port);
app.listen(port);