diff --git a/Changes.md b/Changes.md index 9acd2b9..b95c808 100644 --- a/Changes.md +++ b/Changes.md @@ -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 diff --git a/README.md b/README.md index 49d2374..d2a0515 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,6 @@ The `redis.ini` file has the following sections (defaults shown): ; host=127.0.0.1 ; port=6379 - ; db=0 ### [pubsub] @@ -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) diff --git a/config/redis.ini b/config/redis.ini new file mode 100644 index 0000000..fcad343 --- /dev/null +++ b/config/redis.ini @@ -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 \ No newline at end of file diff --git a/index.js b/index.js index b92a5a3..09cce48 100644 --- a/index.js +++ b/index.js @@ -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 .ini loaded? + if (!plugin.cfg) plugin.cfg = {}; // no .ini loaded? + if (!plugin.cfg.redis) plugin.cfg.redis = {}; // no [redis] in .ini file + if (!plugin.redisCfg) plugin.load_redis_ini(); - if (!plugin.cfg.redis) { // no [redis] in .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) { @@ -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) { @@ -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 () { @@ -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; } diff --git a/package.json b/package.json index f84b7ef..3d2960e 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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", diff --git a/test/config/redis.ini b/test/config/redis.ini new file mode 100644 index 0000000..f190a84 --- /dev/null +++ b/test/config/redis.ini @@ -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 \ No newline at end of file diff --git a/test/redis.js b/test/redis.js index f578bfa..185d454 100644 --- a/test/redis.js +++ b/test/redis.js @@ -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) { @@ -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({ @@ -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) {