-
Notifications
You must be signed in to change notification settings - Fork 5
/
execCode.js
48 lines (36 loc) · 1.16 KB
/
execCode.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
const express = require('express')
const cors = require('cors')
const fs = require('fs')
const app = express();
app.use(cors())
var Docker = require('dockerode');
const { Volume, Container } = require('dockerode')
var docker = new Docker({
socketPath: '/var/run/docker.sock'
});
async function execCode(randomNumber, lang, ext, img_name, run_cmd, input, code, callback) {
fs.writeFile(`./${lang}/input.txt`, input, function (err) {
if (err) {
throw err;
}
})
fs.writeFile(`./${lang}/code.${ext}`, code, function (err) {
if (err) {
throw err;
}
else {
docker.run(img_name, run_cmd, process.stdout, {
name: `imageName_${randomNumber}`, HostConfig: {
AutoRemove: true, NetworkMode: 'bridge', Binds: [
`${__dirname}/${lang}/:/var/`
]
}
}).then(function (err, data, container) {
fs.readFile(`./${lang}/output.txt`, (error, data) => {
callback(data)
});
})
}
})
};
module.exports = { execCode };