Spaces:
Sleeping
Sleeping
Update index.js
Browse files
index.js
CHANGED
|
@@ -45,61 +45,79 @@ app.post("/compile", function (req, res) {
|
|
| 45 |
});
|
| 46 |
}
|
| 47 |
}else if(lang == "Java"){
|
| 48 |
-
|
| 49 |
-
if (err) {
|
| 50 |
-
console.error(err);
|
| 51 |
-
res.status(500).send("Error writing Java file");
|
| 52 |
-
return;
|
| 53 |
-
}
|
| 54 |
-
const javaProcess = exec(
|
| 55 |
-
"javac -d /code/temp /code/temp/Main.java && java -cp /code/temp Main",
|
| 56 |
-
{ timeout: TIMEOUT_VALUE }, // Add timeout option
|
| 57 |
-
(error, stdout, stderr) => {
|
| 58 |
-
if (error) {
|
| 59 |
-
console.error(error);
|
| 60 |
-
res.send({error: "Time Limit Exceeded!!"});
|
| 61 |
-
return;
|
| 62 |
-
}
|
| 63 |
-
console.error(stderr);
|
| 64 |
-
res.send({output:stdout});
|
| 65 |
-
}
|
| 66 |
-
);
|
| 67 |
-
if (input) {
|
| 68 |
-
javaProcess.stdin.write(input);
|
| 69 |
-
javaProcess.stdin.end();
|
| 70 |
-
}
|
| 71 |
-
});
|
| 72 |
}
|
| 73 |
else if (lang == "Cpp") {
|
| 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 |
const port = 7860;
|
| 105 |
app.listen(port, () => {
|
|
|
|
| 45 |
});
|
| 46 |
}
|
| 47 |
}else if(lang == "Java"){
|
| 48 |
+
compileJava(code, input, res);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
}
|
| 50 |
else if (lang == "Cpp") {
|
| 51 |
+
compileCpp(code, input, res);
|
| 52 |
+
} catch (e) {
|
| 53 |
+
console.log("error:" + e);
|
| 54 |
+
}
|
| 55 |
+
});
|
| 56 |
+
|
| 57 |
+
const compileJava = (code, input, res) => {
|
| 58 |
+
fs.writeFile("/code/temp/Main.java", code, (err) => {
|
| 59 |
+
if (err) {
|
| 60 |
+
console.error(err);
|
| 61 |
+
res.status(500).send("Error writing Java file");
|
| 62 |
+
return;
|
| 63 |
+
}
|
| 64 |
+
const javaProcess = exec(
|
| 65 |
+
"javac -d /code/temp /code/temp/Main.java && java -cp /code/temp Main",
|
| 66 |
+
{ timeout: TIMEOUT_VALUE }, // Add timeout option
|
| 67 |
+
(error, stdout, stderr) => {
|
| 68 |
+
if (error) {
|
| 69 |
+
if (error.killed) {
|
| 70 |
+
console.error("Time Limit Exceeded!!");
|
| 71 |
+
res.send({ error: "Time Limit Exceeded!!" });
|
| 72 |
+
} else {
|
| 73 |
+
console.error(stderr);
|
| 74 |
+
res.send({ error: stderr });
|
| 75 |
}
|
| 76 |
+
return;
|
| 77 |
+
}
|
| 78 |
+
console.log(stdout);
|
| 79 |
+
res.send({ output: stdout });
|
| 80 |
+
}
|
| 81 |
+
);
|
| 82 |
+
if (input) {
|
| 83 |
+
javaProcess.stdin.write(input);
|
| 84 |
+
javaProcess.stdin.end();
|
| 85 |
+
}
|
| 86 |
+
});
|
| 87 |
+
};
|
| 88 |
+
|
| 89 |
+
const compileCpp = (code, input, res) => {
|
| 90 |
+
fs.writeFile("/code/temp/Main.cpp", code, (err) => {
|
| 91 |
+
if (err) {
|
| 92 |
+
console.error(err);
|
| 93 |
+
res.status(500).send("Error writing C++ file");
|
| 94 |
+
return;
|
| 95 |
+
}
|
| 96 |
+
const cppProcess = exec(
|
| 97 |
+
"g++ -o /code/temp/Main /code/temp/Main.cpp && /code/temp/Main",
|
| 98 |
+
{ timeout: TIMEOUT_VALUE }, // Add timeout option
|
| 99 |
+
(error, stdout, stderr) => {
|
| 100 |
+
if (error) {
|
| 101 |
+
if (error.killed) {
|
| 102 |
+
console.error("Time Limit Exceeded!!");
|
| 103 |
+
res.send({ error: "Time Limit Exceeded!!" });
|
| 104 |
+
} else {
|
| 105 |
+
console.error(stderr);
|
| 106 |
+
res.send({ error: stderr });
|
| 107 |
}
|
| 108 |
+
return;
|
| 109 |
+
}
|
| 110 |
+
console.log(stdout);
|
| 111 |
+
res.send({ output: stdout });
|
| 112 |
}
|
| 113 |
+
);
|
| 114 |
+
if (input) {
|
| 115 |
+
cppProcess.stdin.write(input);
|
| 116 |
+
cppProcess.stdin.end();
|
| 117 |
}
|
| 118 |
});
|
| 119 |
+
};
|
| 120 |
+
|
| 121 |
|
| 122 |
const port = 7860;
|
| 123 |
app.listen(port, () => {
|