From e32494879c680cde6102a0c3adaa8d8531d47944 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Tue, 10 Sep 2013 16:26:26 -0400 Subject: [PATCH] closed #270 --- app.js | 22 +++++++++++++++------ src/install.js | 53 ++++++++++++++++++++++++++++++++++---------------- src/redis.js | 1 - 3 files changed, 52 insertions(+), 24 deletions(-) diff --git a/app.js b/app.js index 73cb0c3efa..d79c33d9d2 100644 --- a/app.js +++ b/app.js @@ -18,13 +18,12 @@ // Configuration setup nconf = require('nconf'); -nconf.argv().file({ file: __dirname + '/config.json'}); +nconf.argv(); var fs = require('fs'), winston = require('winston'), pkg = require('./package.json'), - url = require('url'), - meta = require('./src/meta.js'); + url = require('url'); // Runtime environment global.env = process.env.NODE_ENV || 'production'; @@ -52,7 +51,12 @@ 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(''); -if (!nconf.get('setup') && !nconf.get('upgrade') && nconf.get('base_url')) { +if (!fs.existsSync(__dirname + '/config.json') || (!nconf.get('setup') && !nconf.get('upgrade'))) { + // Load server-side config + nconf.file({ file: __dirname + '/config.json'}); + + var meta = require('./src/meta.js'); + nconf.set('url', nconf.get('base_url') + (nconf.get('use_port') ? ':' + nconf.get('port') : '') + nconf.get('relative_path') + '/'); nconf.set('upload_url', nconf.get('url') + 'uploads/'); @@ -89,6 +93,10 @@ if (!nconf.get('setup') && !nconf.get('upgrade') && nconf.get('base_url')) { }); } else if (nconf.get('upgrade')) { + var meta = require('./src/meta.js'); + + nconf.file({ file: __dirname + '/config.json'}); + meta.configs.init(function() { require('./src/upgrade').upgrade(); }); @@ -97,8 +105,10 @@ if (!nconf.get('setup') && !nconf.get('upgrade') && nconf.get('base_url')) { if (nconf.get('setup')) winston.info('NodeBB Setup Triggered via Command Line'); else winston.warn('Configuration not found, starting NodeBB setup'); - meta.config = {}; - var install = require('./src/install'); + var install = require('./src/install'), + meta = { + config: {} + }; 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.'); diff --git a/src/install.js b/src/install.js index 90dc170a50..6d9a7afd3b 100644 --- a/src/install.js +++ b/src/install.js @@ -3,16 +3,9 @@ var async = require('async'), fs = require('fs'), url = require('url'), path = require('path'), - meta = require('./meta'), - User = require('./user'), - Groups = require('./groups'), - Categories = require('./categories'), - Plugins = require('./plugins'), prompt = require('prompt'), - admin = { - categories: require('./admin/categories') - }, winston = require('winston'), + reds = require('reds'), install = { questions: [ @@ -97,21 +90,29 @@ var async = require('async'), server_conf.base_url = protocol + '//' + host; server_conf.relative_path = relative_path; - meta.configs.set('postDelay', 10000); - meta.configs.set('minimumPostLength', 8); - meta.configs.set('minimumTitleLength', 3); - meta.configs.set('minimumUsernameLength', 2); - meta.configs.set('maximumUsernameLength', 16); - meta.configs.set('minimumPasswordLength', 6); - meta.configs.set('imgurClientID', ''); - install.save(server_conf, client_conf, next); }); }, + function(next) { + // Applying default database configs + var meta = require('./meta'); + + meta.configs.set('postDelay', 10000); + meta.configs.set('minimumPostLength', 8); + meta.configs.set('minimumTitleLength', 3); + meta.configs.set('minimumUsernameLength', 2); + meta.configs.set('maximumUsernameLength', 16); + meta.configs.set('minimumPasswordLength', 6); + meta.configs.set('imgurClientID', ''); + + meta.configs.init(next); + }, function(next) { // Check if an administrator needs to be created + var Groups = require('./groups'); + Groups.getGidFromName('Administrators', function(err, gid) { - if (err) return next(new Error('Could not save configs')); + if (err) return next(err.message); if (gid) { Groups.get(gid, {}, function(err, groupObj) { @@ -125,6 +126,11 @@ var async = require('async'), }, function(next) { // Categories + var Categories = require('./categories'), + admin = { + categories: require('./admin/categories') + }; + categories.getAllCategories(function(data) { if (data.categories.length === 0) { winston.warn('No categories found, populating instance with default categories') @@ -147,6 +153,8 @@ var async = require('async'), }, function(next) { // Default plugins + var Plugins = require('./plugins'); + winston.info('Enabling default plugins'); var defaultEnabled = [ @@ -171,6 +179,9 @@ var async = require('async'), }); }, createAdmin: function(callback) { + var User = require('./user'), + Groups = require('./groups'); + winston.warn('No administrators have been detected, running initial user setup'); var questions = [ { @@ -233,6 +244,14 @@ var async = require('async'), } ], function(err) { winston.info('Configuration Saved OK'); + + nconf.file({ file: path.join(__dirname, '..', 'config.json') }); + + var RDB = require('./redis'); + reds.createClient = function() { + return reds.client || (reds.client = RDB); + } + callback(err); }); } diff --git a/src/redis.js b/src/redis.js index 1d8d41bf2e..6f80a2c5c7 100644 --- a/src/redis.js +++ b/src/redis.js @@ -1,6 +1,5 @@ (function(RedisDB) { var redis = require('redis'), - nconf = require('nconf'), utils = require('./../public/src/utils.js'), winston = require('winston');