fix: redis pubsub not being required correctly

split connection logic into separate module
This commit is contained in:
Barış Soner Uşaklı
2019-08-06 18:27:00 -04:00
parent 186321e646
commit 8d4f20865f
9 changed files with 239 additions and 213 deletions

View File

@@ -0,0 +1,67 @@
'use strict';
const nconf = require('nconf');
const winston = require('winston');
const _ = require('lodash');
const connection = module.exports;
connection.getConnectionString = function (mongo) {
mongo = mongo || nconf.get('mongo');
var usernamePassword = '';
var uri = mongo.uri || '';
if (mongo.username && mongo.password) {
usernamePassword = nconf.get('mongo:username') + ':' + encodeURIComponent(nconf.get('mongo:password')) + '@';
} else if (!uri.includes('@') || !uri.slice(uri.indexOf('://') + 3, uri.indexOf('@'))) {
winston.warn('You have no mongo username/password setup!');
}
// Sensible defaults for Mongo, if not set
if (!mongo.host) {
mongo.host = '127.0.0.1';
}
if (!mongo.port) {
mongo.port = 27017;
}
const dbName = mongo.database;
if (dbName === undefined || dbName === '') {
winston.warn('You have no database name, using "nodebb"');
mongo.database = 'nodebb';
}
var hosts = mongo.host.split(',');
var ports = mongo.port.toString().split(',');
var servers = [];
for (var i = 0; i < hosts.length; i += 1) {
servers.push(hosts[i] + ':' + ports[i]);
}
return uri || 'mongodb://' + usernamePassword + servers.join() + '/' + mongo.database;
};
connection.getConnectionOptions = function (mongo) {
mongo = mongo || nconf.get('mongo');
var connOptions = {
poolSize: 10,
reconnectTries: 3600,
reconnectInterval: 1000,
autoReconnect: true,
connectTimeoutMS: 90000,
useNewUrlParser: true,
};
return _.merge(connOptions, mongo.options || {});
};
connection.connect = function (options, callback) {
callback = callback || function () { };
const mongoClient = require('mongodb').MongoClient;
const connString = connection.getConnectionString(options);
const connOptions = connection.getConnectionOptions(options);
mongoClient.connect(connString, connOptions, callback);
};

View File

@@ -1,7 +1,7 @@
'use strict';
var mubsub = require('mubsub-nbb');
var db = require('../mongo');
var client = mubsub(db.getConnectionString(), db.getConnectionOptions());
const mubsub = require('mubsub-nbb');
const connection = require('./connection');
const client = mubsub(connection.getConnectionString(), connection.getConnectionOptions());
module.exports = client.channel('pubsub');