-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
148 lines (139 loc) · 4.33 KB
/
api.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
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
const express = require('express');
const cors = require('cors');
const app = express();
const bodyP = require('body-parser');
const compiler = require('compilex');
const options = {
stats: true
};
const fs = require('fs');
compiler.init(options);
app.use(cors());
app.use(bodyP.json());
app.use(
'/codemirror-5.65.17', express.static('G:/CodeFlex/codemirror-5.65.17'));
app.get('/', function(req, res) {
compiler.flush(function() {
console.log('deleted');
});
res.sendFile('G:/CodeFlex/index.html');
});
app.post('/compile', function(req, res) {
var code = req.body.code;
var input = req.body.input;
var lang = req.body.lang;
let processCompleted = false; // Flag to ensure only one response is sent
try {
if (lang === 'Cpp') {
var envData = {OS: 'windows', cmd: 'g++', options: {timeout: 10000}};
if (!input) {
compiler.compileCPP(envData, code, function(data) {
if (processCompleted) return; // Prevent sending response twice
if (data.output) {
res.send(data);
} else {
res.send({output: 'error'});
}
processCompleted = true;
});
} else {
compiler.compileCPPWithInput(envData, code, input, function(data) {
if (processCompleted) return; // Prevent sending response twice
if (data.output) {
res.send(data);
} else {
res.send({output: 'error'});
}
processCompleted = true;
});
}
} else if (lang === 'Java') {
var envData = {OS: 'windows'};
if (!input) {
compiler.compileJava(envData, code, function(data) {
if (processCompleted) return; // Prevent sending response twice
if (data.output) {
res.send(data);
} else {
res.send({output: 'error'});
}
processCompleted = true;
});
} else {
compiler.compileJavaWithInput(envData, code, input, function(data) {
if (processCompleted) return; // Prevent sending response twice
if (data.output) {
res.send(data);
} else {
res.send({output: 'error'});
}
processCompleted = true;
});
}
} else if (lang === 'Python') {
var envData = {OS: 'windows'};
if (!input) {
compiler.compilePython(envData, code, function(data) {
if (processCompleted) return; // Prevent sending response twice
if (data.output) {
res.send(data);
} else {
res.send({output: 'error'});
}
processCompleted = true;
});
} else {
compiler.compilePythonWithInput(envData, code, input, function(data) {
if (processCompleted) return; // Prevent sending response twice
if (data.output) {
res.send(data);
} else {
res.send({output: 'error'});
}
processCompleted = true;
});
}
}
} catch (e) {
if (processCompleted) return; // Prevent sending response twice if an error occurs
console.log('Error' + e.message);
res.status(500).send({ error: 'Internal Server Error' });
}
});
// For Cpp
app.get('/template/cpp', function(req, res) {
fs.readFile(
'G:/CodeFlex/templates/cpp_template.txt', 'utf8', function(err, data) {
if (err) {
res.status(500).send('Template not found');
} else {
res.send({template: data});
}
});
});
// For Java
app.get('/template/java', function(req, res) {
fs.readFile(
'G:/CodeFlex/templates/java_template.txt', 'utf8', function(err, data) {
if (err) {
res.status(500).send('Template not found');
} else {
res.send({template: data});
}
});
});
// For Python
app.get('/template/python', function(req, res) {
fs.readFile(
'G:/CodeFlex/templates/python_template.txt', 'utf8', function(err, data) {
if (err) {
res.status(500).send('Template not found');
} else {
res.send({template: data});
}
});
});
const PORT = 8000;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});