| 
									
										
										
										
											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'), | 
					
						
							| 
									
										
										
										
											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-01 17:35:47 -05:00
										 |  |  | 	auth.registerApp(app); | 
					
						
							| 
									
										
										
										
											2014-03-13 00:49:32 -04:00
										 |  |  | 	emailer.registerApp(app); | 
					
						
							| 
									
										
										
										
											2014-03-18 15:35:48 -04:00
										 |  |  | 	notifications.init(); | 
					
						
							|  |  |  | 	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() { | 
					
						
							|  |  |  | 		meta.js.minify(app.enabled('minification')); | 
					
						
							|  |  |  | 		meta.css.minify(); | 
					
						
							|  |  |  | 		meta.sounds.init(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2013-09-23 12:50:27 -04:00
										 |  |  | 	module.exports.init = function () { | 
					
						
							| 
									
										
										
										
											2014-03-05 17:13:55 -05:00
										 |  |  | 		server.on("error", function(err){ | 
					
						
							|  |  |  | 			if (err.code === 'EADDRINUSE') { | 
					
						
							| 
									
										
										
										
											2014-02-09 23:07:12 +00:00
										 |  |  | 				winston.error('NodeBB address in use, exiting...'); | 
					
						
							|  |  |  | 				process.exit(1); | 
					
						
							|  |  |  | 			} 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-06-02 17:34:13 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-17 12:53:31 -04:00
										 |  |  | 		emitter.on('templates:compiled', function() { | 
					
						
							| 
									
										
										
										
											2014-04-27 13:32:58 -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-03-17 12:53:31 -04:00
										 |  |  | 			server.listen(port, nconf.get('bind_address'), function(){ | 
					
						
							| 
									
										
										
										
											2014-06-02 17:34:13 -04:00
										 |  |  | 				winston.info('NodeBB is now listening on: ' + bind_address); | 
					
						
							| 
									
										
										
										
											2014-04-27 13:32:58 -04:00
										 |  |  | 				if (process.send) { | 
					
						
							|  |  |  | 					process.send({ | 
					
						
							|  |  |  | 						action: 'ready', | 
					
						
							|  |  |  | 						bind_address: bind_address | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-03-17 12:53:31 -04:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											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)); |