2013-11-18 02:39:08 -06:00
|
|
|
var path = require('path'),
|
|
|
|
|
fs = require('fs'),
|
2014-01-04 18:05:15 -05:00
|
|
|
nconf = require('nconf'),
|
2013-11-18 02:39:08 -06:00
|
|
|
express = require('express'),
|
2013-04-22 15:17:41 -04:00
|
|
|
WebServer = express(),
|
2014-01-04 18:05:15 -05:00
|
|
|
server,
|
2013-09-30 16:28:22 -04:00
|
|
|
winston = require('winston'),
|
2013-10-14 16:41:34 -04:00
|
|
|
async = require('async'),
|
2014-09-03 01:24:26 -04:00
|
|
|
cluster = require('cluster'),
|
2013-11-18 02:39:08 -06:00
|
|
|
|
2014-03-13 00:49:32 -04:00
|
|
|
emailer = require('./emailer'),
|
2013-12-02 17:10:26 -05:00
|
|
|
db = require('./database'),
|
2013-11-18 02:39:08 -06:00
|
|
|
auth = require('./routes/authentication'),
|
|
|
|
|
meta = require('./meta'),
|
2014-03-18 15:35:48 -04:00
|
|
|
user = require('./user'),
|
|
|
|
|
notifications = require('./notifications'),
|
2014-01-25 16:39:27 -05:00
|
|
|
logger = require('./logger'),
|
2014-03-24 15:35:58 -04:00
|
|
|
plugins = require('./plugins'),
|
2014-03-01 17:26:26 -05:00
|
|
|
middleware = require('./middleware'),
|
2014-03-17 12:53:31 -04:00
|
|
|
routes = require('./routes'),
|
2014-03-28 17:49:58 -04:00
|
|
|
emitter = require('./emitter'),
|
|
|
|
|
|
|
|
|
|
helpers = require('./../public/src/helpers')();
|
2013-05-02 15:57:43 -04:00
|
|
|
|
2014-01-04 18:05:15 -05:00
|
|
|
if(nconf.get('ssl')) {
|
|
|
|
|
server = require('https').createServer({
|
|
|
|
|
key: fs.readFileSync(nconf.get('ssl').key),
|
2014-01-04 18:09:43 -05:00
|
|
|
cert: fs.readFileSync(nconf.get('ssl').cert)
|
2014-01-04 18:05:15 -05:00
|
|
|
}, WebServer);
|
|
|
|
|
} else {
|
|
|
|
|
server = require('http').createServer(WebServer);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-23 12:50:27 -04:00
|
|
|
(function (app) {
|
2013-11-11 13:25:54 -05:00
|
|
|
"use strict";
|
|
|
|
|
|
2014-03-02 14:45:57 -05:00
|
|
|
var port = nconf.get('PORT') || nconf.get('port');
|
2013-08-23 13:14:36 -04:00
|
|
|
|
2014-03-01 17:26:26 -05:00
|
|
|
logger.init(app);
|
2014-03-13 00:49:32 -04:00
|
|
|
emailer.registerApp(app);
|
2014-09-03 12:31:37 -04:00
|
|
|
|
2014-09-05 13:44:56 -04:00
|
|
|
if (cluster.isWorker && process.env.handle_jobs === 'true') {
|
|
|
|
|
notifications.init();
|
2014-09-03 01:24:26 -04:00
|
|
|
user.startJobs();
|
|
|
|
|
}
|
2014-03-02 13:56:46 -05:00
|
|
|
|
2014-04-12 18:33:52 -04:00
|
|
|
// Preparation dependent on plugins
|
|
|
|
|
plugins.ready(function() {
|
2014-10-21 15:25:16 -04:00
|
|
|
async.parallel([
|
|
|
|
|
async.apply(!nconf.get('from-file') ? meta.js.minify : meta.js.getFromFile, app.enabled('minification')),
|
|
|
|
|
async.apply(!nconf.get('from-file') ? meta.css.minify : meta.css.getFromFile),
|
|
|
|
|
async.apply(meta.sounds.init)
|
|
|
|
|
]);
|
2014-04-12 18:33:52 -04:00
|
|
|
});
|
|
|
|
|
|
2014-05-01 15:06:20 -04:00
|
|
|
async.parallel({
|
2014-03-01 17:26:26 -05:00
|
|
|
themesData: meta.themes.get,
|
2014-05-01 15:06:20 -04:00
|
|
|
currentThemeId: function(next) {
|
|
|
|
|
db.getObjectField('config', 'theme:id', next);
|
2014-03-01 17:26:26 -05:00
|
|
|
}
|
|
|
|
|
}, function(err, data) {
|
2014-03-02 14:16:16 -05:00
|
|
|
middleware = middleware(app, data);
|
|
|
|
|
routes(app, middleware);
|
2014-03-01 17:26:26 -05:00
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
winston.error('Errors were encountered while attempting to initialise NodeBB.');
|
|
|
|
|
process.exit();
|
|
|
|
|
} else {
|
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
|
winston.info('Middlewares loaded.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-04-22 16:51:32 +00:00
|
|
|
|
2013-11-28 17:37:17 -05:00
|
|
|
// Cache static files on production
|
|
|
|
|
if (global.env !== 'development') {
|
|
|
|
|
app.enable('cache');
|
2013-12-04 20:37:13 -05:00
|
|
|
app.enable('minification');
|
2013-12-09 14:50:40 -05:00
|
|
|
|
2013-12-10 22:20:11 -05:00
|
|
|
// Configure cache-buster timestamp
|
|
|
|
|
require('child_process').exec('git describe --tags', {
|
|
|
|
|
cwd: path.join(__dirname, '../')
|
|
|
|
|
}, function(err, stdOut) {
|
|
|
|
|
if (!err) {
|
|
|
|
|
meta.config['cache-buster'] = stdOut.trim();
|
|
|
|
|
} else {
|
2014-02-13 12:26:43 -05:00
|
|
|
fs.stat(path.join(__dirname, '../package.json'), function(err, stats) {
|
|
|
|
|
meta.config['cache-buster'] = new Date(stats.mtime).getTime();
|
|
|
|
|
});
|
2013-12-09 14:50:40 -05:00
|
|
|
}
|
2013-12-10 22:20:11 -05:00
|
|
|
});
|
|
|
|
|
}
|
2013-12-09 14:50:40 -05:00
|
|
|
|
2014-03-02 14:45:57 -05:00
|
|
|
if (port !== 80 && port !== 443 && nconf.get('use_port') === false) {
|
2014-03-01 17:26:26 -05:00
|
|
|
winston.info('Enabling \'trust proxy\'');
|
|
|
|
|
app.enable('trust proxy');
|
|
|
|
|
}
|
2013-09-29 20:27:52 -04:00
|
|
|
|
2014-03-02 14:45:57 -05:00
|
|
|
if ((port === 80 || port === 443) && process.env.NODE_ENV !== 'development') {
|
2014-03-01 17:26:26 -05:00
|
|
|
winston.info('Using ports 80 and 443 is not recommend; use a proxy instead. See README.md');
|
|
|
|
|
}
|
2013-09-13 11:10:17 -04:00
|
|
|
|
2014-03-01 17:26:26 -05:00
|
|
|
module.exports.server = server;
|
2014-08-25 11:56:48 -04:00
|
|
|
module.exports.init = function(callback) {
|
2014-03-05 17:13:55 -05:00
|
|
|
server.on("error", function(err){
|
2014-10-28 00:33:58 -04:00
|
|
|
winston.error(err.stack);
|
|
|
|
|
console.log(err.stack);
|
2014-03-05 17:13:55 -05:00
|
|
|
if (err.code === 'EADDRINUSE') {
|
2014-02-09 23:07:12 +00:00
|
|
|
winston.error('NodeBB address in use, exiting...');
|
2014-09-04 20:07:55 -04:00
|
|
|
if (cluster.isWorker) {
|
|
|
|
|
cluster.worker.kill();
|
|
|
|
|
} else {
|
|
|
|
|
process.exit(0);
|
|
|
|
|
}
|
2014-02-09 23:07:12 +00:00
|
|
|
} else {
|
2014-03-05 17:13:55 -05:00
|
|
|
throw err;
|
2014-02-09 23:07:12 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2014-06-02 17:34:13 -04:00
|
|
|
emitter.all(['templates:compiled', 'meta:js.compiled', 'meta:css.compiled'], function() {
|
|
|
|
|
winston.info('NodeBB Ready');
|
2014-06-02 17:40:08 -04:00
|
|
|
emitter.emit('nodebb:ready');
|
2014-08-25 10:13:01 -04:00
|
|
|
emitter.removeAllListeners('templates:compiled').removeAllListeners('meta:js.compiled').removeAllListeners('meta:css.compiled');
|
2014-06-02 17:34:13 -04:00
|
|
|
});
|
|
|
|
|
|
2014-09-04 21:22:34 -04:00
|
|
|
if (process.send) {
|
|
|
|
|
callback();
|
|
|
|
|
} else {
|
|
|
|
|
module.exports.listen();
|
|
|
|
|
}
|
2014-08-25 11:56:48 -04:00
|
|
|
};
|
|
|
|
|
|
2014-10-03 01:09:35 -04:00
|
|
|
module.exports.listen = function(callback) {
|
2014-08-25 11:56:48 -04:00
|
|
|
var bind_address = ((nconf.get('bind_address') === "0.0.0.0" || !nconf.get('bind_address')) ? '0.0.0.0' : nconf.get('bind_address')) + ':' + port;
|
|
|
|
|
winston.info('NodeBB attempting to listen on: ' + bind_address);
|
|
|
|
|
|
2014-09-04 20:07:55 -04:00
|
|
|
server.listen(port, nconf.get('bind_address'), function() {
|
2014-08-25 11:56:48 -04:00
|
|
|
winston.info('NodeBB is now listening on: ' + bind_address);
|
|
|
|
|
if (process.send) {
|
|
|
|
|
process.send({
|
|
|
|
|
action: 'listening',
|
2014-09-05 13:44:56 -04:00
|
|
|
bind_address: bind_address,
|
|
|
|
|
primary: process.env.handle_jobs === 'true'
|
2014-08-25 11:56:48 -04:00
|
|
|
});
|
|
|
|
|
}
|
2014-10-03 01:09:35 -04:00
|
|
|
|
|
|
|
|
if (typeof callback === 'function') {
|
|
|
|
|
callback();
|
|
|
|
|
}
|
2014-02-09 23:07:12 +00:00
|
|
|
});
|
2013-11-11 13:25:54 -05:00
|
|
|
};
|
2013-05-17 13:36:35 -04:00
|
|
|
|
2013-04-22 16:51:32 +00:00
|
|
|
}(WebServer));
|