-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
58 lines (41 loc) · 1.95 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
const express = require('express')
const cors = require('cors')
const fs = require('fs')
var bodyParser = require('body-parser')
const myfunc = require('./execCode.js')
const app = express();
app.use(cors())
app.use(bodyParser.json())
var Docker = require('dockerode');
const { Volume, Container } = require('dockerode')
var docker = new Docker({
socketPath: '/var/run/docker.sock'
});
app.post('/', (req, res) => {
// random number has been generated such that for every request, a new container is formed.
randomNumber = Math.floor(Math.random() * 100)
switch(req.query.lang){
case 'Python':
// a timeout of 5s is given. In case docker on machine is taking time for compiling the code, you can increase the time.
myfunc.execCode(randomNumber, 'Python', 'py', 'python', ['bash', '-c', 'cd var && cat input.txt | timeout 5 python code.py > output.txt 2>&1'], req.query.input, req.body.code, (data) => {
fs.readFile('./Python/output.txt', (error, data) => {
res.send(data);
})
}
)
case 'Cpp':
myfunc.execCode(randomNumber, 'cpp', 'cpp', 'gcc:4.9', ['bash', '-c', 'cd var && g++ -std=c++14 -o binary code.cpp > output.txt 2>&1 && cat input.txt | timeout 5 ./binary > output.txt'], req.query.input, req.body.code, (data) => {
fs.readFile('./cpp/output.txt', (error, data) => {
res.send(data)
});
})
case 'Java':
myfunc.execCode(randomNumber, 'Java', 'java', 'openjdk', ['bash', '-c', 'cd var && javac code.java > output.txt 2>&1 && cat input.txt | timeout 5 java Main > output.txt'], req.query.input, req.body.code, (data) => {
fs.readFile('./Java/output.txt', (error, data) => {
res.send(data);
});
})
})
app.listen(4000, () =>
console.log('Server is listening on port 4000!'),
);