-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·269 lines (231 loc) · 7.26 KB
/
server.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
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
#!/usr/bin/env node
var express = require('express');
var fs = require('fs');
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
var kue = require('kue');
var Job = kue.Job;
var jobs = kue.createQueue();
var hat = require('hat');
var rack = hat.rack();
var exec = require('child_process').exec;
var execFile = require('child_process').execFile;
var mongoclient = require('mongodb').MongoClient;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
/**
* Define the application.
*/
var App = function() {
// Scope.
var self = this;
/* ================================================================ */
/* Helper functions. */
/* ================================================================ */
/**
* Set up server IP address and port # using env variables/defaults.
*/
self.setupVariables = function() {
self.port = 80;
};
/**
* terminator === the termination handler
* Terminate server on receipt of the specified signal.
* @param {string} sig Signal to terminate on.
*/
self.terminator = function(sig){
if (typeof sig === "string") {
console.log('%s: Received %s - terminating app ...',
Date(Date.now()), sig);
process.exit(1);
}
console.log('%s: Node server stopped.', Date(Date.now()) );
};
/**
* Setup termination handlers (for exit and a list of signals).
*/
self.setupTerminationHandlers = function(){
// Process on exit and signals.
process.on('exit', function() { self.terminator(); });
// Removed 'SIGPIPE' from the list - bugz 852598.
['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'
].forEach(function(element, index, array) {
process.on(element, function() { self.terminator(element); });
});
};
/* ================================================================ */
/* App server functions (main app logic here). */
/* ================================================================ */
/**
* Create the routing table entries + handlers for the application.
*/
self.createRoutes = function() {
self.routes = {};
self.routes['/enqueue'] = function(req, res) {
var job = jobs.create('compileAndRun', {
id: req.body.data.id,
input: req.body.data.input,
program: req.body.data.program
});
job.save(function () {
res.send({
id: job.id
});
});
};
self.routes['/get'] = function (req, res) {
Job.get(req.query.id, function (err, job) {
if (err) return;
res.send(JSON.stringify(job));
});
};
};
/**
* Initialize the server (express) and create the routes and register
* the handlers.
*/
self.initializeServer = function() {
self.createRoutes();
self.app = express();
self.app.use(express.json()); // to support JSON-encoded bodies
self.app.use(express.urlencoded()); // to support URL-encoded bodies
self.app.use(express.compress());
// Add handlers for the app (from the routes).
for (var r in self.routes) {
self.app.all(r, self.routes[r]);
}
self.app.use(express.static(__dirname + "/site/"));
mongoclient.connect('mongodb://localhost:27017/openide', function (err, db) {
if (!err) {
self.app.get('/', function(req, res) {
var uuid;
uuid = rack();
res.writeHead(303, {location: '/openide.html?' + uuid});
res.send();
});
self.app.get('/check', function(req, res) {
db.collection('outputs', function (err, collection) {
collection.findAndRemove({
id: req.query.id
}, function(err, item) {
if (err) {
throw new Error(err);
}
if (item) {
res.send(item.output);
}
});
});
});
self.app.get('/get_doc', function (req, res) {
db.collection('programs', function (err, collection) {
collection.findOne({
id: req.query.id
}, function (err, item) {
res.send(item)
});
});
});
self.app.post('/save', function (req, res) {
db.collection('programs', function (err, collection) {
collection.findOne({
id: req.body.id
}, function (err, item) {
var orig_prev = [], new_id = rack();
if (item) {
orig_prev = item.previous;
orig_prev.push({
id: req.body.id,
time: item.time
});
} else {
new_id = req.body.id;
}
collection.insert({
id: new_id,
input: req.body.input,
program: req.body.program,
previous: orig_prev,
time: (new Date()).getTime()
}, {w:1}, function (err, result) {
res.send({
id: new_id
});
});
});
});
});
}
});
};
/**
* Initializes the application.
*/
self.initialize = function() {
self.setupVariables();
self.setupTerminationHandlers();
// Create the express server and routes.
self.initializeServer();
};
/**
* Start the server.
*/
self.start = function() {
// Start the app on the specific interface (and port).
self.app.listen(self.port);
};
};
/**
* main(): Main code.
*/
var zapp = new App();
zapp.initialize();
zapp.start();
} else {
mongoclient.connect('mongodb://localhost:27017/openide', function (err, db) {
if (!err) {
jobs.process('compileAndRun', function (job, done) {
var id = job.data.id;
var proc;
var name = '/tmp/' + id;
fs.writeFileSync('/tmp/' + id + '.cpp', job.data.program);
proc = exec('g++ -g -O2 -static -o ' + name + ' ' + name + '.cpp', function (error, stdout, stderr) {
if (error) {
done(new Error(stderr));
} else {
var run_proc;
run_proc = execFile('./run', [name], {timeout: 1500}, function (error, stdout, stderr) {
if (error) {
done(new Error(error));
}
db.collection('outputs', function (err, collection) {
var doc = {
id: job.id,
output: stdout.toString()
};
collection.insert(doc, {w:1}, function (err, result) {
if (err) {
console.log(err, result);
}
});
});
});
// Write the user's input to stdin
run_proc.stdin.write(job.data.input);
// Flush the stream by EOF
run_proc.stdin.end();
run_proc.on('close', function () {
done();
});
}
});
});
} else {
throw new Error(err);
}
});
}