-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensei.js
482 lines (384 loc) · 17.2 KB
/
sensei.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
var express = require('express')
, expressLayouts = require('express-ejs-layouts')
, expressSession = require('express-session')
, bodyParser = require('body-parser')
, waterline = require('waterline')
, logger = require('morgan')
, async = require('async')
, lodash = require('lodash')
, fs = require('fs')
, path = require('path')
, orm = new waterline();
// Setup global app
global.sensei = {}
global.async = async;
global._ = lodash;
sensei.app = express();
module.exports = {
// Base defaults are null until `configure` is called
defaults : null,
/*
* Configure the application by applying options
* @param {Object} options, mapping to overwrite defaults
* @return void
*/
configure : function( options ) {
// Setup defaults
var defaults = {
globals : [],
paths : {},
cors : true,
keepAlive : 10000,
ssl : {
key : null,
cert : null,
ca : null
},
session : {
name : 'senseid',
secret : '7cdfb2ba6f5f67e8ce99c96c567d612f',
resave: false,
saveUninitialized: false,
cookie : {
maxAge : 1000 * 86400
}
}
}
// Configure application using defaults
sensei.paths = {};
sensei.managers = {};
sensei.entities = {};
sensei.services = {};
sensei.policies = {};
sensei.components = {};
sensei.paths.root = __dirname;
sensei.paths.views = defaults.paths.views || path.join(sensei.paths.root, 'interface', 'views');
sensei.paths.routes = defaults.paths.routes || path.join(sensei.paths.root, 'interface', 'routes');
sensei.paths.assets = defaults.paths.assets || path.join(sensei.paths.root, 'interface', 'assets');
sensei.paths.policies = defaults.paths.policies || path.join(sensei.paths.root, 'interface', 'policies');
sensei.paths.entities = defaults.paths.entities || path.join(sensei.paths.root, 'infrastructure', 'entities');
sensei.paths.layout = defaults.paths.layout || path.join(sensei.paths.root, 'interface', 'views', 'layouts', 'default');
sensei.paths.managers = defaults.paths.managers || path.join(sensei.paths.root, 'infrastructure', 'managers');
sensei.paths.components = defaults.paths.components || path.join(sensei.paths.root, 'infrastructure', 'components');
sensei.paths.services = defaults.paths.services || path.join(sensei.paths.root, 'interface', 'services');
sensei.routes = defaults.routes || path.join(sensei.paths.root, 'config', 'interface', 'routes');
sensei.adapters = path.join(sensei.paths.root, 'config', 'infrastructure','adapters');
sensei.cors = typeof defaults.cors == 'undefined' ? true : defaults.cors;
sensei.ssl = defaults.ssl || null;
sensei.session = defaults.session;
sensei.cleanup = defaults.cleanup || function noop() {};
sensei.port = defaults.port || 3000;
sensei.keepAlive = defaults.keepAlive || 10000;
// Merge options over defaults
sensei = _.defaultsDeep(options, sensei);
// Configure express app server
sensei.app.use(logger('dev'));
sensei.app.use(expressSession(sensei.session));
// static asset loader
sensei.app.use(express.static(sensei.paths.assets));
// cors control
if(sensei.cors !== false) {
(function bootstrap_cors(){
sensei.app.use(function(req, res, next) {
maxAge = sensei.cors.maxAge || 60 * 60 * 24 * 365;
origins = sensei.cors.origins || '*';
methods = sensei.cors.methods || 'GET,OPTIONS';
headers = sensei.cors.headers || 'Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,X-CSRF-Token';
res.set('Access-Control-Max-Age', maxAge);
res.set('Access-Control-Allow-Origin', origins);
res.set('Access-Control-Allow-Methods', methods);
res.set('Access-Control-Allow-Headers', headers) ;
// Intercept OPTIONS method
if (req.method == 'OPTIONS') {
return res.sendStatus(200);
} else {
next();
}
});
})();
};
// body parsing
sensei.app.use(bodyParser.urlencoded({ extended : false }));
sensei.app.use(bodyParser.json());
sensei.app.use(bodyParser.raw());
// Random configs
sensei.app.set('trust proxy', false);
sensei.app.set('x-powered-by', false);
// EJS rendering engine for templates
sensei.app.set('views', sensei.paths.views);
sensei.app.set('view engine', 'ejs');
sensei.app.set('view cache', false);
// for layout control, relative to views path
sensei.app.set('layout', sensei.paths.layout);
sensei.app.use(expressLayouts)
sensei.app.set('layout extractScripts', true);
this.defaults = defaults;
},
/*
* Bootstrap all components in sequence and start the server
* @param none
* @return void
*/
begin : function(){
var self = this;
if(!self.defaults) {
throw new Error('Sensei requires configure() before begin()');
}
// Create proxy var to store models, managers and services
var _entities = {}, _components = {}, _managers = {}, _services = {}, _policies = {};
/*
* Order of bootstrapping is important:
*
* (1) Components
* (2) Entities
* (3) Managers
* (4) Services
* (5) Policies
* (6) Routes
*
*/
/*
* Components are low-level libraries unrelated to Enties and Managers
*/
(function bootstrap_components() {
var components = fs.readdirSync(sensei.paths.components);
components.forEach(function(component) {
if(null == component.match(/\.js/i)) return;
var klass = component.replace(/\.js$/i,'');
_components[klass] = require(path.join(sensei.paths.components, component));
});
sensei.components = _components;
})();
/*
* Entities represent models of data
*/
(function boostrap_entities() {
var entities = fs.readdirSync(sensei.paths.entities);
entities.forEach(function(entity) {
if(null == entity.match(/\.js/i)) return;
var klass = entity.replace(/\.js$/i,'');
var schema = require(path.join(sensei.paths.entities, entity));
var def = _entities[klass.toLowerCase()] = { id : klass };
// Use custom tableName if available
if(false == schema.hasOwnProperty('tableName')) {
schema.tableName = klass;
}
// Connection will use `default` unless otherwise specified
// - infrastructure/config/adapters.js:connection.default
schema.connection = 'default' || def.connection;
def.model = waterline.Collection.extend( schema );
orm.registerModel(def.model);
});
})();
/*
* Managers use entities to execute business logic
*/
(function bootstrap_managers() {
var managers = fs.readdirSync(sensei.paths.managers);
managers.forEach(function(manager) {
if(null == manager.match(/\.js/i)) return;
var klass = manager.replace(/\.js$/i,'');
_managers[klass] = require(path.join(sensei.paths.managers, manager));
});
sensei.managers = _managers;
})();
/*
* Services are used to extract helper functions for the routes/views
*/
(function bootstrap_services() {
var services = fs.readdirSync(sensei.paths.services);
services.forEach(function(service) {
if(null == service.match(/\.js/i)) return;
var klass = service.replace(/\.js$/i,'');
_services[klass] = require(path.join(sensei.paths.services, service));
});
sensei.services = _services;
})();
/*
* Policies are pre-filters that run before a route can be honored
*/
(function bootstrap_policies() {
var policies = fs.readdirSync(sensei.paths.policies);
policies.forEach(function(policy) {
if(null == policy.match(/\.js/i)) return;
var klass = policy.replace(/\.js$/i,'');
_policies[klass] = require(path.join(sensei.paths.policies, policy));
});
sensei.policies = _policies;
})();
/*
* All the routes
*/
(function bootstrap_routes() {
var routes = require(sensei.routes);
sensei.app.use(function(req, res, next) {
var regex = /^(get|post|put|delete|patch|head)/i;
for(var r in routes) {
// Match start of route with: get, post, put, delete, patch
if(m = r.match(regex)) {
// Extract the method and route
var method = m[0].toLowerCase();
var route = r.replace(new RegExp('^' + method + '\\s+','i'),'');
// Create a proxy object to store temp params
var proxy = { route : routes[r], policies : [], expires : 0 };
// If the configured route is an object, extract params: expires, route, policies
if(Object.prototype.toString.call(routes[r]) == '[object Object]') {
// Found: policies
// Policies must be an array. Values must match: interface/policies/`Policy.name`
if(routes[r].policies && Object.prototype.toString.call(routes[r].policies) == '[object Array]') {
proxy.policies = routes[r].policies;
proxy.policies.forEach(function(policy) {
// Allow policy to be lambda function(req, res)
if(typeof policy == 'function') {
return sensei.app[ method ](route, policy);
}
var parts = policy.split('.');
var policy = null;
// Ensure policy configuration is a policy.method
if(parts.length == 1) {
throw new Error('Binding a policy to a route requires a method: Policy.method');
}
// Ensure policy exists
if(false == sensei.policies.hasOwnProperty( parts[0] )) {
throw new Error('Cannot bind non-existent policy ' + parts[0]);
}
policy = sensei.policies[ parts[0] ];
// Ensure policy has method
if(false == policy.hasOwnProperty( parts[1] )) {
throw new Error('Cannot bind non-existent policy method ' + parts[0] + '.' + parts[1] );
}
// We made it, bind the policy to the route
sensei.app[ method ](route, policy[ parts[1] ]);
});
}
// Found: expires, honor configuration
if(routes[r].expires) {
sensei.app[ method ](route, function(req, res, next) {
res.set('Last-Modified', (new Date()).toUTCString());
res.set('Cache-Control', 'private, proxy-revalidate, must-revalidate, max-age=' + routes[r].expires + ', s-max-age=' + routes[r].expires);
res.set('Surrogate-Control', 'must-revalidate, max-age=' + routes[r].expires);
res.set('Expires', new Date((new Date().getTime()) + (routes[r].expires * 1000)).toUTCString());
next();
});
}
// Before process CORS from route config
if(typeof routes[r].cors != 'undefined' && routes[r].cors == false) {
sensei.app[ method ](route, function(req, res, next) {
res.removeHeader('Access-Control-Max-Age');
res.removeHeader('Access-Control-Allow-Origin');
res.removeHeader('Access-Control-Allow-Methods');
res.removeHeader('Access-Control-Allow-Headers');
next();
});
}
// Found: route, honor route
if(routes[r].path) {
proxy.route = routes[r].path;
sensei.app[ method ](route, require( path.join(sensei.paths.routes, proxy.route) ) );
} else if(null == r.match(/\*/)) {
// Use named route if path not specified
sensei.app[ method ](route, require( path.join(sensei.paths.routes, route) ) );
}
} else {
// Assign route with method and route handler
sensei.app[ method ](route, require( path.join(sensei.paths.routes, proxy.route) ) );
}
} else {
console.warn('Invalid route detected ' + routes[r]);
res.status(400).end('Invalid Request for ' + routes[r]);
}
}
next();
});
})();
var adapters = require(sensei.adapters);
// Configure HTTP or HTTPS Server
if(sensei.ssl && sensei.ssl.key && sensei.ssl.cert) {
var https = require('https');
https.globalAgent.maxSockets = 100;
sensei.server = https.createServer(sensei.ssl, sensei.app);
} else {
var http = require('http');
http.globalAgent.maxSockets = 100;
sensei.server = http.createServer(sensei.app);
}
// Keep-alive timeout
sensei.server.on('connection', function(socket) {
socket.setTimeout( self.defaults.keepAlive );
});
// get this server up and running
orm.initialize(adapters, function(err, models) {
if (err) {
console.error(err);
}
// Assign boolean if `globals` in default array are empty
var allAreGlobal = self.defaults.globals.length == 0;
// push components to global scope based on file name
if(allAreGlobal || self.defaults.globals.indexOf('components') > -1) {
if(Object.keys(sensei.components).length) {
for(var name in _components) {
global[ name ] = _components[ name ];
}
}
}
// push entities to global scope based on file name
if(models.collections) {
for(var name in models.collections) {
// Push entities to sensei local scope
sensei.entities[ _entities[name].id ] = models.collections[name];
// Push entities to global scope
if(allAreGlobal || self.defaults.globals.indexOf('entities') > -1) {
global[ _entities[name].id ] = sensei.entities[ _entities[name].id ];
}
}
}
// push managers to global scope based on file name
if(allAreGlobal || self.defaults.globals.indexOf('managers') > -1) {
if(Object.keys(sensei.managers).length) {
for(var name in _managers) {
global[ name ] = _managers[ name ];
}
}
}
// push services to global scope based on file name
if(allAreGlobal || self.defaults.globals.indexOf('services') > -1) {
if(Object.keys(sensei.services).length) {
for(var name in _services) {
global[ name ] = _services[ name ];
}
}
}
sensei.server.setMaxListeners(0);
sensei.server.listen(sensei.port, function(){
console.log('Sensei says, listening on port ' + sensei.port);
});
});
function bootstrap_cleanup(callback) {
// attach user callback to the process event emitter
// if no callback, it will still exit gracefully on Ctrl-C
callback = callback || noOp;
process.on('cleanup', callback);
// do app specific cleaning before exiting
process.on('exit', function () {
process.emit('cleanup');
});
// catch ctrl+c event and exit normally
process.on('SIGINT', function () {
console.log('Ctrl-C...');
process.exit(2);
});
// catch uncaught exceptions, trace, then exit normally
process.on('uncaughtException', function(e) {
console.log('Uncaught Exception...');
console.log(e.stack);
process.exit(99);
});
};
bootstrap_cleanup(function(){
if(sensei.cleanup && typeof sensei.cleanup == 'function') {
console.log('Sensei says cleanup time');
sensei.cleanup();
}
});
}
};