hprasath commited on
Commit
93d296d
·
verified ·
1 Parent(s): 3bd94a1

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +38 -8
index.js CHANGED
@@ -16,7 +16,6 @@ app.get("/", function (req, res) {
16
  res.send("Hello World!!");
17
  });
18
 
19
-
20
  app.post("/compile", function (req, res) {
21
  var code = req.body.code;
22
  var input = req.body.input;
@@ -43,7 +42,7 @@ app.post("/compile", function (req, res) {
43
  }
44
  });
45
  }
46
- }else if(lang == "Java"){
47
  fs.writeFile("/code/temp/Main.java", code, (err) => {
48
  if (err) {
49
  console.error(err);
@@ -55,21 +54,36 @@ app.post("/compile", function (req, res) {
55
  (error, stdout, stderr) => {
56
  if (error) {
57
  console.error(error);
58
- res.send({error:stderr});
59
  return;
60
  }
61
  console.log(stdout);
62
  console.error(stderr);
63
- res.send({output:stdout});
64
  }
65
  );
66
  if (input) {
67
  javaProcess.stdin.write(input);
68
  javaProcess.stdin.end();
69
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  });
71
- }
72
- else if (lang == "Cpp") {
73
  fs.writeFile("/code/temp/Main.cpp", code, (err) => {
74
  if (err) {
75
  console.error(err);
@@ -93,14 +107,30 @@ app.post("/compile", function (req, res) {
93
  cppProcess.stdin.write(input);
94
  cppProcess.stdin.end();
95
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  });
97
  }
98
  } catch (e) {
99
  console.log("error:" + e);
100
  }
101
- });
102
 
103
  const port = 7860;
104
  app.listen(port, () => {
105
  console.log(`Open http://localhost:${port}`);
106
- });
 
16
  res.send("Hello World!!");
17
  });
18
 
 
19
  app.post("/compile", function (req, res) {
20
  var code = req.body.code;
21
  var input = req.body.input;
 
42
  }
43
  });
44
  }
45
+ } else if (lang == "Java") {
46
  fs.writeFile("/code/temp/Main.java", code, (err) => {
47
  if (err) {
48
  console.error(err);
 
54
  (error, stdout, stderr) => {
55
  if (error) {
56
  console.error(error);
57
+ res.send({ error: stderr });
58
  return;
59
  }
60
  console.log(stdout);
61
  console.error(stderr);
62
+ res.send({ output: stdout });
63
  }
64
  );
65
  if (input) {
66
  javaProcess.stdin.write(input);
67
  javaProcess.stdin.end();
68
  }
69
+
70
+ // Timeout functionality
71
+ const timeout = 5000; // 5 seconds timeout
72
+ let isTimeout = false;
73
+ const timeoutId = setTimeout(() => {
74
+ isTimeout = true;
75
+ javaProcess.kill(); // Kill the process if it exceeds the timeout
76
+ res.status(408).send({ error: "Time Limit Exceeded" });
77
+ }, timeout);
78
+
79
+ javaProcess.on("exit", () => {
80
+ clearTimeout(timeoutId); // Clear the timeout if the process exits before the timeout
81
+ if (!isTimeout) {
82
+ res.send({ output: stdout });
83
+ }
84
+ });
85
  });
86
+ } else if (lang == "Cpp") {
 
87
  fs.writeFile("/code/temp/Main.cpp", code, (err) => {
88
  if (err) {
89
  console.error(err);
 
107
  cppProcess.stdin.write(input);
108
  cppProcess.stdin.end();
109
  }
110
+
111
+ // Timeout functionality
112
+ const timeout = 5000; // 5 seconds timeout
113
+ let isTimeout = false;
114
+ const timeoutId = setTimeout(() => {
115
+ isTimeout = true;
116
+ cppProcess.kill(); // Kill the process if it exceeds the timeout
117
+ res.status(408).send({ error: "Time Limit Exceeded" });
118
+ }, timeout);
119
+
120
+ cppProcess.on("exit", () => {
121
+ clearTimeout(timeoutId); // Clear the timeout if the process exits before the timeout
122
+ if (!isTimeout) {
123
+ res.send({ output: stdout });
124
+ }
125
+ });
126
  });
127
  }
128
  } catch (e) {
129
  console.log("error:" + e);
130
  }
131
+ });
132
 
133
  const port = 7860;
134
  app.listen(port, () => {
135
  console.log(`Open http://localhost:${port}`);
136
+ });