-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
51 lines (43 loc) · 1.33 KB
/
app.js
File metadata and controls
51 lines (43 loc) · 1.33 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
var express = require('express');
var app = express();
var fs = require('fs');
const {exec} = require('child_process');
const uuidv1 = require('uuid/v1');
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({extended: true})); // support encoded bodies
app.get('/', function (req, res) {
res.sendFile("index.html", {root: __dirname});
});
app.post('/compile', function (req, res) {
let code = req.body.code;
//create dir
let dir = tmpDir();
console.log('folder created')
//create file
fs.writeFile(dir + '/file.php', code, function (err) {
if (err) throw err;
//run docker
let command = `docker run --rm -v $(pwd):/app -w /app php:cli php ${dir}/file.php`;
console.log(command)
exec(command, (err, stdout, stderr) => {
res.json({
"output": stdout
});
//delete file and folder
fs.unlinkSync(dir + "/file.php");
fs.rmdirSync(dir)
console.log('folder removed')
});
});
});
app.listen(3000, function () {
console.log('app listening on port 3000!');
});
function tmpDir() {
var dir = 'temp/' + uuidv1();
fs.mkdir(dir, {recursive: true}, (err) => {
if (err) throw err;
});
return dir;
}