/
service.js
283 lines (227 loc) · 13.1 KB
/
service.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
require('./exitHandler')();
require('colors');
var initialize_hook = require('./boot/initialize_hook'),
express = require("express"),
app = express(),
server = require('http').Server(app),
replHistory = require('repl.history');
module.exports = function (callback, conf) {
initialize_hook(server, app, express, function () {
after_init_hook(callback, conf);
});
};
function after_init_hook (callback, conf) {
var requireCwd = function (aPath) {
return require(process.cwd() + aPath);
},
version_notifications = require('./boot/version_notifications'),
version_polyfills = version_notifications(),
hooks = version_polyfills.hooks,
config = requireCwd('/config'),
globalReport = require('./globalReport'),
exposeGlobals = requireCwd('/globals'),
path = require("path"),
mongoose = require('mongoose'),
middleware = requireCwd('/middleware'),
cookieParser = require('cookie-parser'),
verbose = require('./logger').onVerbose,
routes_path = path.join(process.cwd(), 'routes'),
controllers_path = path.join(process.cwd(), 'controllers'),
models_path = path.join(process.cwd(), 'models'),
routes = {},
controllers = {},
showLogo = require('./logo'),
errors = [],
buildFileSystems = require('./boot/build_file_systems'),
applyPolicy = require('./boot/apply_policy'),
bind_routes = require('./boot/bind_routes'),
app_hook = require('./boot/app_hook'),
before_mongoose_hook = require('./boot/before_mongoose_hook'),
before_body_parser_hook = require('./boot/before_body_parser_hook'),
before_method_override_hook = require('./boot/before_method_override_hook'),
before_cookie_parser_hook = require('./boot/before_cookie_parser_hook'),
before_read_session_hook = require('./boot/before_read_session_hook'),
before_router_hook = require('./boot/before_router_hook'),
before_listen_hook = require('./boot/before_listen_hook'),
tokenizer = require('./classes/tokenizer'),
scan_plugins = require('./boot/scan_glad_plugins'),
finalize;
/**
* Extend Developer Config with Defaults
*/
if (conf) {
(function () {
for (var key in conf) {
if (conf.hasOwnProperty(key)) {
config[key] = conf[key];
}
}
}());
}
// Show The Logo, or not (depending on config)
showLogo(config);
// ID each request and possibly log it as well.
app.use(function (req, res, next) {
req.id = tokenizer.timeCoded();
if (config.logHTTP) {
console.log(
("Request " + req.id + " <<").cyan,
(req.method + ":").cyan, (req.url).cyan
);
}
next();
});
/**
* If the developer has chosen to make the models available globally
*/
if (config.exposeModels) {
buildFileSystems(models_path, global, true, errors);
}
/**
* Require in all of the routes and controllers
*/
buildFileSystems(routes_path, routes, false, errors);
buildFileSystems(controllers_path, controllers, false, errors);
scan_plugins(function (plugins) {
app_hook(hooks, server, app, express, function () {
var appHookInjections = plugins.filter(function (plugin) {
return plugin.hook === 'app';
});
var nextInjection = function () {
appHookInjections.shift();
if (appHookInjections[0] && appHookInjections[0].method) {
verbose("Injecting", appHookInjections[0].name, "Plugin");
appHookInjections[0].method(server, app, express, nextInjection);
} else {
onAfterAppHookInjections();
}
};
if (appHookInjections.length) {
verbose("Injecting", appHookInjections[0].name, "Plugin");
appHookInjections[0].method(server, app, express, nextInjection);
} else {
onAfterAppHookInjections();
}
function onAfterAppHookInjections () {
verbose("Glad: Exposing Global Objects".yellow);
exposeGlobals(global, function () {
verbose('Glad: Globals Registered ::'.magenta, (globalReport()).magenta);
before_mongoose_hook(mongoose, hooks, server, app, express, plugins, function () {
if (config.mongodb) {
mongoose.connect('mongodb://' + config.mongodb.host + ':' + config.mongodb.port + '/' + config.mongodb.database);
verbose("Glad: Connecting To".yellow, ('mongodb://' + config.mongodb.host + ':' + config.mongodb.port + '/' + config.mongodb.database).yellow);
} else {
verbose("Glad: Bypassing Connection To Mongo DB.".yellow);
}
before_body_parser_hook(hooks, server, app, express, plugins, function () {
app.use(require('body-parser')[config.bodyParser]({limit : config.maxBodySize || '1mb'}));
before_method_override_hook(hooks, server, app, express, plugins, function () {
app.use(require('method-override')());
before_cookie_parser_hook(hooks, server, app, express, plugins, function () {
app.use(cookieParser());
before_read_session_hook(hooks, server, app, express, plugins, function () {
// Support Users Who don't need a session
if (!config.sessionless) {
verbose("Glad: Middleware :: Using Session Middleware");
middleware.session(app);
} else {
verbose("Glad: Middleware :: Skipping Session Middleware");
}
app.use(middleware.onRequest);
app.use(function (req, res, next) {
req.on("end", function () {
var color = "green";
if (config.logHTTP) {
if (res.statusCode >= 400 && res.statusCode < 500) {
color = "yellow";
} else if (res.statusCode >= 500) {
color = "red";
}
console.log(
("Response " + req.id + " >>")[color],
String(res.statusCode)[color],
String(res.statusMessage)[color],
String((res._headers['content-length'] || 0) + " bytes").black
);
}
middleware.onAfterResponse(req, res);
});
next();
});
app.use(function (req, res, next) {
app.disable('x-powered-by');
res.setHeader('X-Powered-By', 'Glad');
next();
});
app.use(require('errorhandler')({dumpExceptions : true, showStack : true}));
app.use(function (req, res, next) {
var send = res.send;
res.send = function () {
if (req.route.methods.put) {
middleware.onAfterPUT(req);
} else if (req.route.methods.get) {
middleware.onAfterGET(req);
} else if (req.route.methods.post) {
middleware.onAfterPOST(req);
} else if (req.route.methods.delete) {
middleware.onAfterDELETE(req);
}
send.apply(res, arguments);
};
next();
});
before_router_hook(hooks, server, app, express, plugins, function () {
// Bind The Routes
bind_routes(app, routes, controllers, applyPolicy);
// Allow Using Custom Middleware
if (middleware.custom) {
middleware.custom(app, express);
}
finalize = function (listen) {
if (hooks.onAfterListen && listen !== false) {
verbose("Glad: Hooks :: onAfterListen");
hooks.onAfterListen(server, app, express);
}
require('dns').lookup(require('os').hostname(), function (err, add) {
if (listen !== false) {
console.log(' :) Glad: Server Listening On:'.green, (add + ':' + config.port.toString()).green);
}
if (errors.length) {
console.log(" :( Glad: Server is up with the following errors".red);
errors.forEach(function (err) {
console.log((err.message).red);
});
}
});
if (config.interactive) {
setTimeout(function () {
console.log(" Application will now run in interactive mode".green);
console.log(' (ctrl + c) twice to exit interactive mode, then once more to quit the application\n'.grey)
var repl = require("repl");
replHistory(repl.start("Glad > ".yellow), path.join(process.cwd(), '.glad_history'));
}, 1000);
}
if (callback && typeof callback === "function") {
callback(app, express, server);
}
};
if (config.listen !== false) {
before_listen_hook(hooks, server, app, express, function () {
server.listen(config.port || 4242);
finalize();
});
} else {
console.log('Application is not bound to any port because config.listen is set to false'.green);
finalize(false);
}
});
});
});
});
});
});
});
}
});
});
}