| 
									
										
										
										
											2013-07-11 12:14:49 -04:00
										 |  |  | /* | 
					
						
							|  |  |  | 	NodeBB - A forum powered by node in development by designcreateplay | 
					
						
							|  |  |  | 	Copyright (C) 2013  DesignCreatePlay Inc. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | 	it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  | 	the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | 	(at your option) any later version. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | 	but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | 	GNU General Public License for more details. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | 	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-27 14:02:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var nconf = require('nconf'); | 
					
						
							|  |  |  | nconf.argv().env(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var fs = require('fs'), | 
					
						
							|  |  |  | 	os = require('os'), | 
					
						
							|  |  |  | 	semver = require('semver'), | 
					
						
							|  |  |  | 	winston = require('winston'), | 
					
						
							|  |  |  | 	path = require('path'), | 
					
						
							|  |  |  | 	pkg = require('./package.json'), | 
					
						
							|  |  |  | 	utils = require('./public/src/utils.js'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-13 14:45:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | global.env = process.env.NODE_ENV || 'production'; | 
					
						
							| 
									
										
										
										
											2013-07-11 12:10:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | winston.remove(winston.transports.Console); | 
					
						
							|  |  |  | winston.add(winston.transports.Console, { | 
					
						
							|  |  |  | 	colorize: true | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | winston.add(winston.transports.File, { | 
					
						
							|  |  |  | 	filename: 'error.log', | 
					
						
							|  |  |  | 	level: 'error' | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // TODO: remove once https://github.com/flatiron/winston/issues/280 is fixed
 | 
					
						
							|  |  |  | winston.err = function (err) { | 
					
						
							|  |  |  | 	winston.error(err.stack); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if(os.platform() === 'linux') { | 
					
						
							| 
									
										
										
										
											2013-12-07 16:51:47 -05:00
										 |  |  | 	require('child_process').exec('/usr/bin/which convert', function(err, stdout, stderr) { | 
					
						
							| 
									
										
										
										
											2013-12-07 17:03:07 -05:00
										 |  |  | 		if(err || !stdout) { | 
					
						
							| 
									
										
										
										
											2013-12-08 14:49:47 -05:00
										 |  |  | 			winston.warn('Couldn\'t find convert. Did you install imagemagick?'); | 
					
						
							| 
									
										
										
										
											2013-12-07 16:51:47 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-12-07 16:51:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | // Log GNU copyright info along with server info
 | 
					
						
							|  |  |  | winston.info('NodeBB v' + pkg.version + ' Copyright (C) 2013 DesignCreatePlay Inc.'); | 
					
						
							|  |  |  | winston.info('This program comes with ABSOLUTELY NO WARRANTY.'); | 
					
						
							|  |  |  | winston.info('This is free software, and you are welcome to redistribute it under certain conditions.'); | 
					
						
							|  |  |  | winston.info(''); | 
					
						
							| 
									
										
										
										
											2013-09-10 16:26:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-14 11:49:16 -05:00
										 |  |  | // Alternate configuration file support
 | 
					
						
							|  |  |  | var	configFile = __dirname + '/config.json', | 
					
						
							|  |  |  | 	configExists; | 
					
						
							|  |  |  | if (nconf.get('config')) { | 
					
						
							|  |  |  | 	configFile = path.join(__dirname, nconf.get('config')); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | configExists = fs.existsSync(configFile); | 
					
						
							| 
									
										
										
										
											2013-09-10 16:26:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-14 11:49:16 -05:00
										 |  |  | if (!nconf.get('help') && !nconf.get('setup') && !nconf.get('install') && !nconf.get('upgrade') && configExists) { | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | 	start(); | 
					
						
							| 
									
										
										
										
											2014-02-14 11:49:16 -05:00
										 |  |  | } else if (nconf.get('setup') || nconf.get('install') || !configExists) { | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | 	setup(); | 
					
						
							|  |  |  | } else if (nconf.get('upgrade')) { | 
					
						
							|  |  |  | 	upgrade(); | 
					
						
							|  |  |  | } else/* if (nconf.get('help') */{ | 
					
						
							|  |  |  | 	displayHelp(); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2013-09-17 15:38:50 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-19 13:31:04 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | function start() { | 
					
						
							|  |  |  | 	nconf.file({ | 
					
						
							|  |  |  | 		file: __dirname + '/config.json' | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2013-07-16 15:22:59 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-14 05:01:02 +00:00
										 |  |  | 	nconf.defaults({ | 
					
						
							|  |  |  | 		themes_dir: path.join(__dirname, 'node_modules') | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | 	nconf.set('url', nconf.get('base_url') + (nconf.get('use_port') ? ':' + nconf.get('port') : '') + nconf.get('relative_path')); | 
					
						
							|  |  |  | 	nconf.set('upload_url', path.join(path.sep, nconf.get('relative_path'), 'uploads', path.sep)); | 
					
						
							|  |  |  | 	nconf.set('base_dir', __dirname); | 
					
						
							| 
									
										
										
										
											2013-05-27 14:02:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-14 05:01:02 +00:00
										 |  |  | 	// Ensure themes_dir is a full filepath
 | 
					
						
							|  |  |  | 	nconf.set('themes_dir', path.resolve(__dirname, nconf.get('themes_dir'))); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | 	winston.info('Time: ' + new Date()); | 
					
						
							| 
									
										
										
										
											2014-02-14 05:03:38 +00:00
										 |  |  | 	winston.info('Initializing NodeBB v' + pkg.version); | 
					
						
							|  |  |  | 	winston.info('* using ' + nconf.get('database') +' store at ' + nconf.get(nconf.get('database') + ':host') + ':' + nconf.get(nconf.get('database') + ':port')); | 
					
						
							| 
									
										
										
										
											2014-02-14 05:04:08 +00:00
										 |  |  | 	winston.info('* using themes stored in: ' + nconf.get('themes_dir')); | 
					
						
							| 
									
										
										
										
											2014-01-05 23:57:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | 	if (process.env.NODE_ENV === 'development') { | 
					
						
							|  |  |  | 		winston.info('Base Configuration OK.'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var meta = require('./src/meta'); | 
					
						
							| 
									
										
										
										
											2013-12-03 14:21:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | 	require('./src/database').init(function(err) { | 
					
						
							|  |  |  | 		meta.configs.init(function () { | 
					
						
							| 
									
										
										
										
											2014-01-04 17:44:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | 			var templates = require('./public/src/templates'), | 
					
						
							|  |  |  | 				translator = require('./public/src/translator'), | 
					
						
							|  |  |  | 				webserver = require('./src/webserver'), | 
					
						
							|  |  |  | 				sockets = require('./src/socket.io'), | 
					
						
							|  |  |  | 				plugins = require('./src/plugins'), | 
					
						
							|  |  |  | 				notifications = require('./src/notifications'), | 
					
						
							|  |  |  | 				upgrade = require('./src/upgrade'); | 
					
						
							| 
									
										
										
										
											2014-01-11 16:15:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | 			templates.setGlobal('relative_path', nconf.get('relative_path')); | 
					
						
							| 
									
										
										
										
											2013-12-03 14:21:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | 			upgrade.check(function(schema_ok) { | 
					
						
							|  |  |  | 				if (schema_ok || nconf.get('check-schema') === false) { | 
					
						
							| 
									
										
										
										
											2013-12-03 14:21:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | 					sockets.init(webserver.server); | 
					
						
							| 
									
										
										
										
											2013-12-03 14:21:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | 					plugins.init(); | 
					
						
							| 
									
										
										
										
											2013-12-03 14:21:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | 					translator.loadServer(); | 
					
						
							| 
									
										
										
										
											2013-12-31 17:01:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-14 05:01:02 +00:00
										 |  |  | 					var customTemplates = meta.config['theme:templates'] ? path.join(nconf.get('themes_dir'), meta.config['theme:id'], meta.config['theme:templates']) : false; | 
					
						
							| 
									
										
										
										
											2013-12-31 17:01:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | 					utils.walk(path.join(__dirname, 'public/templates'), function (err, tplsToLoad) { | 
					
						
							|  |  |  | 						templates.init(tplsToLoad, customTemplates); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-12-03 14:21:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | 					plugins.ready(function() { | 
					
						
							|  |  |  | 						templates.ready(webserver.init); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					notifications.init(); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					winston.warn('Your NodeBB schema is out-of-date. Please run the following command to bring your dataset up to spec:'); | 
					
						
							|  |  |  | 					winston.warn('    node app --upgrade'); | 
					
						
							|  |  |  | 					winston.warn('To ignore this error (not recommended):'); | 
					
						
							|  |  |  | 					winston.warn('    node app --no-check-schema') | 
					
						
							|  |  |  | 					process.exit(); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-11-13 12:46:55 -05:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-09-17 15:38:50 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-05-27 14:02:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | function setup() { | 
					
						
							|  |  |  | 	if (nconf.get('setup')) { | 
					
						
							|  |  |  | 		winston.info('NodeBB Setup Triggered via Command Line'); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		winston.warn('Configuration not found, starting NodeBB setup'); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-11-03 11:53:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | 	nconf.file({ | 
					
						
							|  |  |  | 		file: __dirname + '/config.json' | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | 	var install = require('./src/install'); | 
					
						
							| 
									
										
										
										
											2013-09-10 16:26:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | 	winston.info('Welcome to NodeBB!'); | 
					
						
							|  |  |  | 	winston.info('This looks like a new installation, so you\'ll have to answer a few questions about your environment before we can proceed.'); | 
					
						
							|  |  |  | 	winston.info('Press enter to accept the default setting (shown in brackets).'); | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | 	install.setup(function (err) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			winston.error('There was a problem completing NodeBB setup: ', err.message); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			winston.info('NodeBB Setup Completed. Run \'./nodebb start\' to manually start your NodeBB server.'); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-10-28 15:24:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | 		process.exit(); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-10-26 10:56:05 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | function upgrade() { | 
					
						
							|  |  |  | 	nconf.file({ | 
					
						
							|  |  |  | 		file: __dirname + '/config.json' | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var meta = require('./src/meta'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	require('./src/database').init(function(err) { | 
					
						
							|  |  |  | 		meta.configs.init(function () { | 
					
						
							|  |  |  | 			require('./src/upgrade').upgrade(); | 
					
						
							| 
									
										
										
										
											2013-10-26 10:56:05 -06:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-02-02 01:38:46 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function displayHelp() { | 
					
						
							|  |  |  | 	winston.info('Usage: node app [options] [arguments]'); | 
					
						
							|  |  |  | 	winston.info('       [NODE_ENV=development | NODE_ENV=production] node app [--start] [arguments]'); | 
					
						
							|  |  |  | 	winston.info(''); | 
					
						
							|  |  |  | 	winston.info('Options:'); | 
					
						
							|  |  |  | 	winston.info('  --help              displays this usage information'); | 
					
						
							|  |  |  | 	winston.info('  --setup             configure your environment and setup NodeBB'); | 
					
						
							|  |  |  | 	winston.info('  --upgrade           upgrade NodeBB, first read: github.com/designcreateplay/NodeBB/wiki/Upgrading-NodeBB'); | 
					
						
							|  |  |  | 	winston.info('  --start             manually start NodeBB (default when no options are given)'); | 
					
						
							|  |  |  | } |