forked from geddy/geddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
647 lines (568 loc) · 21.2 KB
/
app.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/*
* Geddy JavaScript Web development framework
* Copyright 2112 Matthew Eernisse (mde@fleegix.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
var fs = require('fs')
, path = require('path')
, url = require('url')
, model = require('model')
, utils = require('./utils')
, querystring = require('../deps/qs')
, cwd = process.cwd()
, errors = require('./response/errors')
, response = require('./response')
, init = require('./init')
, helpers = require('./template/helpers')
, actionHelpers = require('./template/helpers/action')
, BaseController = require('./controller/base_controller').BaseController
, ErrorController = require('./controller/error_controller').ErrorController
, StaticFileController =
require('./controller/static_file_controller').StaticFileController
, sessions = require('./sessions')
, CookieCollection = require('./cookies').CookieCollection
, Request = require('./request').Request
, Response = response.Response
, InFlight = require('./in_flight').InFlight
, i18n = utils.i18n
, usingCoffee; // Global variable for CoffeeScript
// Set up a bunch of aliases
geddy.inFlight = new InFlight();
geddy.inflection = utils.inflection;
geddy.model = model;
geddy.utils = utils;
geddy.errors = errors;
geddy.viewHelpers = {};
// Use Geddy logger as the utility logger
utils.log.registerLogger(geddy.log);
var App = function () {
var JSPAT = /\.(js|coffee)$/;
var _getDirList = function (dirname) {
var dirList = fs.readdirSync(dirname)
, fileName
, filePath
, ctorName
, ret = [];
for (var i = 0; i < dirList.length; i++) {
fileName = dirList[i];
// Any files ending in '.js' or '.coffee'
if (JSPAT.test(fileName)) {
if (fileName.match(/\.coffee$/)) {
// fileName is a CoffeeScript file so try to require it
usingCoffee = usingCoffee || utils.file.requireLocal('coffee-script');
}
// Strip the extension from the file name
fileName = fileName.replace(JSPAT, '');
// Convert underscores to camelCase with
// initial cap, e.g., 'NeilPearts'
ctorName = geddy.string.camelize(fileName, {initialCap: true});
filePath = path.join(cwd, dirname, fileName);
ret.push({
ctorName: ctorName
, filePath: filePath
});
}
}
return ret;
}
// Locales and models
// ==================
, _baseInit = function (next) {
init.init(this, next);
}
// Load controller ctors
// ==================
, _registerControllers = function (next) {
var controllerDir = 'app/controllers'
, dirList = _getDirList(controllerDir)
, item
, ctor
, ctorActions;
// Dynamically create controller constructors
// from files in app/controllers
for (var i = 0; i < dirList.length; i++) {
item = dirList[i];
ctor = require(item.filePath)[item.ctorName];
ctor.origPrototype = ctor.prototype;
this.controllerRegistry[item.ctorName] = ctor;
}
next();
}
// Connect to session-store
// ==================
, _loadSessionStore = function (next) {
sessionsConfig = geddy.config.sessions;
if (sessionsConfig) {
sessions.createStore(sessionsConfig.store, next);
}
else {
next();
}
}
// Load metrics if they're turned on
// ==================
, _loadMetrics = function (next) {
if (geddy.config.metrics) {
geddy.metrics = require('./metric');
}
next();
}
// Register template-paths
// ==================
, _registerTemplatePaths = function (next) {
var viewsPath = path.normalize('app/views')
, geddyTemplatesPath = path.join(__dirname, 'template', 'templates');
// If viewsPath doesn't exist they're running viewless
if(!utils.file.existsSync(viewsPath)) {
this.templateRegistry = {};
next();
} else {
var files = utils.file.readdirR(viewsPath)
, geddyTemplatesFiles = utils.file.readdirR(geddyTemplatesPath)
, pat = /\.(ejs|jade|hbs|mustache|ms|mu)$/
, templates = {}
, file
, origFile
, noExtFile
, fileExt
, fileBaseName
, addTemplate
, createTemplates;
// Adds a template object to templates
addTemplate = function (noExtFile, file, origFile, fileExt, fileBaseName) {
if(!origFile) origFile = noExtFile;
templates[origFile] = {
registered: true
, file: file
, ext: fileExt
, baseName: fileBaseName
, baseNamePath: noExtFile
};
};
// Read dir list and create template objects from each file
createTemplates = function (dir, isGeddy) {
for (var i = 0; i < dir.length; i++) {
file = dir[i];
fileExt = path.extname(file);
fileBaseName = path.basename(file, fileExt).replace(/\.html$/, '');
if (isGeddy) {
origFile = 'geddy/' + fileBaseName;
}
if (pat.test(file)) {
// Strip .html and extension for easier detecting when rendering
if (isGeddy) {
noExtFile = 'geddy/' + fileBaseName;
} else noExtFile = file.replace(/\.html.*$/, '');
addTemplate(noExtFile, file, origFile, fileExt, fileBaseName);
}
}
};
// Loop through template files and add it them to registry
createTemplates(files);
// Add custom templates from `lib/template/templates`
createTemplates(geddyTemplatesFiles, true);
this.templateRegistry = templates;
next();
}
}
// Run code in the app's config/init.js or .coffee variation
// ==================
, _runAppLocalInit = function (next) {
var dirList = fs.readdirSync(cwd + '/config')
, fileName
, fileExt
, fileBaseName
, i = dirList.length;
while (--i >= 0) {
fileName = dirList[i];
fileExt = path.extname(fileName);
fileBaseName = path.basename(fileName, fileExt);
if (fileBaseName === 'init') {
if (fileExt === '.coffee') {
// fileName is a CoffeeScript file so try to require it
usingCoffee = usingCoffee || utils.file.requireLocal('coffee-script');
}
require(path.join(cwd, 'config', fileName));
}
}
next();
}
// Run code in the app's config/after_start.js file
// and run anything else that needs to be run after
// the server has started listening for requests
// ================================================
, _afterStart = function (next) {
geddy.server.listeners('request');
// Load socket.io if it's enabled. This gets run after
// the server starts listening for requests. Socket.io
// won't work if we start it before.
if (geddy.config.realtime) {
geddy.io = utils.file.requireLocal('socket.io').listen(geddy.server, {'log level': 0});
// add event listeners to all the models
// set up socket.io emitters for each event
var events = [
, 'save'
, 'update'
, 'beforeRemove'
]
, registerEventListener = function (model, event) {
geddy.model[model].on(event, function (data) {
var id;
event = event.replace('before','').toLowerCase();
if (typeof data == 'object') {
data.model = model;
data.event = event;
id = data.id;
}
else {
id = data;
}
geddy.io.sockets.emit(model+':'+event, data);
geddy.io.sockets.emit(model+':'+event+':'+id, data);
});
}
for (var i in geddy.model.descriptionRegistry) {
for (var e in events) {
registerEventListener(i, events[e]);
}
}
}
try {
require(path.join(cwd, 'config', 'after_start'));
} catch (e) {}
};
this.router = null;
this.modelRegistry = {};
this.templateRegistry = {};
this.controllerRegistry = {};
this.init = function (callback) {
var self = this
, items
, chain;
items = [
_baseInit
// TODO: Migrate all these into _baseInit
, _registerControllers
, _loadSessionStore
, _loadMetrics
, _registerTemplatePaths
, _runAppLocalInit
];
chain = new geddy.async.SimpleAsyncChain(items, this);
chain.last = function () {
self.start(callback);
};
chain.run();
};
this.start = function (callback) {
var self = this
, ctors = this.controllerRegistry
, router = this.router
, allRequestTimer = geddy.config.metrics && geddy.metrics.Timer('Geddy.all-requests')
, controllerActionTimers = {};
// Handle the requests
// ==================
geddy.server.addListener('request', function (req, resp) {
var reqUrl
, reqObj
, respObj
, params
, urlParams
, method
, ctor
, appCtor
, baseController
, controller
, controllerName
, actionName
, staticUrl
, publicPath
, partialsPath
, staticResp
, nonMethodRoutes
, acceptableMethods
, err
, errResp
, contentType
, body = ''
, bodyParams
, steps = {
parseBody: false
, sessions: false
}
, finish
, accessTime = (new Date()).getTime()
, inFlighId
, sendStaticFile;
reqUrl = req.url;
// Sanitize URL; reduce multiple slashes to single slash
reqUrl = reqUrl.replace(/\/{2,}/g, '/');
// Strip trailing slash for the purpose of looking for a matching
// route (will still check for directory + index on statics)
// Don't strip if the entire path is just '/'
reqUrl = reqUrl.replace(/(.+)\/$/, '$1');
// Buffered request-obj -- buffer the request data,
// and pass this proxy object to the controller
reqObj = new Request(req);
// Wrapped response-obj
respObj = new Response(resp);
finish = function (step) {
steps[step] = true;
for (var p in steps) {
if (!steps[p]) {
return false;
}
}
controller._handleAction.call(controller, params.action);
reqObj.sync(); // Flush buffered events and begin emitting
};
if (router) {
urlParams = url.parse(reqUrl, true).query;
if (req.method.toUpperCase() == 'POST') {
// POSTs may be overridden by the _method param
if (urlParams._method) {
method = urlParams._method;
}
// Or x-http-method-override header
else if (req.headers['x-http-method-override']) {
method = req.headers['x-http-method-override'];
}
else {
method = req.method;
}
}
else {
method = req.method;
}
// Okay, let's be anal and force all the HTTP verbs to uppercase
method = method.toUpperCase();
inFlightId = geddy.inFlight.addEntry({
request: req
, method: method
, response: respObj
, accessTime: accessTime
});
reqObj._geddyId = inFlightId;
respObj._geddyId = inFlightId;
// FIXME: Holy shit, all inline here
// Probably should proxy events for wrapped Response obj
respObj.resp.addListener('finish', function () {
var id = respObj._geddyId
, entry = geddy.inFlight.getEntry(id)
, req = entry.request
, stat = respObj.statusCode
, endTime = new Date()
, level = parseInt(stat, 10);
// Status code representation for logging
if (level > 499) {
level = 'error';
} else level = 'access';
// Apache extended log-format
geddy.log[level](req.connection.remoteAddress + ' ' +
'- ' +
'- ' +
'[' + new Date(entry.accessTime) + '] ' +
'"' + entry.method + ' ' + reqUrl + ' ' +
req.httpVersion + '" ' +
stat + ' ' +
(respObj._length || '-') + ' ' +
'"' + (req.headers['referer'] || '-') + '" ' +
'"' + (req.headers['user-agent'] || '-') + '" ');
geddy.inFlight.removeEntry(id);
if (geddy.config.metrics) {
allRequestTimer.update(endTime - accessTime);
controllerActionTimers[controllerName] = controllerActionTimers[controller] || {};
controllerActionTimers[controllerName][actionName] =
controllerActionTimers[controllerName][actionName] ||
geddy.metrics.Timer("Geddy."+controllerName + '.' + actionName);
controllerActionTimers[controllerName][actionName].update(endTime - accessTime);
}
});
params = router.first(reqUrl, method);
}
if (params) {
controllerName = utils.string.camelize(params.controller, {initialCap: true});
// Shim -- router now returns snakeized name
params.controller = controllerName;
actionName = params.action;
ctor = ctors[controllerName];
if (ctor) {
// Merge params from the URL and the query-string to produce a
// Grand Unified Params object
geddy.mixin(params, urlParams);
// If it's a plain form-post, save the request-body, and parse it into
// params as well
contentType = req.headers['content-type'];
if ((req.method == 'POST' || req.method == 'PUT') &&
(contentType &&
(contentType.indexOf('form-urlencoded') > -1 ||
contentType.indexOf('application/json') > -1))) {
bodyParams = {};
req.addListener('data', function (data) {
body += data.toString();
});
// Parse the body into params once it's finished
req.addListener('end', function () {
if (contentType.indexOf('form-urlencoded') > -1) {
bodyParams = querystring.parse(body);
}
else if (contentType.indexOf('application/json') > -1) {
try {
bodyParams = JSON.parse(body);
}
catch (e) {}
}
geddy.mixin(params, bodyParams);
req.body = body;
finish('parseBody');
});
}
else {
finish('parseBody');
}
// If there's an Application controller, use an instance of it
// as the prototype of the controller for this request
if (ctors.Application) {
// The constructor for the Application controller
appCtor = ctors.Application;
// Give the constructor a new prototype to inherit from: an instance
// of the BaseController, with the methods/properties from its original
// prototype copied over
appCtor.prototype = utils.enhance(new BaseController(),
appCtor.origPrototype);
appCtor.prototype.constructor = appCtor;
// Make an instance -- this instance will be the proto for the
// controller for this request
baseController = new appCtor();
}
// If there's no Application controller, use an instance of
// BaseController as the proto
else {
baseController = new BaseController();
}
// Give the constructor for this request's controller a new
// prototype: the Application/BaseController (or just BaseController)
// instance, with the methods/properties from the original prototype
// copied over
ctor.prototype = utils.enhance(baseController,
ctor.origPrototype);
ctor.prototype.constructor = ctor;
// Instantiate the controller
controller = new ctor();
// Now that there's a controller, record the access-time
controller.accessTime = accessTime;
if (params.action == 'destroy' && typeof controller[params.action] != 'function') {
params.action = 'remove';
}
if (typeof controller[params.action] == 'function') {
controller.app = self;
controller.request = reqObj;
reqObj.controller = controller;
controller.response = respObj;
respObj.controller = controller;
controller.method = method;
controller.params = params;
controller.name = params.controller;
controller.i18n = new i18n.I18n(controller);
// We have a controller instance; register it with the
// in-flight data
geddy.inFlight.updateEntry(reqObj._geddyId, {
controller: controller
});
controller.cookies = new CookieCollection(req);
if (geddy.config.sessions) {
controller.session =
new sessions.Session(controller, function () {
finish('sessions');
});
}
else {
finish('sessions');
}
}
// No action, 500 error
else {
err = new errors.InternalServerError('No ' + params.action +
' action on ' + params.controller + ' controller.');
controller = new ErrorController(err, reqObj, respObj);
controller.respond();
}
}
// no controller, 500 error
else {
err = new errors.InternalServerError('controller ' +
params.controller + ' not found.');
controller = new ErrorController(err, reqObj, respObj);
controller.respond();
}
}
// Either static, 404, or 405
else {
// Get the path to the file
staticPath = geddy.config.staticFilePath + reqUrl;
// Ignore querystring
staticPath = staticPath.split('?')[0];
// Decode path (e.g. %20)
staticPath = decodeURIComponent(staticPath);
if (utils.file.existsSync(staticPath)) {
// May be a path to a directory, with or without a trailing
// slash -- any trailing slash has already been stripped by
// this point
if (fs.statSync(staticPath).isDirectory()) {
// TODO: Make the name of any index file configurable
staticPath = path.join(staticPath, 'index.html');
if (utils.file.existsSync(staticPath) && fs.statSync(staticPath).isFile()) {
controller = new StaticFileController(staticPath, reqObj, respObj);
controller.respond();
}
}
// Path to an actual file. Just serve it up
else if (fs.statSync(staticPath).isFile()) {
controller = new StaticFileController(staticPath, reqObj, respObj);
controller.respond();
}
}
// 405?
else {
nonMethodRoutes = router.all(reqUrl);
if (nonMethodRoutes.length) {
// build a unique list of acceptable methods for this resource
acceptableMethods = {};
nonMethodRoutes.map(function (params) {
acceptableMethods[params.method] = true;
});
acceptableMethods = Object.keys(acceptableMethods);
// send a friendly error response
err = new errors.MethodNotAllowedError(
method + ' method not allowed. Please consider ' +
acceptableMethods.join(', ').replace(/,\s(\w+)$/," or $1") +
' instead.');
errResp = new response.Response(resp);
errResp.send( err.message, err.statusCode, {'Content-Type': 'text/html'} );
}
// 404
else {
err = new errors.NotFoundError(reqUrl + ' not found.');
controller = new ErrorController(err, reqObj, respObj);
controller.respond();
}
}
}
});
_afterStart();
callback();
};
};
module.exports.App = App;