You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/webserver.js

159 lines
4.3 KiB
JavaScript

var path = require('path'),
fs = require('fs'),
11 years ago
nconf = require('nconf'),
express = require('express'),
WebServer = express(),
11 years ago
server,
winston = require('winston'),
async = require('async'),
cluster = require('cluster'),
emailer = require('./emailer'),
11 years ago
db = require('./database'),
auth = require('./routes/authentication'),
meta = require('./meta'),
user = require('./user'),
notifications = require('./notifications'),
logger = require('./logger'),
plugins = require('./plugins'),
middleware = require('./middleware'),
routes = require('./routes'),
emitter = require('./emitter'),
helpers = require('./../public/src/helpers')();
11 years ago
if(nconf.get('ssl')) {
server = require('https').createServer({
key: fs.readFileSync(nconf.get('ssl').key),
11 years ago
cert: fs.readFileSync(nconf.get('ssl').cert)
11 years ago
}, WebServer);
} else {
server = require('http').createServer(WebServer);
}
12 years ago
(function (app) {
"use strict";
var port = nconf.get('PORT') || nconf.get('port');
logger.init(app);
emailer.registerApp(app);
if (cluster.isWorker && process.env.handle_jobs === 'true') {
notifications.init();
user.startJobs();
}
// Preparation dependent on plugins
plugins.ready(function() {
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)
]);
});
11 years ago
async.parallel({
themesData: meta.themes.get,
11 years ago
currentThemeId: function(next) {
db.getObjectField('config', 'theme:id', next);
}
}, function(err, data) {
middleware = middleware(app, data);
routes(app, middleware);
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.');
}
}
});
// Cache static files on production
if (global.env !== 'development') {
app.enable('cache');
app.enable('minification');
// 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 {
11 years ago
fs.stat(path.join(__dirname, '../package.json'), function(err, stats) {
meta.config['cache-buster'] = new Date(stats.mtime).getTime();
});
}
});
}
if (port !== 80 && port !== 443 && nconf.get('use_port') === false) {
winston.info('Enabling \'trust proxy\'');
app.enable('trust proxy');
}
if ((port === 80 || port === 443) && process.env.NODE_ENV !== 'development') {
winston.info('Using ports 80 and 443 is not recommend; use a proxy instead. See README.md');
}
10 years ago
server.on('error', function(err) {
winston.error(err.stack);
console.log(err.stack);
if (err.code === 'EADDRINUSE') {
winston.error('NodeBB address in use, exiting...');
if (cluster.isWorker) {
cluster.worker.kill();
} else {
10 years ago
process.exit(0);
}
10 years ago
} else {
throw err;
}
});
module.exports.server = server;
emitter.all(['templates:compiled', 'meta:js.compiled', 'meta:css.compiled'], function() {
winston.info('NodeBB Ready');
emitter.emit('nodebb:ready');
});
11 years ago
module.exports.listen = function(callback) {
11 years ago
var bind_address = ((nconf.get('bind_address') === "0.0.0.0" || !nconf.get('bind_address')) ? '0.0.0.0' : nconf.get('bind_address')) + ':' + port;
if (cluster.isWorker) {
port = 0;
}
10 years ago
server.listen(port, nconf.get('bind_address'), function(err) {
if (err) {
winston.info('NodeBB was unable to listen on: ' + bind_address);
10 years ago
return callback(err);
}
11 years ago
winston.info('NodeBB is now listening on: ' + bind_address);
if (process.send) {
process.send({
action: 'listening',
bind_address: bind_address,
primary: process.env.handle_jobs === 'true'
11 years ago
});
}
10 years ago
callback();
});
};
process.on('message', function(message, connection) {
if (!message || message.action !== 'sticky-session:connection') {
return;
}
process.send({action: 'sticky-session:accept', handleIndex: message.handleIndex});
server.emit('connection', connection);
});
}(WebServer));