-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathepiphany.js
More file actions
122 lines (95 loc) · 3.17 KB
/
epiphany.js
File metadata and controls
122 lines (95 loc) · 3.17 KB
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
// modules > native
var fs = require('fs');
var p = require('path');
// modules > 3rd party
var dust = require('dustjs-linkedin');
var chalk = require('chalk');
var express = require('express');
var mongoose = require('mongoose');
// modules > express middlewares
var bodyParser = require('body-parser');
var session = require('express-session');
var cookieParser = require('cookie-parser');
// modules > internal
var loadTemplates = require('./load-templates');
var loadPages = require('./load-pages');
// extend dust
require('./dust-extensions');
var Epiphany = function(options) {
this.config = options.config;
// setup express
this.express = require('./express-setup');
// set up default prewares
this.prewares = [
express.static(this.config.dir.static, ENV === 'production' ? { maxAge: '1 year' } : null),
express.static(this.config.dir.uploads, ENV === 'production' ? { maxAge: '1 year' } : null),
bodyParser.json(),
bodyParser.urlencoded({ extended:true }),
cookieParser(),
session(this.config.session)
];
// TODO should this be active in staging?
if(ENV !== 'production') {
this.prewares.unshift(require('morgan')('dev'));
}
// set up default postwares
this.postwares = [
require('./middleware/ensure-found'),
// transform and log error
require('./middleware/error-handler'),
// respond
require('./middleware/responder'),
// handle error rendering error
require('./middleware/responder-error'),
];
loadTemplates(this.config.dir.templates);
this.routes = [
[ '/templates/*', 'get', require('./middleware/templates') ]
];
_.each([ options ].concat(options.modules), this.module.bind(this));
_.mergeWith(this, loadPages(options.pages), function(a, b) {
if (_.isArray(a)) {
return a.concat(b);
}
});
/* paths on express instance below is currently only used by hats/content
* paths middleware.
*/
_.merge(this.express, _.pick(this, 'paths'));
if(options.start !== false) this.start();
return this;
};
Epiphany.prototype.module = function(module, last) {
if(module.routes)
this.routes = this.routes.concat(module.routes);
_.merge(dust, _.pick(module, 'filters', 'helpers'));
};
Epiphany.prototype.start = function() {
var self = this;
this.prewares.forEach(function(middleware, i) {
if(_.isArray(middleware) && _.isString(middleware[0]))
self.express.use(middleware[0], middleware[1]);
else
self.express.use(middleware);
});
_.each(this.routes, function(arr) {
var path = arr[0],
method = arr[1],
middleware = arr[2];
if(!_.isFunction(middleware) && (_.isEmpty(middleware) || _.some(middleware, function(value) { return !_.isFunction(value); }))) {
throw new Error('Undefined or non-function as middleware for [' + method + ']:' + path);
}
self.express[method](path, middleware);
});
// initialize postwares (from old setPost)
this.postwares.forEach(function(middleware) {
self.express.use(middleware);
});
// connect to mongodb
mongoose.connect(this.config.mongo.uri, _.omit(this.config.mongo, 'uri'));
// start server and let them know it
this.express.listen(this.config.port, function() {
console.info('Express server started on port %s (%s)', self.config.port, ENV);
});
};
module.exports = Epiphany;