diff --git a/app.js b/app.js index 256ddfc9d4..618c31aaad 100644 --- a/app.js +++ b/app.js @@ -18,9 +18,7 @@ // Read config.js to grab redis info var fs = require('fs'), - path = require('path'), nconf = require('nconf'), - utils = require('./public/src/utils.js'), pkg = require('./package.json'), url = require('url'); @@ -41,7 +39,7 @@ if (!nconf.get('setup') && nconf.get('base_url')) { nconf.set('upload_url', nconf.get('url') + 'uploads/'); global.nconf = nconf; - console.log('Info: Initializing NodeBB v' + pkg.version + ', on port ' + nconf.get('port') + ', using Redis store at ' + nconf.get('redis').host + ':' + nconf.get('redis').port + '.'); + console.log('Info: Initializing NodeBB v' + pkg.version + ', on port ' + nconf.get('port') + ', using Redis store at ' + nconf.get('redis:host') + ':' + nconf.get('redis:port') + '.'); console.log('Info: Base Configuration OK.'); // TODO: Replace this with nconf-redis @@ -115,99 +113,25 @@ if (!nconf.get('setup') && nconf.get('base_url')) { if (nconf.get('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()); - }); - } + var install = require('./src/install'); 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" + "Press enter to accept the default setting (shown in brackets).\n\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 urlObject = url.parse(base_url), - relative_path = urlObject.pathname, - host = urlObject.host, - protocol = urlObject.protocol; - - if(relative_path.length === 1) { - relative_path = ''; - } - - var fs = require('fs'), - path = require('path'), - config = { - secret: secret, - base_url: base_url, - relative_path: relative_path, - 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 (!nconf.get('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: protocol + '//' + host, - port: port - }, - api_url: protocol + '//' + host + (use_port ? ':' + port : '') + relative_path + '/api/', - relative_path: relative_path - }, null, 4)); - }); - }); - }); - }); - }); - }); - }); + install.setup(function(err) { + if (err) { + console.log('Error: There was a problem completing NodeBB setup: ', err.message); + } 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" + ); + } + } + + process.exit(); }); } \ No newline at end of file diff --git a/src/install.js b/src/install.js new file mode 100644 index 0000000000..24709cb5ab --- /dev/null +++ b/src/install.js @@ -0,0 +1,89 @@ +var async = require('async'), + utils = require('../public/src/utils.js'), + fs = require('fs'), + url = require('url'), + path = require('path'), + install = { + questions: [ + 'base_url|Publically accessible URL of this installation? (http://localhost)', + 'port|Port number of your install? (4567)', + 'use_port|Will you be using a port number to access NodeBB? (y)', + 'redis:host|Host IP or address of your Redis instance? (127.0.0.1)', + 'redis:port|Host port of your Redis instance? (6379)', + 'redis:password|Password of your Redis database? (no password)', + 'secret|Your NodeBB secret? (keyboard mash for a bit here)', + 'bcrypt_rounds|The number of rounds to use for bcrypt.genSalt? (10)' + ], + defaults: { + "base_url": 'http://localhost', + "port": 4567, + "use_port": true, + "redis:host": '127.0.0.1', + "redis:port": 6379, + "redis:password": '', + "secret": utils.generateUUID(), + "bcrypt_rounds": 10, + "upload_path": '/public/uploads' + }, + ask: function(question, callback) { + process.stdin.resume(); + process.stdout.write(question + ': '); + + process.stdin.once('data', function(data) { + callback(data.toString().trim()); + }); + }, + setup: function(callback) { + var config = {}; + for(d in install.defaults) config[d] = install.defaults[d]; + + async.eachSeries(install.questions, function(question, next) { + var question = question.split('|'); + install.ask(question[1], function(value) { + if (value !== '') config[question[0]] = value; + next(); + }); + }, function() { + var urlObject = url.parse(config.base_url), + relative_path = (urlObject.pathname && urlObject.pathname.length > 1) ? urlObject.pathname : '', + host = urlObject.host, + protocol = urlObject.protocol, + server_conf = config, + client_conf = { + socket: { + address: protocol + '//' + host, + port: config.port + }, + api_url: protocol + '//' + host + (config.use_port ? ':' + config.port : '') + relative_path + '/api/', + relative_path: relative_path + }; + + server_conf.relative_path = relative_path; + + install.save(server_conf, client_conf, callback); + }); + }, + save: function(server_conf, client_conf, callback) { + // Server Config + async.parallel([ + function(next) { + fs.writeFile(path.join(__dirname, '../', 'config.json'), JSON.stringify(server_conf, null, 4), function(err) { + next(err); + }); + }, + function(next) { + fs.writeFile(path.join(__dirname, '../', 'public', 'config.json'), JSON.stringify(client_conf, null, 4), function(err) { + next(err); + }); + } + ], function(err) { + process.stdout.write( + "\n\nConfiguration Saved OK\n\n" + ); + + callback(err); + }); + } + }; + +module.exports = install; \ No newline at end of file diff --git a/src/redis.js b/src/redis.js index 1661952cb1..54b0850cf9 100644 --- a/src/redis.js +++ b/src/redis.js @@ -2,10 +2,10 @@ var redis = require('redis'), utils = require('./../public/src/utils.js'); - RedisDB.exports = redis.createClient(global.nconf.get('redis').port, global.nconf.get('redis').host); + RedisDB.exports = redis.createClient(global.nconf.get('redis:port'), global.nconf.get('redis:host')); - if( global.nconf.get('redis').password ) { - RedisDB.exports.auth(global.nconf.get('redis').password); + if( global.nconf.get('redis:password') ) { + RedisDB.exports.auth(global.nconf.get('redis:password')); } RedisDB.exports.handle = function(error) { diff --git a/src/webserver.js b/src/webserver.js index eda01e1619..0ae65d7f60 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -5,7 +5,7 @@ var express = require('express'), RedisStore = require('connect-redis')(express), path = require('path'), redis = require('redis'), - redisServer = redis.createClient(global.nconf.get('redis').port, global.nconf.get('redis').host), + redisServer = redis.createClient(global.nconf.get('redis:port'), global.nconf.get('redis:host')), marked = require('marked'), utils = require('../public/src/utils.js'), pkg = require('../package.json'),