@ -5,7 +5,7 @@ var path = require('path'),
fs = require ( 'fs' ) ,
nconf = require ( 'nconf' ) ,
express = require ( 'express' ) ,
WebServer = express ( ) ,
app = express ( ) ,
server ,
winston = require ( 'winston' ) ,
async = require ( 'async' ) ,
@ -18,41 +18,64 @@ var path = require('path'),
routes = require ( './routes' ) ,
emitter = require ( './emitter' ) ,
helpers = require ( './../public/src/modules/helpers' ) ,
net ;
helpers = require ( '../public/src/modules/helpers' ) ;
if ( nconf . get ( 'ssl' ) ) {
server = require ( 'https' ) . createServer ( {
key : fs . readFileSync ( nconf . get ( 'ssl' ) . key ) ,
cert : fs . readFileSync ( nconf . get ( 'ssl' ) . cert )
} , WebServer ) ;
} , app ) ;
} else {
server = require ( 'http' ) . createServer ( WebServer ) ;
server = require ( 'http' ) . createServer ( app ) ;
}
( function ( app ) {
var port = nconf . get ( 'port' ) ;
module . exports . server = server ;
if ( Array . isArray ( port ) ) {
if ( ! port . length ) {
winston . error ( '[startup] empty ports array in config.json' ) ;
process . exit ( ) ;
server . on ( 'error' , function ( err ) {
winston . error ( err ) ;
if ( err . code === 'EADDRINUSE' ) {
winston . error ( 'NodeBB address in use, exiting...' ) ;
process . exit ( 0 ) ;
} else {
throw err ;
}
} ) ;
winston . warn ( '[startup] If you want to start nodebb on multiple ports please use loader.js' ) ;
winston . warn ( '[startup] Defaulting to first port in array, ' + port [ 0 ] ) ;
port = port [ 0 ] ;
if ( ! port ) {
winston . error ( '[startup] Invalid port, exiting' ) ;
if ( server . setTimeout ) {
server . setTimeout ( 10000 ) ;
}
module . exports . listen = function ( ) {
emailer . registerApp ( app ) ;
middleware = middleware ( app ) ;
helpers . register ( ) ;
logger . init ( app ) ;
emitter . all ( [ 'templates:compiled' , 'meta:js.compiled' , 'meta:css.compiled' ] , function ( ) {
winston . info ( 'NodeBB Ready' ) ;
emitter . emit ( 'nodebb:ready' ) ;
listen ( ) ;
} ) ;
initializeNodeBB ( function ( err ) {
if ( err ) {
winston . error ( err ) ;
process . exit ( ) ;
}
if ( process . send ) {
process . send ( {
action : 'ready'
} ) ;
}
} ) ;
} ;
module . exports . init = function ( ) {
function initializeNodeBB ( callback ) {
var skipJS , skipLess , fromFile = nconf . get ( 'from-file' ) || '' ;
emailer . registerApp ( app ) ;
if ( fromFile . match ( 'js' ) ) {
winston . info ( '[minifier] Minifying client-side JS skipped' ) ;
skipJS = true ;
@ -63,34 +86,38 @@ if(nconf.get('ssl')) {
skipLess = true ;
}
// Preparation dependent on plugins
plugins . ready ( function ( ) {
async . waterfall ( [
async . apply ( cacheStaticFiles ) ,
async . apply ( meta . themes . setupPaths ) ,
function ( next ) {
plugins . init ( app , middleware , next ) ;
} ,
function ( next ) {
async . parallel ( [
async . apply ( meta . templates . compile ) ,
async . apply ( ! skipJS ? meta . js . minify : meta . js . getFromFile , app . enabled ( 'minification' ) ) ,
async . apply ( ! skipLess ? meta . css . minify : meta . css . getFromFile ) ,
async . apply ( meta . sounds . init )
] ) ;
] , next ) ;
} ,
function ( results , next ) {
plugins . fireHook ( 'static:app.preload' , {
app : app ,
middleware : middleware
} , function ( err ) {
if ( err ) {
return winston . error ( '[plugins] Encountered error while executing pre-router plugins hooks: ' + err . message ) ;
}
} , next ) ;
} ,
function ( results , next ) {
routes ( app , middleware ) ;
} ) ;
} ) ;
middleware = middleware ( app ) ;
plugins . init ( app , middleware ) ;
next ( ) ;
}
] , callback ) ;
}
// Load server-side template helpers
helpers . register ( ) ;
function cacheStaticFiles ( callback ) {
if ( global . env === 'development' ) {
return callback ( ) ;
}
// Cache static files on production
if ( global . env !== 'development' ) {
app . enable ( 'cache' ) ;
app . enable ( 'minification' ) ;
@ -100,14 +127,37 @@ if(nconf.get('ssl')) {
} , function ( err , stdOut ) {
if ( ! err ) {
meta . config [ 'cache-buster' ] = stdOut . trim ( ) ;
callback ( ) ;
} else {
fs . stat ( path . join ( _ _dirname , '../package.json' ) , function ( err , stats ) {
if ( err ) {
return callback ( err ) ;
}
meta . config [ 'cache-buster' ] = new Date ( stats . mtime ) . getTime ( ) ;
callback ( ) ;
} ) ;
}
} ) ;
}
function listen ( callback ) {
var port = nconf . get ( 'port' ) ;
if ( Array . isArray ( port ) ) {
if ( ! port . length ) {
winston . error ( '[startup] empty ports array in config.json' ) ;
process . exit ( ) ;
}
winston . warn ( '[startup] If you want to start nodebb on multiple ports please use loader.js' ) ;
winston . warn ( '[startup] Defaulting to first port in array, ' + port [ 0 ] ) ;
port = port [ 0 ] ;
if ( ! port ) {
winston . error ( '[startup] Invalid port, exiting' ) ;
process . exit ( ) ;
}
}
if ( port !== 80 && port !== 443 && nconf . get ( 'use_port' ) === false ) {
winston . info ( 'Enabling \'trust proxy\'' ) ;
app . enable ( 'trust proxy' ) ;
@ -116,30 +166,6 @@ if(nconf.get('ssl')) {
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' ) ;
}
} ;
server . on ( 'error' , function ( err ) {
winston . error ( err . stack ) ;
console . log ( err . stack ) ;
if ( err . code === 'EADDRINUSE' ) {
winston . error ( 'NodeBB address in use, exiting...' ) ;
process . exit ( 0 ) ;
} 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' ) ;
} ) ;
server . setTimout && server . setTimeout ( 10000 ) ;
module . exports . listen = function ( ) {
logger . init ( app ) ;
var isSocket = isNaN ( port ) ,
args = isSocket ? [ port ] : [ port , nconf . get ( 'bind_address' ) ] ,
@ -161,12 +187,9 @@ if(nconf.get('ssl')) {
// Alter umask if necessary
if ( isSocket ) {
oldUmask = process . umask ( '0000' ) ;
net = require ( 'net' ) ;
module . exports . testSocket ( port , function ( err ) {
if ( ! err ) {
emitter . on ( 'nodebb:ready' , function ( ) {
server . listen . apply ( server , args ) ;
} ) ;
} else {
winston . error ( '[startup] NodeBB was unable to secure domain socket access (' + port + ')' ) ;
winston . error ( '[startup] ' + err . message ) ;
@ -174,17 +197,15 @@ if(nconf.get('ssl')) {
}
} ) ;
} else {
emitter . on ( 'nodebb:ready' , function ( ) {
server . listen . apply ( server , args ) ;
} ) ;
}
} ;
}
module . exports . testSocket = function ( socketPath , callback ) {
if ( typeof socketPath !== 'string' ) {
return callback ( new Error ( 'invalid socket path : ' + socketPath ) ) ;
}
var net = require ( 'net' ) ;
async . series ( [
function ( next ) {
fs . exists ( socketPath , function ( exists ) {
@ -209,4 +230,4 @@ if(nconf.get('ssl')) {
] , callback ) ;
} ;
} ( WebServer ) ) ;