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-08-23 16:25:16 -04:00
// Configuration setup
nconf = require ( 'nconf' ) ;
nconf . argv ( ) . file ( { file : _ _dirname + '/config.json' } ) ;
2013-05-27 14:02:57 -04:00
var fs = require ( 'fs' ) ,
2013-08-13 14:45:28 -04:00
winston = require ( 'winston' ) ,
2013-07-16 13:30:48 -04:00
pkg = require ( './package.json' ) ,
2013-08-23 13:14:36 -04:00
url = require ( 'url' ) ,
meta = require ( './src/meta.js' ) ;
2013-06-05 11:26:50 -04:00
2013-06-21 13:28:55 -04:00
// Runtime environment
2013-09-04 12:13:36 -04:00
global . env = process . env . NODE _ENV || 'production' ;
2013-06-21 13:28:55 -04:00
2013-08-23 16:25:16 -04:00
2013-05-27 14:02:57 -04:00
2013-08-13 14:45:28 -04:00
winston . remove ( winston . transports . Console ) ;
winston . add ( winston . transports . Console , {
colorize : true
} ) ;
winston . add ( winston . transports . File , {
filename : 'error.log' ,
level : 'error'
2013-09-04 12:13:36 -04:00
} ) ;
2013-08-13 14:45:28 -04:00
// TODO: remove once https://github.com/flatiron/winston/issues/280 is fixed
winston . err = function ( err ) {
winston . error ( err . stack ) ;
} ;
2013-07-11 12:10:45 -04:00
// Log GNU copyright info along with server info
2013-08-13 14:45:28 -04:00
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-07-11 12:10:45 -04:00
2013-08-23 13:14:36 -04:00
2013-08-09 15:17:50 -04:00
if ( nconf . get ( 'upgrade' ) ) {
2013-08-23 13:14:36 -04:00
meta . configs . init ( function ( ) {
require ( './src/upgrade' ) . upgrade ( ) ;
} ) ;
2013-08-09 15:17:50 -04:00
} else if ( ! nconf . get ( 'setup' ) && nconf . get ( 'base_url' ) ) {
2013-07-19 23:56:30 -04:00
nconf . set ( 'url' , nconf . get ( 'base_url' ) + ( nconf . get ( 'use_port' ) ? ':' + nconf . get ( 'port' ) : '' ) + nconf . get ( 'relative_path' ) + '/' ) ;
2013-07-16 15:22:59 -04:00
nconf . set ( 'upload_url' , nconf . get ( 'url' ) + 'uploads/' ) ;
2013-08-19 13:31:04 -04:00
2013-08-13 14:45:28 -04:00
winston . info ( 'Initializing NodeBB v' + pkg . version + ', on port ' + nconf . get ( 'port' ) + ', using Redis store at ' + nconf . get ( 'redis:host' ) + ':' + nconf . get ( 'redis:port' ) + '.' ) ;
winston . info ( 'Base Configuration OK.' ) ;
2013-07-16 15:22:59 -04:00
2013-08-23 13:14:36 -04:00
meta . configs . init ( function ( ) {
2013-08-26 17:10:41 -04:00
// Initial setup for Redis & Reds
var reds = require ( 'reds' ) ;
RDB = require ( './src/redis.js' ) ;
reds . createClient = function ( ) {
return reds . client || ( reds . client = RDB ) ;
}
2013-05-27 14:02:57 -04:00
2013-07-16 15:22:59 -04:00
var categories = require ( './src/categories.js' ) ,
templates = require ( './public/src/templates.js' ) ,
webserver = require ( './src/webserver.js' ) ,
websockets = require ( './src/websockets.js' ) ,
2013-07-28 01:16:21 -04:00
plugins = require ( './src/plugins' ) ,
2013-07-16 15:22:59 -04:00
admin = {
'categories' : require ( './src/admin/categories.js' )
} ;
2013-05-27 14:02:57 -04:00
2013-07-16 15:22:59 -04:00
DEVELOPMENT = true ;
2013-08-26 16:37:22 -04:00
2013-07-16 15:22:59 -04:00
global . configuration = { } ;
global . templates = { } ;
2013-05-27 14:02:57 -04:00
2013-07-16 15:22:59 -04:00
( function ( config ) {
config [ 'ROOT_DIRECTORY' ] = _ _dirname ;
2013-05-27 14:02:57 -04:00
2013-07-16 15:22:59 -04:00
templates . init ( [
2013-07-23 03:07:27 +08:00
'header' , 'footer' , 'logout' , 'outgoing' , 'admin/header' , 'admin/footer' , 'admin/index' ,
2013-07-16 15:22:59 -04:00
'emails/reset' , 'emails/reset_plaintext' , 'emails/email_confirm' , 'emails/email_confirm_plaintext' ,
2013-08-26 16:37:22 -04:00
'emails/header' , 'emails/footer' ,
2013-06-06 15:34:12 -04:00
2013-07-16 15:22:59 -04:00
'noscript/header' , 'noscript/home' , 'noscript/category' , 'noscript/topic'
] ) ;
2013-05-27 14:02:57 -04:00
2013-07-17 01:02:28 -04:00
templates . ready ( webserver . init ) ;
2013-05-27 14:02:57 -04:00
2013-07-16 15:22:59 -04:00
//setup scripts to be moved outside of the app in future.
function setup _categories ( ) {
2013-08-13 14:45:28 -04:00
winston . info ( 'Checking categories...' ) ;
2013-07-16 15:22:59 -04:00
categories . getAllCategories ( function ( data ) {
if ( data . categories . length === 0 ) {
2013-08-13 14:45:28 -04:00
winston . info ( 'Setting up default categories...' ) ;
2013-05-27 14:02:57 -04:00
2013-07-16 15:22:59 -04:00
fs . readFile ( config . ROOT _DIRECTORY + '/install/data/categories.json' , function ( err , default _categories ) {
default _categories = JSON . parse ( default _categories ) ;
2013-05-27 14:02:57 -04:00
2013-07-16 15:22:59 -04:00
for ( var category in default _categories ) {
admin . categories . create ( default _categories [ category ] ) ;
}
} ) ;
2013-05-27 14:02:57 -04:00
2013-08-23 13:14:36 -04:00
2013-08-13 14:45:28 -04:00
winston . info ( 'Hardcoding uid 1 as an admin' ) ;
2013-07-16 15:22:59 -04:00
var user = require ( './src/user.js' ) ;
user . makeAdministrator ( 1 ) ;
2013-08-13 13:05:15 -04:00
2013-07-16 15:22:59 -04:00
} else {
2013-08-13 14:45:28 -04:00
winston . info ( 'Categories OK. Found ' + data . categories . length + ' categories.' ) ;
2013-07-16 15:22:59 -04:00
}
2013-05-27 14:02:57 -04:00
} ) ;
}
2013-08-13 13:05:15 -04:00
2013-07-16 15:22:59 -04:00
setup _categories ( ) ;
} ( global . configuration ) ) ;
} ) ;
2013-08-23 13:14:36 -04:00
2013-07-16 15:22:59 -04:00
} else {
// New install, ask setup questions
2013-08-13 14:45:28 -04:00
if ( nconf . get ( 'setup' ) ) winston . info ( 'NodeBB Setup Triggered via Command Line' ) ;
2013-09-03 22:21:56 -04:00
else winston . warn ( 'Configuration not found, starting NodeBB setup' ) ;
2013-07-16 15:22:59 -04:00
2013-07-16 16:12:09 -04:00
var install = require ( './src/install' ) ;
2013-07-16 15:22:59 -04:00
process . stdout . write (
"\nWelcome to NodeBB!\nThis looks like a new installation, so you'll have to answer a " +
"few questions about your environment before we can proceed.\n\n" +
2013-07-16 16:12:09 -04:00
"Press enter to accept the default setting (shown in brackets).\n\n\n"
2013-07-16 15:22:59 -04:00
) ;
2013-07-16 16:12:09 -04:00
install . setup ( function ( err ) {
if ( err ) {
2013-08-13 14:45:28 -04:00
winston . error ( 'There was a problem completing NodeBB setup: ' , err . message ) ;
2013-07-16 16:12:09 -04:00
} else {
if ( ! nconf . get ( 'setup' ) ) {
process . stdout . write (
"Please start NodeBB again and register a new user. This user will automatically become an administrator.\n\n"
) ;
}
}
2013-08-23 13:14:36 -04:00
2013-07-16 16:12:09 -04:00
process . exit ( ) ;
2013-07-16 15:22:59 -04:00
} ) ;
}