forked from mozilla/firehug
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
365 lines (314 loc) · 8.83 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
/*
Get Libs
*/
var express = require( 'express' );
var morgan = require( 'morgan' );
var helmet = require( 'helmet' );
var moment = require( 'moment' );
var marked = require( 'marked' );
var lodash = require( 'lodash' );
var later = require( 'later' );
var fs = require( 'fs' );
var nunjucks = require( 'nunjucks' );
var sessions = require( './lib/sessions' );
var documents = require( './lib/documents' );
var shared = require( './shared' );
var env = shared.env;
var debug = shared.debug( 'env' );
var serverStartTime = moment();
/*
Start Recurring Jobs
*/
var jobs = require( './bin' );
// var jobStartTime = moment();
/*
Server Setup
*/
var app = express();
app.use( express.static( __dirname + '/public' ) );
app.use( helmet.xframe( 'sameorigin' ) );
app.use( helmet.hsts() );
app.use( helmet.nosniff() );
app.use( helmet.xssFilter() );
if( shared.debug( 'http' ).enabled ) {
debug( 'using morgan for \033[0;37mhttp\033[0m debug notices' );
app.use( morgan( ' \033[0;37mhttp\033[0m :method :url :status +:response-time ms - :res[content-length] bytes' ) );
}
app.disable( 'x-powered-by' );
// pretty print json
app.set( 'json spaces', 2 );
/**
* @todo proper CSP
*
* Should allow for x-ray goggles
*/
// Content Security Policy
// app.use( helmet.csp( {
// defaultSrc: [ '\'self\'' ],
// reportUri: '/report-violation',
// reportOnly: true
// } ) );
// No caching api routes pl0x
app.all( [ '/healthcheck', '/api/*' ], function( req, res, next ) {
res.set({
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
});
return next();
});
// add env to res.locals
app.use( function( req, res, next ) {
res.locals.env = env.get();
next();
});
/*
Generates a webapp manifest
Uses the defaults in /manifest.webapp and extends them with any
environment set details.
*/
debug( 'generating webapp manifest file' );
var webappManifest = fs.readFileSync( 'manifest.webapp', 'utf8' );
webappManifest = JSON.parse( webappManifest );
var webappManifestOverrides = env.get( 'WEBAPP_MANIFEST' ) || '{}';
webappManifestOverrides = JSON.parse( webappManifestOverrides );
webappManifest = lodash.extend( webappManifest, webappManifestOverrides );
debug( '↳ completed generation of webapp manifest' );
// add to res.locals
app.use( function( req, res, next ) {
res.locals.app = JSON.parse( JSON.stringify( webappManifest ) ); // use as default values
res.locals.app.webappManifest = webappManifest; // unchanging
next();
});
// add build time to res.locals
app.use( function( req, res, next ) {
res.locals.app.serverStartTime = serverStartTime.valueOf();
next();
});
/*
Setup Nunjucks
*/
var nunjucksEnv = nunjucks.configure( 'views', {
autoescape: true
});
// add markdown parser to nunjucks
nunjucksEnv.addFilter( 'marked', marked );
// add nunjucks to res.render
nunjucksEnv.express( app );
/*
Healthcheck
*/
app.get( '/healthcheck', function( req, res ) {
res.jsonp({
version: require( './package' ).version,
http: 'okay',
jobs: jobs.getStatus()
});
});
/*
Routes
*/
app.get( '/', function( req, res ) {
res.render( 'index.html', {
timezone: env.get( 'EVENT_TIMEZONE' )
});
});
/**
* @todo render a page showing server time, time of next poll, and time remaining (tick)
*/
app.get( '/time', function( req, res ) {
var schedule = later.parse.cron( env.get( 'JOB_SCHEDULE' ) );
res.render( 'time.html', {
serverTime: moment().toISOString(),
laterTime: later.schedule( schedule ).next( 1 ),
laterCron: env.get( 'JOB_SCHEDULE' )
});
});
/**
* Serves the webapp manifest file
*/
app.get( '/manifest.webapp', function( req, res ) {
res.type( 'application/x-web-app-manifest+json' );
res.send( JSON.stringify( webappManifest ) );
});
/**
* @todo generate dynamically using `fs`
* @todo concat core vendor packages into a vendor.js file for easy caching
*/
app.get( '/firehug.appcache', function( req, res ) {
var caches = [];
res.contentType( 'text/cache-manifest' );
// stylesheets
caches = caches.concat([
'/core/css/core.min.css',
'/theme/css/main.min.css'
]);
// fonts (font-awesome)
// fs.readdirSync( __dirname + '/public/vendor/font-awesome/fonts' ).filter( function( file ) {
// // return true IF not a dotfile AND not this file
// return ( file.indexOf( '.' ) !== 0 );
// }).forEach( function( file ) {
// caches.push( '/vendor/font-awesome/fonts/' + file );
// });
caches = caches.concat([
'/vendor/font-awesome/fonts/fontawesome-webfont.eot?v=4.2.0',
'/vendor/font-awesome/fonts/fontawesome-webfont.eot?#iefix&v=4.2.0',
'/vendor/font-awesome/fonts/fontawesome-webfont.woff?v=4.2.0',
'/vendor/font-awesome/fonts/fontawesome-webfont.ttf?v=4.2.0',
'/vendor/font-awesome/fonts/fontawesome-webfont.svg?v=4.2.0#fontawesomeregular',
]);
// javascript
caches = caches.concat([
'/vendor/jquery/dist/jquery.min.js',
'/vendor/moment/min/moment.min.js',
'/vendor/moment-timezone/builds/moment-timezone-with-data.min.js',
'/vendor/marked/lib/marked.js',
'/vendor/routie/dist/routie.min.js',
'/vendor/nunjucks/browser/nunjucks-slim.min.js',
'/theme/partials.js',
'/core/js/core.min.js',
'/theme/js/app.min.js',
'/vendor/bootstrap/js/dropdown.js'
]);
// imgs + maps
caches = caches.concat([
'/theme/imgs/maps/floor_0.svg',
'/theme/imgs/maps/floor_1.svg',
'/theme/imgs/maps/floor_2.svg',
'/theme/imgs/maps/floor_3.svg',
'/theme/imgs/maps/floor_4.svg',
'/theme/imgs/maps/floor_5.svg',
'/theme/imgs/maps/floor_6.svg',
'/theme/imgs/maps/floor_7.svg',
'/theme/imgs/maps/floor_8.svg',
'/theme/imgs/maps/floor_9.svg',
'/theme/imgs/maps/floor_m.svg',
'/theme/imgs/logo.png'
]);
// send back manifest
res.send( 'CACHE MANIFEST\n# Created ' + serverStartTime.format() + '\n\n' + caches.join( '\n' ) + '\n\nNETWORK:\n*' );
});
/*
API Routes
*/
/**
* Get a specific session by its id. This should
* the "sid" column in the spreadsheet.
*/
app.get( '/api/session/:id', function( req, res, next ) {
sessions.getSessions( function( err, sessions ) {
if( err ) {
console.error( err );
return next();
}
// variable to hold the session info + return
var session = {};
// dumb find id in sessions
for( var idx = 0, len = sessions.length; idx < len; idx++ ) {
if( sessions[ idx ].id === req.params.id ) {
session = sessions[ idx ];
break;
}
}
// check we have a result before response
if( lodash.isEmpty( session ) ) {
return next();
}
res.jsonp( session );
});
});
/**
* Get all sessions, in a given theme if provided.
*/
app.get( '/api/sessions/:theme?', function( req, res, next ) {
if( req.params.theme ) {
return sessions.getSessions( req.params.theme, function( err, sessions ) {
if( err ) {
console.error( err );
return next();
}
res.jsonp( sessions );
});
}
sessions.getSessions( function( err, sessions ) {
if( err ) {
console.error( err );
return next();
}
res.jsonp( sessions );
});
});
/**
* Get all themes + descriptions
*/
app.get( '/api/themes', function( req, res ) {
res.jsonp( sessions.getThemes() );
});
/**
* Get a specific document, and parse assuming the format if provided.
*
* All documents are stored as plain text.
*
* Valid formats to parse as:
* * html
* * markdown
*/
app.get( '/api/doc/:name/:format?', function( req, res, next ) {
// check doc exists
if( documents.getDocNames().indexOf( req.params.name ) === -1 ) {
return next();
}
documents.getDoc( req.params.name, function( err, doc ) {
if( err ) {
console.error( err );
return next();
}
switch( req.params.format ) {
case 'html':
res.type( 'text/html' );
break;
case 'markdown':
case 'md':
res.type( 'text/html' );
doc = marked( doc );
break;
default:
res.type( 'text/plain' );
break;
}
res.send( doc );
});
});
/**
* Get a listing of all documents available and the route to
* access them (as plain text).
*/
app.get( '/api/docs', function( req, res ) {
var docNames = documents.getDocNames();
var docs = [];
docNames.forEach( function( docName ) {
docs.push({
name: docName,
link: '/api/doc/' + docName
});
});
res.jsonp( docs );
});
/*
Some nice redirects for app
*/
// tag shortlink
app.get( '/t/:tag', function( req, res ) {
res.redirect( '/#tag/' + req.params.tag );
});
// session short link
app.get( '/s/:sessionId', function( req, res ) {
res.redirect( '/#session/' + req.params.sessionId );
});
// map short link
app.get( '/m/:locationId', function( req, res ) {
res.redirect( '/#map/' + req.params.locationId );
});
var server = app.listen( env.get( 'PORT' ) || 5000, function() {
console.log( 'Now listening on port %d', server.address().port );
});