Skip to content

Commit

Permalink
apply all opts to [server] config
Browse files Browse the repository at this point in the history
fixes #18

fixes #19
  • Loading branch information
msimerson committed Apr 10, 2019
1 parent d3d8a04 commit 70fbd9a
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 55 deletions.
6 changes: 6 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

# 1.0.10 - 2019-04-09

- merge ALL of [opts] into [server] config (fixes #18)
- merge all of [opts] into [pubsub] config


# 1.0.9 - 2019-02-19

- bump redis version to 2.8.0
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ The `redis.ini` file has the following sections (defaults shown):

; host=127.0.0.1
; port=6379
; db=0

### [pubsub]

Expand All @@ -28,7 +27,9 @@ Publish & Subscribe are DB agnostic and thus have no db setting. If host and por

### [opts]

; see https://www.npmjs.com/package/redis#overloading
; see https://www.npmjs.com/package/redis#options-object-properties
; db=0
; password=battery-horse-staple


## Usage (shared redis)
Expand Down
12 changes: 12 additions & 0 deletions config/redis.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

; host=127.0.0.1
; port=6379
; db=0

[pubsub]
; host=127.0.0.1
; port=6379

[opts]
; db=0
; password=dontUseThisOne
81 changes: 32 additions & 49 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,32 @@ exports.register = function () {
plugin.register_hook('init_child', 'init_redis_shared');
}

const defaultOpts = { host: '127.0.0.1', port: '6379' };

exports.load_redis_ini = function () {
const plugin = this;

// store redis cfg at redisCfg, to avoid conflicting with plugins that
// inherit this plugin and have *their* config at plugin.cfg
plugin.redisCfg = plugin.config.get('redis.ini', function () {
plugin.load_redis_ini();
});

if (!plugin.redisCfg.server) plugin.redisCfg.server = {};
const s = plugin.redisCfg.server;
if (s.ip && !s.host) s.host = s.ip;
if (!s.host) s.host = '127.0.0.1';
if (!s.port) s.port = '6379';

if (!plugin.redisCfg.pubsub) {
plugin.redisCfg.pubsub = JSON.parse(JSON.stringify(s));
}
const ps = plugin.redisCfg.pubsub;
if (!ps.host) ps.host = s.host;
if (!ps.port) ps.port = s.port;
const rc = plugin.redisCfg;
plugin.redisCfg.server = Object.assign({}, defaultOpts, rc.opts, rc.server);
if (rc.server.ip && !rc.server.host) rc.server.host = rc.server.ip; // backwards compat

if (plugin.redisCfg.opts === undefined) plugin.redisCfg.opts = {};
Object.keys(plugin.redisCfg.opts).forEach(opt => {
if (ps[opt] === undefined) ps[opt] = plugin.redisCfg.opts[opt];
});
plugin.redisCfg.pubsub = Object.assign({}, defaultOpts, rc.opts, rc.server, rc.pubsub);
}

exports.merge_redis_ini = function () {
const plugin = this;

if (!plugin.cfg) plugin.cfg = {}; // no <plugin>.ini loaded?
if (!plugin.cfg) plugin.cfg = {}; // no <plugin>.ini loaded?
if (!plugin.cfg.redis) plugin.cfg.redis = {}; // no [redis] in <plugin>.ini file
if (!plugin.redisCfg) plugin.load_redis_ini();

if (!plugin.cfg.redis) { // no [redis] in <plugin>.ini file
plugin.cfg.redis = {};
}

if (!plugin.redisCfg) plugin.load_redis_ini();

['host', 'port', 'db'].forEach((k) => {
if (plugin.cfg.redis[k] !== undefined) return; // already set
plugin.cfg.redis[k] = plugin.redisCfg.server[k];
});
plugin.cfg.redis = Object.assign({}, plugin.redisCfg.server, plugin.cfg.redis);
}

exports.init_redis_shared = function (next, server) {
Expand All @@ -72,20 +57,17 @@ exports.init_redis_shared = function (next, server) {

// this is the server-wide redis, shared by plugins that don't
// specificy a db ID.
if (server.notes.redis) {
server.notes.redis.ping((err, res) => {
if (err) return nextOnce(err);

plugin.loginfo('already connected');
nextOnce(); // connection is good
});
}
else {
const opts = JSON.parse(JSON.stringify(plugin.redisCfg.opts));
opts.host = plugin.redisCfg.server.host;
opts.port = plugin.redisCfg.server.port;
server.notes.redis = plugin.get_redis_client(opts, nextOnce);
if (!server.notes.redis) {
server.notes.redis = plugin.get_redis_client(plugin.redisCfg.server, nextOnce);
return
}

server.notes.redis.ping((err, res) => {
if (err) return nextOnce(err);

plugin.loginfo('already connected');
nextOnce(); // connection is good
});
}

exports.init_redis_plugin = function (next, server) {
Expand All @@ -105,16 +87,17 @@ exports.init_redis_plugin = function (next, server) {
if (!plugin.cfg) plugin.cfg = { redis: {} };
if (!server) server = { notes: {} };

// use server-wide redis connection when using default DB id
if (!plugin.cfg.redis.db) {
if (server.notes.redis) {
server.loginfo(plugin, 'using server.notes.redis');
plugin.db = server.notes.redis;
return nextOnce();
}
if (plugin.cfg.redis.db !== undefined) {
plugin.db = plugin.get_redis_client(plugin.cfg.redis, nextOnce);
return;
}

plugin.db = plugin.get_redis_client(plugin.cfg.redis, nextOnce);
// use server-wide redis connection when DB not specified
if (server.notes.redis) {
server.loginfo(plugin, 'using server.notes.redis');
plugin.db = server.notes.redis;
nextOnce();
}
}

exports.shutdown = function () {
Expand Down Expand Up @@ -150,7 +133,7 @@ function getUriStr (client, opts) {
let msg = `redis://${opts.host}:${opts.port}`;
if (opts.db) msg += `/${opts.db}`;
if (client && client.server_info && client.server_info.redis_version) {
msg += ` v${client.server_info.redis_version}`;
msg += `\tv${client.server_info.redis_version}`;
}
return msg;
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "haraka-plugin-redis",
"version": "1.0.9",
"version": "1.0.10",
"description": "Redis plugin for Haraka & other plugins to inherit from",
"main": "index.js",
"directories": {
Expand All @@ -18,7 +18,7 @@
"scripts": {
"lint": "./node_modules/.bin/eslint *.js test/*.js",
"lintfix": "./node_modules/.bin/eslint --fix *.js test/*.js",
"test": "./run_tests"
"test": "node run_tests"
},
"repository": {
"type": "git",
Expand Down
12 changes: 12 additions & 0 deletions test/config/redis.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

; host=127.0.0.1
; port=6379
; db=0

[pubsub]
; host=127.0.0.1
; port=6379

[opts]
db=5
password=dontUseThisOne
28 changes: 26 additions & 2 deletions test/redis.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

const fixtures = require('haraka-test-fixtures');
const path = require('path');

const fixtures = require('haraka-test-fixtures');

function _set_up_redis (done) {

Expand Down Expand Up @@ -30,6 +32,28 @@ exports.redis = {
test.equal(this.plugin.redisCfg.server.port, 6379);
test.done();
},
'merges [opts] into server config': function (test) {
this.plugin.config = this.plugin.config.module_config(path.resolve('test'));
this.plugin.load_redis_ini();
test.expect(1);
test.deepEqual(this.plugin.redisCfg, {
main: {},
pubsub: {
host: '127.0.0.1',
port: '6379',
db: 5,
password: 'dontUseThisOne'
},
opts: { db: 5, password: 'dontUseThisOne' },
server: {
host: '127.0.0.1',
port: '6379',
db: 5,
password: 'dontUseThisOne'
}
});
test.done();
},
'connects' : function (test) {
test.expect(1);
const redis = this.plugin.get_redis_client({
Expand All @@ -46,7 +70,7 @@ exports.redis = {
test.expect(2);
test.equal(this.plugin.cfg, undefined);
this.plugin.merge_redis_ini();
test.deepEqual(this.plugin.cfg.redis, { host: '127.0.0.1', port: '6379', db: undefined });
test.deepEqual(this.plugin.cfg.redis, { host: '127.0.0.1', port: '6379' });
test.done();
},
'connects to a different redis db' : function (test) {
Expand Down

0 comments on commit 70fbd9a

Please sign in to comment.