-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
149 lines (142 loc) · 4.46 KB
/
middleware.js
File metadata and controls
149 lines (142 loc) · 4.46 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const DockerMananger = require("./docker-manager");
const short = require("short-uuid");
const uuid = short();
const fs = require("fs");
// File Middleware
const createFiles = async (req, res, next) => {
req.sourceFile = uuid.new() + `.${req.params.language}`;
req.inputFile = uuid.new() + ".in";
fs.writeFile(`./project/src/${req.sourceFile}`, req.body.code, err => {
if (err) {
res.json({ error: err });
}
fs.writeFile(
`./project/IO/${req.inputFile}`,
req.body.input,
async err => {
if (err) {
res.json({ error: err });
}
next();
}
);
});
};
// These arr utilities
function getCCmd(language, fname) {
if (language === "c") {
return [
"/bin/sh",
"-c",
`gcc -o /project/bin/${
fname.split(".")[0]
} /project/src/${fname}`
];
} else if (language === "cpp") {
return [
"/bin/sh",
"-c",
`g++ -o /project/bin/${
fname.split(".")[0]
} /project/src/${fname}`
];
}
}
function getECmd(language, fname, iname) {
if (language === "c" || language === "cpp") {
return [
"/bin/sh",
"-c",
`/project/bin/${fname.split(".")[0]} < project/IO/${iname};`
];
}
}
function deleteFiles(filename, inputFile) {
fs.access(`./project/src/${filename}`, fs.F_OK, err => {
if (err) {
return;
}
fs.unlink(`./project/src/${filename}`, err => {
if (err) {
console.log(err);
}
});
});
fs.access(`./project/bin/${filename.split(".")[0]}`, fs.F_OK, err => {
if (err) {
return;
}
fs.unlink(`./project/bin/${filename.split(".")[0]}`, err => {
if (err) {
console.log(err);
}
});
});
fs.access(`./project/IO/${inputFile}`, fs.F_OK, err => {
if (err) {
return;
}
fs.unlink(`./project/IO/${inputFile}`, err => {
if (err) {
console.log(err);
}
});
});
}
// code compiler
const compileCode = async (req, res, next) => {
const filename = req.sourceFile,
inputFile = req.inputFile,
language = req.params.language;
let docker = new DockerMananger();
const name = await docker.createContainer();
console.log(`Container : ${name} created....`);
let compileCmd = getCCmd(language, filename);
let execCmd = getECmd(language, filename, inputFile);
let resp = await docker.execute(compileCmd);
if (resp !== "") {
docker.removeContainer();
deleteFiles(filename, inputFile);
req.ans = {
type: "Compilation error",
error: resp,
output: "CERR"
};
next();
} else {
// Check for infinite loops!
// docker.stop();
let timer = setTimeout(async () => {
try {
console.log("Process timed Out! Check for infinite loops!");
const inspectData = await docker.inspect();
if (inspectData.State.Running) {
docker.removeContainer();
deleteFiles(filename, inputFile);
console.log(
"Detected infinite loop! Killing right away...."
);
req.error = {
type: "Timeout",
error: "Time limit exceeded!",
output: "TLE"
};
next();
}
} catch (err) {
// Do Nothing
}
}, 5000);
resp = await docker.execute(execCmd);
clearTimeout(timer);
docker.removeContainer();
deleteFiles(filename, inputFile);
req.ans = {
error: "NIL",
type: "Execution Result",
output: resp
};
next();
}
};
module.exports = { createFiles, compileCode };