Skip to content

Commit

Permalink
Added ultimate.db.redis.getConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
pilwon committed Dec 2, 2013
1 parent d7d389d commit 447ede0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
39 changes: 24 additions & 15 deletions lib/db/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,8 @@ var debug = require('debug')('ultimate.db.redis');

var _client = null;

function createClient(redisConfig, onConnectCallback) {
var redisUrl = url.parse(process.env.REDISTOGO_URL || process.env.REDISCLOUD_URL || '');

if (redisUrl.href) {
redisConfig.host = redisUrl.hostname;
redisConfig.port = parseInt(redisUrl.port, 10);
redisConfig.password = redisUrl.auth.split(':')[1];
}
function createClient(defaultRedisConfig, onConnectCallback) {
var redisConfig = getConfig(defaultRedisConfig);

var client = redis.createClient(redisConfig.port, redisConfig.host);

Expand All @@ -46,17 +40,19 @@ function createClient(redisConfig, onConnectCallback) {
/**
* Connect to Redis.
*
* @param {Object} redisConfig Redis config.
* @param {Object} defaultRedisConfig Redis config.
* @param {Function} cb Callback.
* @return {Client}
*/
function connect(redisConfig, cb) {
function connect(defaultRedisConfig, cb) {
if (!_.isFunction(cb)) {
cb = function (err) {
if (err) { console.error(err); }
};
}

var redisConfig = getConfig(defaultRedisConfig);

if (_client && _client.connected) { return cb(); }

if (!_.isObject(redisConfig)) {
Expand All @@ -80,17 +76,30 @@ function connect(redisConfig, cb) {
return _client;
}

function publish(channel, message) {
_client.PUBLISH(channel, message);
}

// Accesssors
function getClient() {
return _client;
}

function getConfig(defaultConfig) {
if (!_.isPlainObject(defaultConfig)) { defaultConfig = {}; }

var redisUrl = url.parse(process.env.REDISTOGO_URL || process.env.REDISCLOUD_URL || '');

return {
host: redisUrl.href ? redisUrl.hostname : defaultConfig.host,
port: parseInt(redisUrl.href ? redisUrl.port : defaultConfig.port, 10),
password: redisUrl.href ? redisUrl.auth.split(':')[1] : defaultConfig.password,
namespace: defaultConfig.namespace
};
}

function publish(channel, message) {
_client.PUBLISH(channel, message);
}

// Public API
exports.getClient = getClient;
exports.getConfig = getConfig;
exports.createClient = createClient;
exports.connect = connect;
exports.publish = publish;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ultimate",
"description": "Dependency library for `ultimate-seed`.",
"version": "0.3.5",
"version": "0.3.6",
"license": "MIT",
"author": "Pilwon Huh <[email protected]>",
"contributors": [
Expand Down

0 comments on commit 447ede0

Please sign in to comment.