-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
300 lines (234 loc) · 11.2 KB
/
server.ts
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/// <reference path="typings/index.d.ts" />
// --------------------------------- IMPORT MODULES ON -----------------------------------
import express = require('express');
import path = require('path');
// --------------------------------- IMPORT MODULES OFF ----------------------------------
// --------------------------------- VARIABLES INIT ON -----------------------------------
var Datastore = require('nedb');
// Persistent datastore with automatic loading UNITS
var unitsDB = new Datastore(
{
filename: 'database/aprendaC.database',
autoload: true
}
);
// Persistent datastore with automatic loading TESTS
var testsDB = new Datastore(
{
filename: 'database/tests.database',
autoload: true
}
);
var port: number = process.env.PORT || 3000;
var app = express();
// Allow CORS - because of the two servers (lite-server and node server app)
var cors = require('cors');
app.use(cors());
// File System API
var fs = require('fs');
// Child process to run commands
const spawn = require('child_process').spawn;
// Required to parse the body content and get the parameters in the POST callbacks
var bodyParser = require('body-parser');
// --------------------------------- VARIABLES INIT OFF ----------------------------------
// ------------------------------------- ROUTE ON -----------------------------------------
app.use('/app', express.static(path.resolve(__dirname, 'app')));
app.use('/node_modules', express.static(path.resolve(__dirname, 'node_modules')));
app.use('/assets', express.static(path.resolve(__dirname, 'assets')));
app.use(express.static('user_code_folder'));
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
// GET - return systemjs.config.js
var renderSystemJs = (req: express.Request, res: express.Response) => {
res.sendFile(path.resolve(__dirname, 'systemjs.config.js'));
}
app.get('/systemjs.config.js', renderSystemJs);
// GET - return All Units
app.get('/content', function (req: express.Request, res: express.Response){
unitsDB.find({}, function (err, docs) {
// console.log('all units result: ',docs);
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(docs));
});
});
// POST - create a .c file with the user C Code and compile it into JS with emcc
// POST method is used to submits data to be processed to a specified resource
app.post('/compileCCode', function (req, res) {
var cCode: String = req.body.c_code;
console.log('lets compile this C Code: ',cCode );
fs.writeFile("user_code_folder/user_code.c", cCode, function(err) {
// Error creating and writing into .c file
if(err) {
res.send(JSON.stringify('Error on compiling C Code... (creating and writing into .c file)'));
// The return statement is needed for stop this callback and avoid continue with C Code compilation
return console.log('Error writing file: ',err);
}
console.log("The file was saved successfully!");
/**
* Complete emcc command:
* emcc -O2 --pre-js user_code_folder/module_configuration.js user_code_folder/user_code.c -o user_code_folder/user_code_compiled.js
*/
var emsc = spawn('emcc',['user_code_folder/user_code.c',
'--pre-js','user_code_folder/module_configuration.js',
/*
* TODO: Program console behavior in the textarea that represent the console
* This behavior will be in the console_behavior.js file
*
* Currently commented because the functionality is not build!!
*/
// '--post-js','user_code_folder/console_behavior.js',
'-o','user_code_folder/user_code_compiled.js']);
emsc.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
emsc.stderr.on('data', (error) => {
/* ToDo:
- Handle the emcc error and return a custom message to the user */
/**
* IF the header hasn't been sent, set the code of the header
* This condition is needed because headers can't be modified after sent
*/
if(!res.headersSent){
// Http 500 - Internal Server Error (For now)
res.writeHead(
500,
{
'Content-Type': 'application/json'
}
);
}
// Send piece of error (each line invokes this error callback function)
res.write(error);
console.log(`Error on compiling C Code with emcc, stderr: ${error}`);
console.log('Piece of error printed!================================');
});
emsc.on('close', (code) => {
// Error (piece) already sent
if(res.headersSent){
res.end();
}
else{
// Compilation completed successfully!
res.send(JSON.stringify('C Code compiled successfully, now load the JS'));
}
console.log('finish');
});
});
});
// GET - return the JS file that contains the user C Code compiled.
app.get('/user_code_folder/user_code_compiled.js', function (req, res) {
// res.send(JSON.stringify('alert test file'));
res.sendFile(path.join(__dirname, '/user_code_folder','user_code_compiled.js'));
// res.sendFile(path.join(__dirname, '/user_code_folder','alert_test.js'));
});
// GET - return the .mem file correspondent to the JS file that contains the user C Code compiled.
app.get('/user_code_compiled.js.mem', function (req, res) {
// This file is generated when optimizing the compiling process with Emscripten (-O2)
res.sendFile(path.join(__dirname, '/user_code_folder','user_code_compiled.js.mem'));
});
// GET - return the specified lesson that belongs to the specified unit
app.get('/unit/:unitId/lesson/:lessonId', function (req, res) {
var requestedUnitId = parseInt(req.params.unitId);
var requestedLessonId = parseInt(req.params.lessonId);
console.log('GET lesson with _id: ',requestedLessonId,
' from the unit with _id: ',requestedUnitId);
unitsDB.findOne({ _id: requestedUnitId, "lessons._id":requestedLessonId }, function (err, docs) {
// the findOne function returns only an object, not an array
/**
* In this case 'docs' contains the complete unit document that fits the query filter
*/
// console.log('lets see our lesson: ',docs.lessons);
res.setHeader('Content-Type', 'application/json');
// Get and return only the lesson object that fits the query filter
var requestedLesson = docs.lessons.find( lesson => lesson._id === requestedLessonId);
// console.log('the one that I want: ', requestedLesson);
res.send(JSON.stringify(requestedLesson));
});
});
// GET - return the specified Unit
app.get('/unit/:unitId', function (req, res) {
var requestedId = parseInt(req.params.unitId);
console.log('GET unit with _id 1: ',requestedId);
unitsDB.findOne({ _id: requestedId }, function (err, docs) {
// the findOne function returns only an object, not an array
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(docs));
});
});
// GET - return All Tests
app.get('/tests', function (req: express.Request, res: express.Response){
testsDB.find({}, function (err, docs) {
// console.log('all units result: ',docs);
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(docs));
});
});
// GET - return the specified Test
app.get('/test/:testId', function (req, res) {
var requestedId = parseInt(req.params.testId);
console.log('GET test with _id: ',requestedId);
testsDB.findOne({ _id: requestedId }, function (err, docs) {
// the findOne function returns only an object, not an array
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(docs));
});
});
// POST - create a .c file with the user C Code and validate its syntax
// POST method is used to submits data to be processed to a specified resource
app.post('/validateSyntax', function (req, res) {
var cCode: String = req.body.cCode;
console.log('lets validate the syntax of this C Code: ',cCode );
fs.writeFile("user_code_folder/validate_user_code.c", cCode, function(err) {
// Error creating and writing into .c file
if(err) {
res.send(JSON.stringify('Error on validating C Code... (creating and writing into .c file)'));
// The return statement is needed for stop this callback and avoid continue with C Code compilation
return console.log('Error writing file: ',err);
}
console.log("The file was saved successfully!");
var clangCommand = spawn('clang',['--analyze','user_code_folder/validate_user_code.c']);
clangCommand.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
clangCommand.stderr.on('data', (error) => {
if(!res.headersSent){
/**
* Http 200 - It has to be an OK response because if an error is sent, no more validations
* will be perform in the future
*/
res.writeHead(
200,
{
'Content-Type': 'text/html'
}
);
}
// Send piece of error (each line invokes this error callback function)
res.write(error);
console.log(`Syntax error in the C Code, stderr: ${error}`);
console.log('Piece of error printed!================================');
});
clangCommand.on('close', (code) => {
// Error (piece) already sent
if(res.headersSent){
res.end();
}
else{
// No syntax errors - Note: Do NOT change the message sent ('no syntax errors')
res.send(JSON.stringify('no syntax errors'));
}
console.log('Finish syntax validation process');
});
});
});
// GET - return the index.html file for all other requests
var renderIndex = (req: express.Request, res: express.Response) => {
res.sendFile(path.resolve(__dirname, 'index.html'));
}
app.get('/*', renderIndex);
// ------------------------------------- ROUTE OFF ---------------------------------------
var server = app.listen(port, function() {
var host = server.address().address;
var port = server.address().port;
console.log('This awesome express app is listening on: '+ host +':' + port);
});