forked from pencilblue/pencilblue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pencilblue.js
executable file
·251 lines (225 loc) · 8.08 KB
/
pencilblue.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
/*
Copyright (C) 2014 PencilBlue, LLC
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// A grouping of all require calls
global.pb = require('./include/requirements');
/**
* The main driver file for PencilBlue. 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 avaialble by requiring the
* "requirements.js" file.
* @class PencilBlue
* @constructor
*/
function PencilBlue(){}
/**
* 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.
*/
PencilBlue.init = function(){
var tasks = [
PencilBlue.initRequestHandler,
PencilBlue.initDBConnections,
PencilBlue.initDBIndices,
PencilBlue.initServer,
PencilBlue.initPlugins,
PencilBlue.initServerRegistration,
PencilBlue.initCommandService,
PencilBlue.initLibraries
];
async.series(tasks, function(err, results) {
if (util.isError(err)) {
throw err;
}
pb.log.info('PencilBlue: Ready to run!');
});
};
/**
* 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)
*/
PencilBlue.initRequestHandler = function(cb) {
pb.RequestHandler.init();
cb(null, true);
}
/**
* Initializes the installed plugins.
* @static
* @method initPlugins
* @param {Function} cb A callback that provides two parameters: cb(Error, Boolean)
*/
PencilBlue.initPlugins = function(cb) {
pb.plugins.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)
*/
PencilBlue.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);
}
log.debug('PencilBlue: 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)
*/
PencilBlue.initDBIndices = function(cb) {
if (pb.config.db.skip_index_check || !util.isArray(pb.config.db.indices)) {
pb.log.info('PencilBlue: Skipping ensurance of indices');
return cb(null, true);
}
pb.log.info('PencilBlue: 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)
*/
PencilBlue.initServer = function(cb){
log.debug('Starting server...');
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),
ca: fs.readFileSync(pb.config.server.ssl.chain)
};
pb.server = https.createServer(options, PencilBlue.onHttpConnect);
//create an http server that redirects to SSL site
pb.handOffServer = http.createServer(PencilBlue.onHttpConnectForHandoff);
pb.handOffServer.listen(pb.config.server.ssl.handoff_port, function() {
log.info('PencilBlue: Handoff HTTP server running on port: %d', pb.config.server.ssl.handoff_port);
});
}
else {
pb.server = http.createServer(PencilBlue.onHttpConnect);
}
//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() {
log.info('PencilBlue: %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
*/
PencilBlue.onHttpConnect = function(req, resp){
if (pb.log.isSilly()) {
req.uid = new ObjectID();
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') {
PencilBlue.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
*/
PencilBlue.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])
*/
PencilBlue.initServerRegistration = function(cb) {
pb.ServerRegistration.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])
*/
PencilBlue.initCommandService = function(cb) {
pb.CommandService.init(cb);
};
/**
* Initializes the libraries service
* @static
* @method initLibraries
* @param {Function} cb A callback that provides two parameters: cb(Error, [RESULT])
*/
PencilBlue.initLibraries = function(cb) {
pb.libraries.init(cb);
};
//start system
pb.system.onStart(PencilBlue.init);