This repository has been archived by the owner on Jul 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
executable file
·325 lines (288 loc) · 8.88 KB
/
main.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
//dependencies
var fs = require('fs');
var http = require('http');
var https = require('https');
var async = require('async');
var npm = require('npm');
var util = require('./include/util.js');
var HtmlEncoder = require('htmlencode');
/**
* The main driver file for Pimp My Cause. Provides the function necessary to
* start up the master and/or child processes. In addition, it is responsible
* for ensuring that all system services are available by requiring the
* "requirements.js" file.
* @class Main
* @constructor
*/
function Main(config) {
/**
*
* @private
* @static
* @property pb
* @type {Object}
*/
var pb = require('./lib')(config);
/**
* To be called when the configuration is loaded. The function is responsible
* for triggered the startup of the HTTP connection listener as well as start a
* connection pool to the core DB.
* @method init
*/
this.init = function () {
var self = this;
var tasks = [
this.initModules,
this.initRequestHandler,
this.initDBConnections,
this.initDBIndices,
util.wrapTask(this, this.initServer),
this.initSessions,
this.initPlugins,
this.initServerRegistration,
this.initCommandService,
this.initLibraries
];
async.series(tasks, function (err, results) {
if (util.isError(err)) {
throw err;
}
pb.log.info('Main: Ready to run!');
});
};
/**
* Ensures that any log messages by the NPM module are forwarded as output
* to the system logs
* @static
* @method initLogWrappers
* @param {Function} cb A callback that provides two parameters: cb(Error, Boolean)
*/
this.initModules = function (cb) {
npm.on('log', function (message) {
pb.log.info(message);
});
HtmlEncoder.EncodeType = 'numerical';
pb.Localization.init();
cb(null, true);
};
/**
* Initializes the request handler. This causes all system routes to be
* registered.
* @static
* @method initRequestHandler
* @param {Function} cb A callback that provides two parameters: cb(Error, Boolean)
*/
this.initRequestHandler = function (cb) {
pb.RequestHandler.init();
cb(null, true);
}
/**
* Starts the session handler
* @method initSessions
* @param {Function} cb
*/
this.initSessions = function (cb) {
pb.session.start(cb);
};
/**
* Initializes the installed plugins.
* @static
* @method initPlugins
* @param {Function} cb A callback that provides two parameters: cb(Error, Boolean)
*/
this.initPlugins = function (cb) {
//initialize command listeners
pb.PluginService.init();
//initialize the plugins
var pluginService = new pb.PluginService();
pluginService.initPlugins(cb);
};
/**
* Attempts to initialize a connection pool to the core database
* @static
* @method initDBConnections
* @param {Function} cb A callback that provides two parameters: cb(Error, Boolean)
*/
this.initDBConnections = function (cb) {
//setup database connection to core database
pb.dbm.getDb(pb.config.db.name, function (err, result) {
if (util.isError(err)) {
return cb(err, false);
}
else if (!result.databaseName) {
return cb(new Error("Failed to establish a connection to: " + pb.config.db.name), false);
}
pb.log.debug('Main: Established connection to DB: %s', result.databaseName);
cb(null, true);
});
};
/**
* Checks to see if the process should verify that the indices are valid and in
* place.
* @static
* @method initDBIndices
* @param {Function} cb A callback that provides two parameters: cb(Error, Boolean)
*/
this.initDBIndices = function (cb) {
if (pb.config.db.skip_index_check || !util.isArray(pb.config.db.indices)) {
pb.log.info('Main: Skipping ensurance of indices');
return cb(null, true);
}
pb.log.info('Main: Ensuring indices...');
pb.dbm.processIndices(pb.config.db.indices, function (err, results) {
cb(err, !util.isError(err));
});
};
/**
* Initializes the HTTP server(s). When SSL is enabled two servers are created.
* One to handle incoming HTTP traffic and one to handle HTTPS traffic.
* @static
* @method initServer
* @param {Function} cb A callback that provides two parameters: cb(Error, Boolean)
*/
this.initServer = function (cb) {
pb.log.debug('Starting server...');
var self = this;
try {
if (pb.config.server.ssl.enabled) {
//set SSL options
var options = {
key: fs.readFileSync(pb.config.server.ssl.key),
cert: fs.readFileSync(pb.config.server.ssl.cert)
};
//the certificate authority or "chain" is optional. Needed for
//self-signed certs
var chainPath = pb.config.server.ssl.chain;
if (util.isString(chainPath)) {
options.ca = fs.readFileSync(chainPath);
}
//create the server with options & callback
pb.server = https.createServer(options, function (req, res) {
self.onHttpConnect(req, res);
});
//create an http server that redirects to SSL site
pb.handOffServer = http.createServer(function (req, res) {
self.onHttpConnectForHandoff(req, res);
});
pb.handOffServer.listen(pb.config.server.ssl.handoff_port, function () {
pb.log.info('Main: Handoff HTTP server running on port: %d', pb.config.server.ssl.handoff_port);
});
}
else {
pb.server = http.createServer(function (req, res) {
self.onHttpConnect(req, res);
});
}
//start the server
var onServerStartError = function (err) {
err.message = util.format("Failed to start HTTP server on PORT=[%s] binding to SITE_IP=[%s]: %s", pb.config.sitePort, pb.config.siteIP, err.message);
cb(err, false);
};
pb.server.once('error', onServerStartError);
pb.server.listen(pb.config.sitePort, pb.config.siteIP, function () {
pb.log.info('Main: %s running at site root [%s] on port [%d]', pb.config.siteName, pb.config.siteRoot, pb.config.sitePort);
pb.server.removeListener('error', onServerStartError);
cb(null, true);
});
}
catch (e) {
cb(e, false);
}
};
/**
* The function that handles normal server traffic. The function ensures that
* the incoming request is delegated out appropriately. When SSL Termination
* is in use if the 'x-forwarded-proto' header does equal 'https' then the
* request is delegated to the handoff function so the request can be
* redirected appropriately.
* @static
* @method onHttpConnect
* @param {Request} req The incoming request
* @param {Response} resp The outgoing response
*/
this.onHttpConnect = function (req, resp) {
if (pb.log.isSilly()) {
req.uid = util.uniqueId();
pb.log.silly('New Request: ' + req.uid);
}
//check to see if we should inspect the x-forwarded-proto header for SSL
//load balancers use this for SSL termination relieving the stress of SSL
//computation on more powerful load balancers. For me it is a giant pain
//in the ass when all I want to do is simple load balancing.
if (pb.config.server.ssl.use_x_forwarded && req.headers['x-forwarded-proto'] !== 'https') {
this.onHttpConnectForHandoff(req, resp);
return;
}
//route the request
var handler = new pb.RequestHandler(pb.server, req, resp);
handler.handleRequest();
};
/**
* Handles traffic that comes in for HTTP when SSL is enabled. The request is
* redirected to the appropriately protected HTTPS url.
* @static
* @method onHttpConnectForHandoff
* @param {Request} req The incoming request
* @param {Response} res The outgoing response
*/
this.onHttpConnectForHandoff = function (req, res) {
var host = req.headers.host;
if (host) {
var index = host.indexOf(':');
if (index >= 0) {
host = host.substring(0, index);
}
}
if (pb.config.server.ssl.use_handoff_port_in_redirect) {
host += ':' + pb.config.sitePort;
}
res.writeHead(301, {"Location": "https://" + host + req.url});
res.end();
};
/**
* Initializes server registration.
* @static
* @method initServerRegistration
* @param {Function} cb A callback that provides two parameters: cb(Error, [RESULT])
*/
this.initServerRegistration = function (cb) {
pb.ServerRegistration.getInstance().init(cb);
};
/**
* Initializes the command service by calling its "init" function.
* @static
* @method initCommandService
* @param {Function} cb A callback that provides two parameters: cb(Error, [RESULT])
*/
this.initCommandService = function (cb) {
pb.CommandService.getInstance().init(cb);
};
/**
* Initializes the libraries service
* @static
* @method initLibraries
* @param {Function} cb A callback that provides two parameters: cb(Error, [RESULT])
*/
this.initLibraries = function (cb) {
pb.LibrariesService.init(cb);
};
/**
* Starts up the instance of Main
* @method start
*/
this.start = function () {
var self = this;
pb.system.onStart(function () {
self.init();
});
};
}
//start system only when the module is called directly
if (require.main === module) {
var Configuration = require('./include/config.js');
var config = Configuration.load();
var pb = new Main(config);
pb.start();
}
//exports
module.exports = Main;