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-07-10 16:22:03 -04:00
express _namespace = require ( 'express-namespace' ) ,
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' ) ,
2013-11-18 02:39:08 -06:00
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' ) ,
plugins = require ( './plugins' ) ,
2014-01-25 16:39:27 -05:00
logger = require ( './logger' ) ,
2014-03-01 17:26:26 -05:00
middleware = require ( './middleware' ) ,
2014-03-05 17:06:24 -05:00
routes = require ( './routes' ) ;
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-01 17:35:47 -05:00
auth . registerApp ( app ) ;
2014-03-02 13:56:46 -05:00
2014-03-01 17:26:26 -05:00
async . series ( {
themesData : meta . themes . get ,
currentThemeData : function ( next ) {
db . getObjectFields ( 'config' , [ 'theme:type' , 'theme:id' , 'theme:staticDir' , 'theme:templates' ] , next ) ;
}
} , 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 ;
2013-09-23 12:50:27 -04:00
module . exports . init = function ( ) {
2014-01-23 17:08:33 -05:00
plugins . fireHook ( 'action:app.load' , app ) ;
2013-11-22 11:42:42 -05:00
2014-02-09 23:07:12 +00:00
server . on ( "error" , function ( e ) {
if ( e . code === 'EADDRINUSE' ) {
winston . error ( 'NodeBB address in use, exiting...' ) ;
process . exit ( 1 ) ;
} else {
throw e ;
}
} ) ;
winston . info ( 'NodeBB attempting to listen on: ' + ( ( nconf . get ( 'bind_address' ) === "0.0.0.0" || ! nconf . get ( 'bind_address' ) ) ? '0.0.0.0' : nconf . get ( 'bind_address' ) ) + ':' + port ) ;
server . listen ( port , nconf . get ( 'bind_address' ) , function ( ) {
winston . info ( 'NodeBB Ready' ) ;
} ) ;
2013-11-11 13:25:54 -05:00
} ;
2013-04-22 16:51:32 +00:00
} ( WebServer ) ) ;