Created
July 21, 2012 17:15
-
-
Save ox/3156461 to your computer and use it in GitHub Desktop.
Config that Node for different environments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'), | |
http = require('http'), | |
redis = require('redis'), | |
argv = require('optimist').default('env', 'default').options('c', { 'alias' : 'config' | |
, 'default' : './default_config.json' | |
}).argv | |
, config = require(argv.config); | |
var app = express(); | |
var server = http.createServer(app); | |
var io = require('socket.io').listen(server); | |
// Sanity Checks | |
if(!config.redis) { // you don't even have redis settings | |
console.log("!!! ERROR: No redis settings found. Don't be a noob. !!!"); | |
return; | |
} else if(!config.redis['default']) { // you don't even have a default. YOU NOOB! | |
console.log("!!! ERROR: You don't even have a redis.default hash. Who do you think you are?"); | |
return; | |
} | |
// Well at least you made it this far | |
if(argv.env !== 'default' && config.redis[argv.env]) { // set an env and you have it | |
config.redis = config.redis[argv.env]; | |
} else { // just use default | |
if(argv.env !== 'default') { | |
console.log("!!! WARNING: cannot find env `redis." + argv.env + "` in ./default_config.json, using `redis.default`"); | |
} | |
config.redis = config.redis['default']; | |
} | |
// C-C-C-C-C-COMBO BREAKER, you done goof'd. | |
if(!config.redis.port || !config.redis.host) { | |
console.log("!!! ERROR: No `port` of `host` keys found for the environment " + argv.env); | |
return; | |
} | |
// CONFIG | |
var client = redis.createClient(config.redis.port, config.redis.host, {}) | |
, listener = redis.createClient(config.redis.port, config.redis.host, {}); | |
// Password protected database? No problem. | |
if (config.redis.password) { | |
client.auth(config.redis.password); | |
listener.auth(config.redis.password); | |
} | |
// Different database on the same instance? We got you covered; | |
if(config.redis.database) { | |
client.select(config.redis.database, function(){}); | |
listener.select(config.redis.database, function(){}); | |
} | |
// Error Handling for Redis | |
client.on('error', function(err){ | |
console.trace('!!! ERROR: ' + err); | |
}); | |
listener.on('error', function(err){ | |
console.trace('!!! ERROR: ' + err); | |
}); | |
// Lets start the party! | |
server.listen(config.general.port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment