-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
494 lines (465 loc) · 17.1 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#!/bin/env node
var express = require('express');
var fs = require('fs');
var _ = require('underscore')._;
var assert = require('assert');
var Handlebars = require('handlebars');
//var ref = require('json-ref');
Handlebars.registerHelper('stringify', function(object) {
try{
return JSON.stringify((_.extend({}, object)), 2, 4);
}catch(e) {
//console.log(JSON.stringify(ref.ref(_.extend({}, object)), 2, 4));
throw e;
}
});
//Database:
var mongo = require('mongoskin');
var db;
//For connecting to github api:
var https = require('https');
var pipette = require('pipette');
/*
I'm considering using node-github for this purpose instead.
However, it might be nice to make the gh api code node.js independent so it can be used in browser widgets as well.
var GitHubApi = require("github");
*/
var config = require('./config');
var EarleyParser = require('./earley');
var utils = require('./utils');
// Local cache for static content [fixed and loaded at startup]
var zcache = {};
zcache.indexTemplate = Handlebars.compile(fs.readFileSync('templates/index.html', 'utf8'));
zcache.resultsTemplate = Handlebars.compile(fs.readFileSync('templates/results.html', 'utf8'));
zcache.parseChartTemplate = Handlebars.compile(fs.readFileSync('templates/parseChart.html', 'utf8'));
zcache.nodeInfoTemplate = Handlebars.compile(fs.readFileSync('templates/langNode.html', 'utf8'));
zcache.defaultWidget = fs.readFileSync('defaultWidget.html', 'utf8');
// Create "express" server.
//TODO: Upgrade on OpenShift
var app = express.createServer();
app.configure(function(){
//TODO: I'll probably need to use a storage provider for sessions.
//sessions stuff should come first.
app.use(express.cookieParser()),
app.use(express.session({ secret: "keyboard cat" }));
//This must come before router
app.use(express.bodyParser());
app.use(app.router);
//app.use(express.static('./ace-builds'));
app.use(express.static('./static'));
//error handling should come last
app.use(express.errorHandler({showStack: true, dumpExceptions: true}));
});
/* ===================================================================== */
/* Setup route handlers. */
/* ===================================================================== */
// Handler for GET /health
app.get('/health', function(req, res){
res.send('1');
});
if(config.debug){
//I want most debug functions to be public,
//however I'm going to draw a line for now at dumping the entire database.
app.get('/dbg', function(req, res, next) {
db.collection('langNodes').find().toArray(function(err, items){
if(err) {
next(err);
return;
}
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(JSON.stringify(items));
});
return;
});
app.get('/dbgf', function(req, res, next) {
db.collection('files').find().toArray(function(err, items){
if(err) {
next(err);
return;
}
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(JSON.stringify(items));
});
return;
});
}
app.get('/interpretations/:id', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
db.collection('interpretations').findById(req.params.id, function(err, interpretation){
if(err) {
next(err);
return;
}
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(interpretation));
});
return;
});
/**
* It might be desirable to have widget html served from this server for a few reasons:
* -Widgets can be server-side templates.
* -Gists can't be hosted on gh-pages, and I want them to be very easy to use.
* -There could be some origin issues.
**/
app.get('/pages/:id', function(req, res, next) {
db.collection('files').findById(req.params.id, function(err, langNodeFiles){
if(err) {
next(err);
return;
}
if(langNodeFiles && langNodeFiles.files && langNodeFiles.files['index.html']) {
//res.writeHead(200, {'Content-Type': 'text/plain'});
//TODO: Generalize this so we can serve the full gist.
res.send(langNodeFiles.files['index.html'].content);
} else {
res.send(zcache.defaultWidget);
}
});
return;
});
function renderChart(input, chart){
//TODO: Provide indicator of when colspan is 0.
return zcache.parseChartTemplate({
columns : _.map(chart, function(col, colIdx) {
return _.map(col, function(langNode){
return _.extend(langNode, {
colspan: colIdx - langNode.parseData.origin,
complete: langNode.parseData.atComponent >= langNode.components.length,
origin: langNode.parseData.origin
});
});
}),
symbols: input.split('')
});
}
app.get('/category/:category', function(req, res, next){
//TODO: Check that the category exists.
var renderedTemplate;
var category = req.params.category;
if('q' in req.query){
var queryId = new mongo.ObjectID();
db.collection('queries').insert({
'_id': queryId,
'query': req.query.q,
'category': category,
'timestamp': String(new Date())
},
{ safe: true },
function(err, result){
if(err) {
next(err);
return;
}
});
EarleyParser.parse(req.query.q, category, db.collection('langNodes'), function(err, chart){
var interpretations = [];
var gammaNode;
if(err){
next(err);
return;
}
try{
if('chart' in req.query){
res.send(renderChart(req.query.q, chart));
} else if( 'json' in req.query) {
res.send('<pre>'+JSON.stringify(utils.deprototype(_.find(chart[chart.length - 1], function(x){return x.category === "GAMMA";})), 2, 4)+'</pre>');
// res.send('<pre>'+JSON.stringify(EarleyParser.chartToInterpretations(chart), 2, 4)+'</pre>');
} else {
//interpretations = EarleyParser.chartToInterpretations(chart);
gammaNode = _.find(chart[chart.length - 1], function(x) {
return x.category === "GAMMA";
});
if(gammaNode){
interpretations = _.map(gammaNode.interpretations, function(conponents){
//Gamma node interpretations all have one component which corresponds to the queried category langNode
var categoryNode = conponents[0];
return {
'_id': new mongo.ObjectID(),
'root': categoryNode,
'queryId': queryId
};
});
}
_.defer(function(){
//This is deferred so it can happen whilst the interpretations are inserted.
renderedTemplate = zcache.resultsTemplate({
'interpretations': interpretations,
'query': req.query.q,
'category': category,
'serverUrl': encodeURIComponent(config.serverUrl)
});
});
if(interpretations.length > 0){
db.collection('interpretations').insert(interpretations, {
safe: true
}, function(err, result) {
if(err) {
next(err);
return;
}
res.send(renderedTemplate);
});
} else {
_.defer(function(){
//This is deferred so it happens after the template is rendered.
res.send(renderedTemplate);
});
}
}
} catch(e) {
next(e);
}
});
} else {
renderedTemplate = zcache.indexTemplate({'category' : category});
res.send(renderedTemplate);
}
});
app.get('/', function(req, res, next){
var renderedTemplate;
var category = 'main';
/*
http://www.garann.com/dev/2011/calling-the-github-api-with-node-js/
console.log(req.session);
if(!req.session.blah){
req.session.blah = 1;
}
req.session.destroy(function(err){
if(err) {
next(err);
return;
}
});
*/
renderedTemplate = zcache.indexTemplate({'category' : category});
res.send(renderedTemplate);
});
app.get('/upvote/:id', function(req, res){
res.send("Not yet implemented");
});
app.get('/downvote/:id', function(req, res){
res.send("Not yet implemented");
});
function fetchRepo(repository, callback) {
if (repository.type === 'gist') {
https.request({
host: 'api.github.com',
port: 443,
path: '/gists/' + repository.gistId,
method: 'GET'
}, function(gitres) {
var gitdata = new pipette.Sink(gitres);
gitdata.on('data', function(data) {
var parsedData;
try{
parsedData = JSON.parse(data.toString());
} catch(err) {
callback(err);
return;
}
callback(null, parsedData);
});
}).on('error', callback).end();
}
else if (repository.type === 'github') {
//see:
//http://developer.github.com/v3/repos/contents/
}
else {
callback("Unknown repo type");
}
}
function syncNode(query, content, callback) {
db.collection('langNodes').findAndModify(query, [['_id', 'descending']], {
$set: {
lastSync: String(new Date()),
content: content
}
}, {
'upsert': true,
'new': true,
'safe': true //Not sure if this is needed for findAndModify, it checks for success
},
function(err, result) {
callback(err, result);
});
}
/**
* Return info on a language node and sync it with its repo
**/
app.get('/langNode/:id', function(req, res, next) {
var id = req.params.id;
db.collection('langNodes').findById(id, function(err, langNode) {
var renderedTemplate;
if (err) {
res.send('Error: ' + err);
return;
}
if (langNode === null) {
res.send('Could not find langNode with id ' + String(id));
return;
}
fetchRepo(langNode.repository, function(err, repositoryData) {
var content, lastGistCommitDate, lastSyncDate;
if (err) {
next(err);
return;
}
lastGistCommitDate = new Date(repositoryData.history[0].committed_at);
lastSyncDate = langNode.lastSync ? new Date(langNode.lastSync) : new Date(0);
if (lastGistCommitDate > lastSyncDate) {
if('languageNode.json' in repositoryData.files) {
try{
content = JSON.parse(repositoryData.files['languageNode.json'].content);
} catch(err) {
res.send('Cound not parse languageNode.json.\nError: ' + err);
return;
}
} else {
res.send('Error: Repository does not have languageNode.json file.');
return;
}
//content.files = repositoryData.files;
syncNode({
_id: langNode._id
}, content, function(err, syncedNode) {
if(err) {
next(err);
return;
}
//Files are kept separately so langNode docs are smaller.
//Eventually, I would like to use ghpages
db.collection('files').update({
_id: syncedNode._id
}, {
_id: syncedNode._id,
files: repositoryData.files
}, {
upsert: true,
safe: true
}, function() {});
renderedTemplate = zcache.nodeInfoTemplate({
syncedNode: syncedNode,
repositoryData: repositoryData,
previousSyncDate: lastSyncDate
});
res.send(renderedTemplate);
});
}
else {
renderedTemplate = zcache.nodeInfoTemplate({
repositoryData: repositoryData,
previousSyncDate: lastSyncDate
});
res.send(renderedTemplate);
}
});
});
});
//TODO: Make way of submitting JSON
//TODO: Emphasize upsert functionality in documentation
app.post('/submit', function(req, res) {
var repository = {
type: 'gist',
gistId: req.body.gistId
};
fetchRepo(repository, function(err, repositoryData) {
var content;
if (err) {
res.send('Error: ' + err);
return;
}
//TODO: Think about this. Duck typing might be more future proof.
//Also, I want to be sure the repository was deleted.
if(repositoryData.message == "Not Found") {
//My approach to deleting is kinda hacky.
//The entries are still in the database.
content = null;
} else {
try{
content = JSON.parse(repositoryData.files['languageNode.json'].content);
} catch(err) {
res.send('Error: ' + err);
return;
}
//content.files = repositoryData.files;
}
syncNode({
repository: repository
}, content, function(err, syncedNode) {
if (err) {
res.send('Error: ' + err);
return;
}
db.collection('files').update({
_id: syncedNode._id
}, {
_id: syncedNode._id,
files: repositoryData.files
}, {
upsert: true,
safe: true
}, function(err) {
if (err) {
res.send('Error: ' + err);
return;
}
//TODO: Redirect to langNode url (this could trigger syncing, and the page could indicate progress.)
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(syncedNode, 2, 4));
});
});
});
});
// Get the environment variables we need.
var ipaddr, port;
if(config.openshift){
ipaddr = process.env.OPENSHIFT_INTERNAL_IP;
port = process.env.OPENSHIFT_INTERNAL_PORT || 8080;
} else {
ipaddr = process.env.IP;
port = process.env.PORT;
}
if (typeof ipaddr === "undefined") {
console.warn('No OPENSHIFT_INTERNAL_IP environment variable');
}
// terminator === the termination handler.
function terminator(sig) {
if (typeof sig === "string") {
console.log('%s: Received %s - terminating Node server ...',
Date(Date.now()), sig);
process.exit(1);
}
console.log('%s: Node server stopped.', Date(Date.now()) );
}
// Process on exit and signals.
process.on('exit', function() { terminator(); });
['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT', 'SIGBUS',
'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGPIPE', 'SIGTERM'
].forEach(function(element, index, array) {
process.on(element, function() { terminator(element); });
});
//Question: What is the ideal place to connect to the db
app.listen(port, ipaddr, function() {
var reinit = false;
db = mongo.db(config.databaseUrl);
db.createCollection('files', function(err, collection) {});
db.createCollection('interpretations', function(err, collection) {});
db.createCollection('queries', function(err, collection) {});
db.createCollection('langNodes', function(err, collection) {
if(reinit){
db.collection('langNodes').remove(function() {
db.collection('langNodes').insert(langNodes.testNodes, {
upsert: true,
safe: true
},
function(err, result) {
assert.equal(null, err);
});
console.log('%s: Node server started on %s:%d ...', Date(Date.now()), ipaddr, port);
});
} else {
console.log('%s: Node server started on %s:%d ...', Date(Date.now()), ipaddr, port);
}
});
});