-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (42 loc) · 1.48 KB
/
index.js
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
'use strict';
const app = require('express')();
const bodyParser = require('body-parser');
// application
const fs = require('fs');
const cps = require('child_process');
const Puid = require('puid');
const puid = new Puid(true);
// express
app.set('port', (process.env.PORT || 3000));
app.use(bodyParser.json({limit: '10mb'}));
app.get('/', (req, res, next) => res.send('Hello World!'));
app.post('/', (req, res, next) => {
const {inOuts, lang, code} = req.body;
let success = false;
if (!Array.isArray(inOuts) || typeof lang !== 'string' || typeof code !== 'string') {
res.json({success, out: 'Invalid Request'});
return;
}
const codeId = puid.generate();
fs.mkdirSync(`input/${codeId}`);
fs.mkdirSync(`answer/${codeId}`);
fs.writeFileSync(`input/${codeId}/code`, code);
for (let i = 1; i <= inOuts.length; i++) {
const {input, output} = inOuts[Math.floor(Math.random() * inOuts.length)];
fs.writeFileSync(`input/${codeId}/hole${i}`, input);
fs.writeFileSync(`answer/${codeId}/hole${i}`, output);
}
const env = `LANG="${lang}" CODEID="${codeId}" `;
const command = 'bash ./task.sh';
try {
cps.execSync(env + command, {timeout: 30 * 1000});
success = true;
} catch (e) {
}
const out = fs.readFileSync(`output/${codeId}`, 'utf8');
cps.exec(`rm -rf ./input/${codeId} ./answer/${codeId} ./output/${codeId}`);
res.json({success, out});
});
app.listen(app.get('port'), () => {
console.log('Node app is running on port', app.get('port'));
});