using nconf to manage config file now (issue #24)

v1.18.x
Julian Lam 12 years ago
parent 41b3eabf1a
commit 145898c3ba

@ -19,21 +19,16 @@
// 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'),
args = {};
url = require('url');
// 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;
}
});
// Configuration setup
nconf.argv().file({ file: __dirname + '/config.json'});
// Log GNU copyright info along with server info
console.log('Info: NodeBB v' + pkg.version + ' Copyright (C) 2013 DesignCreatePlay Inc.');
@ -41,16 +36,17 @@ 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/';
if (!nconf.get('setup') && nconf.get('base_url')) {
nconf.set('url', nconf.get('base_url') + (nconf.get('use_port') ? ':' + nconf.get('port') : '') + '/');
nconf.set('upload_url', nconf.get('url') + 'uploads/');
global.nconf = nconf;
console.log('Info: Initializing NodeBB v' + pkg.version + ', on port ' + global.config.port + ', using Redis store at ' + global.config.redis.host + ':' + global.config.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
var meta = require('./src/meta.js');
global.config = {};
meta.config.get(function(config) {
for(c in config) {
if (config.hasOwnProperty(c)) {
@ -98,7 +94,6 @@ fs.readFile(path.join(__dirname, 'config.json'), function(err, data) {
default_categories = JSON.parse(default_categories);
for (var category in default_categories) {
console.log(category);
admin.categories.create(default_categories[category]);
}
});
@ -117,7 +112,7 @@ fs.readFile(path.join(__dirname, 'config.json'), function(err, data) {
});
} else {
// New install, ask setup questions
if (args.setup) console.log('Info: NodeBB Setup Triggered via Command Line');
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) {
@ -185,7 +180,7 @@ fs.readFile(path.join(__dirname, 'config.json'), function(err, data) {
process.stdout.write(
"\n\nConfiguration Saved OK\n\n"
);
if (!args.setup) {
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"
@ -216,4 +211,3 @@ fs.readFile(path.join(__dirname, 'config.json'), function(err, data) {
});
});
}
});

@ -52,7 +52,8 @@
"async": "0.2.8",
"node-imagemagick": "0.1.8",
"node-rss": "1.0.1",
"gravatar": "1.0.6"
"gravatar": "1.0.6",
"nconf": "~0.6.7"
},
"bugs": {
"url": "https://github.com/designcreateplay/NodeBB/issues"

@ -181,7 +181,7 @@
if (!templates[tpl_url] || !template_data) return;
if(typeof global !== "undefined")
template_data['relative_path'] = global.config.relative_path;
template_data['relative_path'] = global.nconf.get('relative_path');
else
template_data['relative_path'] = RELATIVE_PATH;

@ -85,7 +85,8 @@
// from http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
isEmailValid: function(email) {
var re = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
// var re = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
var valid = email.indexOf('@') !== -1 ? true : false;
return re.test(email);
},

@ -32,9 +32,9 @@ var RDB = require('./redis.js'),
'category_id': category_id,
'active_users': [],
'topics' : [],
'twitter-intent-url': 'https://twitter.com/intent/tweet?url=' + encodeURIComponent(global.config.url + 'category/' + category_slug) + '&text=' + encodeURIComponent(category_name),
'facebook-share-url': 'https://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(global.config.url + 'category/' + category_slug),
'google-share-url': 'https://plus.google.com/share?url=' + encodeURIComponent(global.config.url + 'category/' + category_slug)
'twitter-intent-url': 'https://twitter.com/intent/tweet?url=' + encodeURIComponent(global.nconf.get('url') + 'category/' + category_slug) + '&text=' + encodeURIComponent(category_name),
'facebook-share-url': 'https://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(global.nconf.get('url') + 'category/' + category_slug),
'google-share-url': 'https://plus.google.com/share?url=' + encodeURIComponent(global.nconf.get('url') + 'category/' + category_slug)
};
function getTopics(next) {

@ -38,7 +38,7 @@ marked.setOptions({
post.username = userData.username || 'anonymous';
post.userslug = userData.userslug || '';
post.user_rep = userData.reputation || 0;
post.picture = userData.picture || require('gravatar').url('', {}, https=global.config.https);
post.picture = userData.picture || require('gravatar').url('', {}, https=global.nconf.get('https'));
post.signature = marked(userData.signature || '');
if(post.editor !== '') {

@ -2,10 +2,10 @@
var redis = require('redis'),
utils = require('./../public/src/utils.js');
RedisDB.exports = redis.createClient(global.config.redis.port, global.config.redis.host);
RedisDB.exports = redis.createClient(global.nconf.get('redis').port, global.nconf.get('redis').host);
if( global.config.redis.password ) {
RedisDB.exports.auth(global.config.redis.password);
if( global.nconf.get('redis').password ) {
RedisDB.exports.auth(global.nconf.get('redis').password);
}
RedisDB.exports.handle = function(error) {

@ -14,9 +14,9 @@ var user = require('./../user.js'),
Admin.build_header = function(res) {
return templates['admin/header'].parse({
cssSrc: global.config['theme:src'] || global.config.relative_path + '/vendor/bootstrap/css/bootstrap.min.css',
cssSrc: global.config['theme:src'] || global.nconf.get('relative_path') + '/vendor/bootstrap/css/bootstrap.min.css',
csrf:res.locals.csrf_token,
relative_path: global.config.relative_path
relative_path: global.nconf.get('relative_path')
});
}

@ -140,10 +140,10 @@
req.login({
uid: uid
}, function() {
res.redirect(global.config.relative_path + '/');
res.redirect(global.nconf.get('relative_path') + '/');
});
} else {
res.redirect(global.config.relative_path + '/register');
res.redirect(global.nconf.get('relative_path') + '/register');
}
});
});

@ -350,7 +350,7 @@ var utils = require('./../public/src/utils.js'),
options.forcedefault = 'y';
}
return require('gravatar').url(email, options, https=global.config.https);
return require('gravatar').url(email, options, https=global.nconf.get('https'));
}
User.hashPassword = function(password, callback) {
@ -560,7 +560,7 @@ var utils = require('./../public/src/utils.js'),
topics.getTopicField(tid, 'slug', function(slug) {
var message = username + ' made a new post';
notifications.create(message, 5, global.config.url + 'topic/' + slug + '#' + pid, 'notification_'+ Date.now(), function(nid) {
notifications.create(message, 5, global.nconf.get('url') + 'topic/' + slug + '#' + pid, 'notification_'+ Date.now(), function(nid) {
notifications.push(nid, followers);
});
});

@ -5,7 +5,7 @@ var express = require('express'),
RedisStore = require('connect-redis')(express),
path = require('path'),
redis = require('redis'),
redisServer = redis.createClient(global.config.redis.port, global.config.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'),
@ -28,10 +28,10 @@ var express = require('express'),
app.build_header = function(res) {
return templates['header'].parse({
cssSrc: global.config['theme:src'] || global.config.relative_path + '/vendor/bootstrap/css/bootstrap.min.css',
cssSrc: global.config['theme:src'] || global.nconf.get('relative_path') + '/vendor/bootstrap/css/bootstrap.min.css',
title: global.config['title'] || 'NodeBB',
csrf:res.locals.csrf_token,
relative_path: global.config.relative_path
relative_path: global.nconf.get('relative_path')
});
};
@ -39,7 +39,7 @@ var express = require('express'),
app.use(express.favicon(path.join(__dirname, '../', 'public', 'favicon.ico')));
app.use(require('less-middleware')({ src: path.join(__dirname, '../', 'public') }));
//app.use(express.static(path.join(__dirname, '../', 'public')));
app.use(global.config.relative_path, express.static(path.join(__dirname, '../', 'public')));
app.use(global.nconf.get('relative_path'), express.static(path.join(__dirname, '../', 'public')));
app.use(express.bodyParser()); // Puts POST vars in request.body
app.use(express.cookieParser()); // If you want to parse cookies (res.cookies)
app.use(express.compress());
@ -48,7 +48,7 @@ var express = require('express'),
client: redisServer,
ttl: 60*60*24*14
}),
secret: global.config.secret,
secret: global.nconf.get('secret'),
key: 'express.sid'
}));
app.use(express.csrf());
@ -65,7 +65,7 @@ var express = require('express'),
app.use(function(req, res, next) {
global.config.https = req.secure;
global.nconf.set('https', req.secure);
// Don't bother with session handling for API requests
if (/^\/api\//.test(req.url)) return next();
@ -89,7 +89,7 @@ var express = require('express'),
if (req.accepts('html')) {
//res.json('404', { url: req.url });
res.redirect(global.config.relative_path + '/404');
res.redirect(global.nconf.get('relative_path') + '/404');
return;
}
@ -121,7 +121,7 @@ var express = require('express'),
};
app.namespace(global.config.relative_path, function() {
app.namespace(global.nconf.get('relative_path'), function() {
auth.create_routes(app);
admin.create_routes(app);
@ -414,5 +414,5 @@ var express = require('express'),
}(WebServer));
server.listen(config.port);
server.listen(nconf.get('port'));
global.server = server;

@ -30,7 +30,7 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
io.set('authorization', function(handshakeData, accept) {
if (handshakeData.headers.cookie) {
handshakeData.cookie = cookie.parse(handshakeData.headers.cookie);
handshakeData.sessionID = connect.utils.parseSignedCookie(handshakeData.cookie['express.sid'], global.config.secret);
handshakeData.sessionID = connect.utils.parseSignedCookie(handshakeData.cookie['express.sid'], global.nconf.get('secret'));
if (handshakeData.cookie['express.sid'] == handshakeData.sessionID) {
return accept('Cookie is invalid.', false);
@ -196,7 +196,7 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
uid:0,
username: "Anonymous User",
email: '',
picture: require('gravatar').url('', {s:'24'}, https=global.config.https)
picture: require('gravatar').url('', {s:'24'}, https=global.nconf.get('https'))
});
}

Loading…
Cancel
Save