// Read config.js to grab redis info
var fs = require ( 'fs' ) ,
path = require ( 'path' ) ,
utils = require ( './public/src/utils.js' ) ,
ver = '0.0.2' ,
args = { } ;
// Runtime environment
global . env = process . env . NODE _ENV || 'production' ,
// Parse any passed-in arguments
process . argv . slice ( 2 ) . forEach ( function ( value ) {
if ( value . slice ( 0 , 2 ) === '--' ) {
var arg = value . slice ( 2 ) . split ( '=' ) ;
args [ arg [ 0 ] ] = arg [ 1 ] || true ;
}
} ) ;
// Log GNU copyright info along with server info
console . log ( 'Info: NodeBB v' + ver + ' Copyright (C) 2013 Design Create Play Inc.' ) ;
console . log ( 'Info: This program comes with ABSOLUTELY NO WARRANTY.' ) ;
console . log ( 'Info: This is free software, and you are welcome to redistribute it under certain conditions.' ) ;
console . log ( 'Info: ===' ) ;
fs . readFile ( path . join ( _ _dirname , 'config.json' ) , function ( err , data ) {
if ( ! err && args . setup !== true ) {
global . config = JSON . parse ( data ) ;
global . config . url = global . config . base _url + ( global . config . use _port ? ':' + global . config . port : '' ) + '/' ;
global . config . upload _url = global . config . url + 'uploads/' ;
console . log ( 'Info: Initializing NodeBB v' + ver + ', on port ' + global . config . port + ', using Redis store at ' + global . config . redis . host + ':' + global . config . redis . port + '.' ) ;
console . log ( 'Info: Base Configuration OK.' ) ;
var meta = require ( './src/meta.js' ) ;
meta . config . get ( function ( config ) {
for ( c in config ) {
if ( config . hasOwnProperty ( c ) ) {
global . config [ c ] = config [ c ] ;
}
}
var categories = require ( './src/categories.js' ) ,
RDB = require ( './src/redis.js' ) ,
templates = require ( './public/src/templates.js' ) ,
webserver = require ( './src/webserver.js' ) ,
websockets = require ( './src/websockets.js' ) ,
admin = {
'categories' : require ( './src/admin/categories.js' )
} ;
DEVELOPMENT = true ;
global . configuration = { } ;
global . templates = { } ;
( function ( config ) {
config [ 'ROOT_DIRECTORY' ] = _ _dirname ;
templates . init ( [
'header' , 'footer' , 'logout' , 'admin/header' , 'admin/footer' , 'admin/index' ,
'emails/reset' , 'emails/reset_plaintext' , 'emails/email_confirm' , 'emails/email_confirm_plaintext' ,
'emails/header' , 'emails/footer' , 'install/header' , 'install/footer' , 'install/redis' ,
'noscript/header' , 'noscript/home' , 'noscript/category' , 'noscript/topic'
] ) ;
templates . ready ( function ( ) {
webserver . init ( ) ;
} ) ;
//setup scripts to be moved outside of the app in future.
function setup _categories ( ) {
console . log ( 'Info: Checking categories...' ) ;
categories . getAllCategories ( function ( data ) {
if ( data . categories . length === 0 ) {
console . log ( 'Info: Setting up default categories...' ) ;
fs . readFile ( config . ROOT _DIRECTORY + '/install/data/categories.json' , function ( err , default _categories ) {
default _categories = JSON . parse ( default _categories ) ;
for ( var category in default _categories ) {
admin . categories . create ( default _categories [ category ] ) ;
}
} ) ;
// Hardcoding uid 1 as an admin
var user = require ( './src/user.js' ) ;
user . makeAdministrator ( 1 ) ;
} else {
console . log ( 'Info: Categories OK. Found ' + data . categories . length + ' categories.' ) ;
}
} ) ;
}
setup _categories ( ) ;
} ( global . configuration ) ) ;
} ) ;
} else {
// New install, ask setup questions
if ( args . setup ) console . log ( 'Info: NodeBB Setup Triggered via Command Line' ) ;
else console . log ( 'Info: Configuration not found, starting NodeBB setup' ) ;
var ask = function ( question , callback ) {
process . stdin . resume ( ) ;
process . stdout . write ( question + ': ' ) ;
process . stdin . once ( 'data' , function ( data ) {
callback ( data . toString ( ) . trim ( ) ) ;
} ) ;
}
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" +
"Press enter to accept the default setting (shown in brackets).\n\n\n" +
"What is...\n\n"
) ;
ask ( '... the publically accessible URL of this installation? (http://localhost)' , function ( base _url ) {
ask ( '... the port number of your install? (4567)' , function ( port ) {
ask ( 'Will you be using a port number to access NodeBB? (y)' , function ( use _port ) {
ask ( '... the host IP or address of your Redis instance? (127.0.0.1)' , function ( redis _host ) {
ask ( '... the host port of your Redis instance? (6379)' , function ( redis _port ) {
ask ( '... the password of your Redis database? (no password)' , function ( redis _password ) {
ask ( '... your NodeBB secret? (keyboard mash for a bit here)' , function ( secret ) {
ask ( '... the number of rounds to use for bcrypt.genSalt? (10)' , function ( bcrypt _rounds ) {
if ( ! base _url ) base _url = 'http://localhost' ;
if ( ! port ) port = 4567 ;
if ( ! use _port ) use _port = true ; else use _port = ( use _port === 'y' ? true : false ) ;
if ( ! redis _host ) redis _host = '127.0.0.1' ;
if ( ! redis _port ) redis _port = 6379 ;
if ( ! secret ) secret = utils . generateUUID ( ) ;
if ( ! bcrypt _rounds ) bcrypt _rounds = 10 ;
var fs = require ( 'fs' ) ,
path = require ( 'path' ) ,
config = {
secret : secret ,
base _url : base _url ,
port : port ,
use _port : use _port ,
upload _path : '/public/uploads/' ,
bcrypt _rounds : bcrypt _rounds ,
redis : {
host : redis _host ,
port : redis _port ,
password : redis _password
}
}
// Server-side config
fs . writeFile ( path . join ( _ _dirname , 'config.json' ) , JSON . stringify ( config , null , 4 ) , function ( err ) {
if ( err ) throw err ;
else {
process . stdout . write (
"\n\nConfiguration Saved OK\n\n"
) ;
if ( ! args . setup ) {
process . stdout . write (
"Please start NodeBB again and register a new user at " +
base _url + ( use _port ? ':' + port : '' ) + "/register. This user will automatically become an administrator.\n\n"
) ;
}
process . stdout . write (
"If at any time you'd like to run this setup again, run the app with the \"--setup\" flag\n\n"
) ;
process . exit ( ) ;
}
} ) ;
// Client-side config
fs . writeFile ( path . join ( _ _dirname , 'public' , 'config.json' ) , JSON . stringify ( {
socket : {
address : base _url ,
port : port
} ,
api _url : base _url + ( use _port ? ':' + port : '' ) + '/api/'
} , null , 4 ) ) ;
} ) ;
} ) ;
} ) ;
} ) ;
} ) ;
} ) ;
} ) ;
} ) ;
}
} ) ;