* closes #6996

* fix lint
v1.18.x
Barış Soner Uşaklı 6 years ago committed by GitHub
parent 7b9a2caec3
commit 0524fd9caa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -19,4 +19,24 @@ primaryDB.parseIntFields = function (data, intFields, requestedFields) {
}); });
}; };
primaryDB.initSessionStore = function (callback) {
const sessionStoreConfig = nconf.get('session_store') || nconf.get('redis') || nconf.get(databaseName);
let sessionStoreDB = primaryDB;
if (nconf.get('session_store')) {
sessionStoreDB = require('./' + sessionStoreConfig.name);
} else if (nconf.get('redis')) {
// if redis is specified, use it as session store over others
sessionStoreDB = require('./redis');
}
sessionStoreDB.createSessionStore(sessionStoreConfig, function (err, sessionStore) {
if (err) {
return callback(err);
}
primaryDB.sessionStore = sessionStore;
callback();
});
};
module.exports = primaryDB; module.exports = primaryDB;

@ -9,7 +9,6 @@ var session = require('express-session');
var _ = require('lodash'); var _ = require('lodash');
var semver = require('semver'); var semver = require('semver');
var prompt = require('prompt'); var prompt = require('prompt');
var db;
var client; var client;
var mongoModule = module.exports; var mongoModule = module.exports;
@ -62,40 +61,42 @@ mongoModule.questions = [
mongoModule.helpers = mongoModule.helpers || {}; mongoModule.helpers = mongoModule.helpers || {};
mongoModule.helpers.mongo = require('./mongo/helpers'); mongoModule.helpers.mongo = require('./mongo/helpers');
mongoModule.getConnectionString = function () { mongoModule.getConnectionString = function (mongo) {
mongo = mongo || nconf.get('mongo');
var usernamePassword = ''; var usernamePassword = '';
var uri = nconf.get('mongo:uri') || ''; var uri = mongo.uri || '';
if (nconf.get('mongo:username') && nconf.get('mongo:password')) { if (mongo.username && mongo.password) {
usernamePassword = nconf.get('mongo:username') + ':' + encodeURIComponent(nconf.get('mongo:password')) + '@'; usernamePassword = nconf.get('mongo:username') + ':' + encodeURIComponent(nconf.get('mongo:password')) + '@';
} else if (!uri.includes('@') || !uri.slice(uri.indexOf('://') + 3, uri.indexOf('@'))) { } else if (!uri.includes('@') || !uri.slice(uri.indexOf('://') + 3, uri.indexOf('@'))) {
winston.warn('You have no mongo username/password setup!'); winston.warn('You have no mongo username/password setup!');
} }
// Sensible defaults for Mongo, if not set // Sensible defaults for Mongo, if not set
if (!nconf.get('mongo:host')) { if (!mongo.host) {
nconf.set('mongo:host', '127.0.0.1'); mongo.host = '127.0.0.1';
} }
if (!nconf.get('mongo:port')) { if (!mongo.port) {
nconf.set('mongo:port', 27017); mongo.port = 27017;
} }
const dbName = nconf.get('mongo:database'); const dbName = mongo.database;
if (dbName === undefined || dbName === '') { if (dbName === undefined || dbName === '') {
winston.warn('You have no database name, using "nodebb"'); winston.warn('You have no database name, using "nodebb"');
nconf.set('mongo:database', 'nodebb'); mongo.database = 'nodebb';
} }
var hosts = nconf.get('mongo:host').split(','); var hosts = mongo.host.split(',');
var ports = nconf.get('mongo:port').toString().split(','); var ports = mongo.port.toString().split(',');
var servers = []; var servers = [];
for (var i = 0; i < hosts.length; i += 1) { for (var i = 0; i < hosts.length; i += 1) {
servers.push(hosts[i] + ':' + ports[i]); servers.push(hosts[i] + ':' + ports[i]);
} }
return nconf.get('mongo:uri') || 'mongodb://' + usernamePassword + servers.join() + '/' + nconf.get('mongo:database'); return uri || 'mongodb://' + usernamePassword + servers.join() + '/' + mongo.database;
}; };
mongoModule.getConnectionOptions = function () { mongoModule.getConnectionOptions = function (mongo) {
mongo = mongo || nconf.get('mongo');
var connOptions = { var connOptions = {
poolSize: 10, poolSize: 10,
reconnectTries: 3600, reconnectTries: 3600,
@ -105,24 +106,19 @@ mongoModule.getConnectionOptions = function () {
useNewUrlParser: true, useNewUrlParser: true,
}; };
return _.merge(connOptions, nconf.get('mongo:options') || {}); return _.merge(connOptions, mongo.options || {});
}; };
mongoModule.init = function (callback) { mongoModule.init = function (callback) {
callback = callback || function () { }; callback = callback || function () { };
var mongoClient = require('mongodb').MongoClient; mongoModule.connect(nconf.get('mongo'), function (err, _client) {
var connString = mongoModule.getConnectionString();
var connOptions = mongoModule.getConnectionOptions();
mongoClient.connect(connString, connOptions, function (err, _client) {
if (err) { if (err) {
winston.error('NodeBB could not connect to your Mongo database. Mongo returned the following error', err); winston.error('NodeBB could not connect to your Mongo database. Mongo returned the following error', err);
return callback(err); return callback(err);
} }
client = _client; client = _client;
db = client.db(); var db = client.db();
mongoModule.client = db; mongoModule.client = db;
require('./mongo/main')(db, mongoModule); require('./mongo/main')(db, mongoModule);
@ -138,30 +134,31 @@ mongoModule.init = function (callback) {
}); });
}; };
mongoModule.initSessionStore = function (callback) { mongoModule.connect = function (options, callback) {
var meta = require('../meta'); callback = callback || function () { };
var sessionStore;
var ttl = meta.getSessionTTLSeconds(); var mongoClient = require('mongodb').MongoClient;
if (nconf.get('redis')) { var connString = mongoModule.getConnectionString(options);
sessionStore = require('connect-redis')(session); var connOptions = mongoModule.getConnectionOptions(options);
var rdb = require('./redis');
rdb.client = rdb.connect();
mongoModule.sessionStore = new sessionStore({ mongoClient.connect(connString, connOptions, callback);
client: rdb.client, };
ttl: ttl,
}); mongoModule.createSessionStore = function (options, callback) {
} else if (nconf.get('mongo')) { mongoModule.connect(options, function (err, client) {
sessionStore = require('connect-mongo')(session); if (err) {
mongoModule.sessionStore = new sessionStore({ return callback(err);
db: db, }
ttl: ttl, const meta = require('../meta');
const sessionStore = require('connect-mongo')(session);
const store = new sessionStore({
db: client.db(),
ttl: meta.getSessionTTLSeconds(),
}); });
}
callback(); callback(null, store);
});
}; };
mongoModule.createIndices = function (callback) { mongoModule.createIndices = function (callback) {

@ -7,7 +7,7 @@ var session = require('express-session');
var _ = require('lodash'); var _ = require('lodash');
var semver = require('semver'); var semver = require('semver');
var dbNamespace = require('continuation-local-storage').createNamespace('postgres'); var dbNamespace = require('continuation-local-storage').createNamespace('postgres');
var db;
var postgresModule = module.exports; var postgresModule = module.exports;
@ -44,29 +44,30 @@ postgresModule.questions = [
postgresModule.helpers = postgresModule.helpers || {}; postgresModule.helpers = postgresModule.helpers || {};
postgresModule.helpers.postgres = require('./postgres/helpers'); postgresModule.helpers.postgres = require('./postgres/helpers');
postgresModule.getConnectionOptions = function () { postgresModule.getConnectionOptions = function (postgres) {
postgres = postgres || nconf.get('postgres');
// Sensible defaults for PostgreSQL, if not set // Sensible defaults for PostgreSQL, if not set
if (!nconf.get('postgres:host')) { if (!postgres.host) {
nconf.set('postgres:host', '127.0.0.1'); postgres.host = '127.0.0.1';
} }
if (!nconf.get('postgres:port')) { if (!postgres.port) {
nconf.set('postgres:port', 5432); postgres.port = 5432;
} }
const dbName = nconf.get('postgres:database'); const dbName = postgres.database;
if (dbName === undefined || dbName === '') { if (dbName === undefined || dbName === '') {
winston.warn('You have no database name, using "nodebb"'); winston.warn('You have no database name, using "nodebb"');
nconf.set('postgres:database', 'nodebb'); postgres.database = 'nodebb';
} }
var connOptions = { var connOptions = {
host: nconf.get('postgres:host'), host: postgres.host,
port: nconf.get('postgres:port'), port: postgres.port,
user: nconf.get('postgres:username'), user: postgres.username,
password: nconf.get('postgres:password'), password: postgres.password,
database: nconf.get('postgres:database'), database: postgres.database,
}; };
return _.merge(connOptions, nconf.get('postgres:options') || {}); return _.merge(connOptions, postgres.options || {});
}; };
postgresModule.init = function (callback) { postgresModule.init = function (callback) {
@ -76,7 +77,7 @@ postgresModule.init = function (callback) {
var connOptions = postgresModule.getConnectionOptions(); var connOptions = postgresModule.getConnectionOptions();
db = new Pool(connOptions); const db = new Pool(connOptions);
db.on('connect', function (client) { db.on('connect', function (client) {
var realQuery = client.query; var realQuery = client.query;
@ -132,6 +133,29 @@ postgresModule.init = function (callback) {
}); });
}; };
postgresModule.connect = function (options, callback) {
var Pool = require('pg').Pool;
var connOptions = postgresModule.getConnectionOptions(options);
const db = new Pool(connOptions);
db.on('connect', function (client) {
var realQuery = client.query;
client.query = function () {
var args = Array.prototype.slice.call(arguments, 0);
if (dbNamespace.active && typeof args[args.length - 1] === 'function') {
args[args.length - 1] = dbNamespace.bind(args[args.length - 1]);
}
return realQuery.apply(client, args);
};
});
db.connect(function (err) {
callback(err, db);
});
};
function checkUpgrade(client, callback) { function checkUpgrade(client, callback) {
client.query(` client.query(`
SELECT EXISTS(SELECT * SELECT EXISTS(SELECT *
@ -347,41 +371,27 @@ SELECT "_key", "type"
}); });
} }
postgresModule.initSessionStore = function (callback) { postgresModule.createSessionStore = function (options, callback) {
var meta = require('../meta'); var meta = require('../meta');
var sessionStore;
var ttl = meta.getSessionTTLSeconds();
if (nconf.get('redis')) {
sessionStore = require('connect-redis')(session);
var rdb = require('./redis');
rdb.client = rdb.connect();
postgresModule.sessionStore = new sessionStore({ function done(db) {
client: rdb.client, const sessionStore = require('connect-pg-simple')(session);
ttl: ttl, const store = new sessionStore({
});
return callback();
}
function done() {
sessionStore = require('connect-pg-simple')(session);
postgresModule.sessionStore = new sessionStore({
pool: db, pool: db,
ttl: ttl, ttl: meta.getSessionTTLSeconds(),
pruneSessionInterval: nconf.get('isPrimary') === 'true' ? 60 : false, pruneSessionInterval: nconf.get('isPrimary') === 'true' ? 60 : false,
}); });
callback(null, store);
callback();
}
if (nconf.get('isPrimary') !== 'true') {
return done();
} }
db.query(` postgresModule.connect(options, function (err, db) {
if (err) {
return callback(err);
}
if (nconf.get('isPrimary') !== 'true') {
return done(db);
}
db.query(`
CREATE TABLE IF NOT EXISTS "session" ( CREATE TABLE IF NOT EXISTS "session" (
"sid" CHAR(32) NOT NULL "sid" CHAR(32) NOT NULL
COLLATE "C" COLLATE "C"
@ -395,11 +405,12 @@ CREATE INDEX IF NOT EXISTS "session_expire_idx" ON "session"("expire");
ALTER TABLE "session" ALTER TABLE "session"
ALTER "sid" SET STORAGE MAIN, ALTER "sid" SET STORAGE MAIN,
CLUSTER ON "session_expire_idx";`, function (err) { CLUSTER ON "session_expire_idx";`, function (err) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
done(); done(db);
});
}); });
}; };
@ -456,7 +467,7 @@ SELECT true "postgres",
postgresModule.close = function (callback) { postgresModule.close = function (callback) {
callback = callback || function () {}; callback = callback || function () {};
db.end(callback); postgresModule.pool.end(callback);
}; };
postgresModule.socketAdapter = function () { postgresModule.socketAdapter = function () {

@ -36,9 +36,20 @@ redisModule.questions = [
}, },
]; ];
redisModule.getConnectionOptions = function (redis) {
redis = redis || nconf.get('redis');
let connOptions = {};
if (redis.password) {
connOptions.auth_pass = redis.password;
}
connOptions = _.merge(connOptions, redis.options || {});
return connOptions;
};
redisModule.init = function (callback) { redisModule.init = function (callback) {
callback = callback || function () { }; callback = callback || function () { };
redisClient = redisModule.connect({}, function (err) { redisClient = redisModule.connect(nconf.get('redis'), function (err) {
if (err) { if (err) {
winston.error('NodeBB could not connect to your Redis database. Redis returned the following error', err); winston.error('NodeBB could not connect to your Redis database. Redis returned the following error', err);
return callback(err); return callback(err);
@ -58,39 +69,22 @@ redisModule.init = function (callback) {
}); });
}; };
redisModule.initSessionStore = function (callback) {
var meta = require('../meta');
var sessionStore = require('connect-redis')(session);
redisModule.sessionStore = new sessionStore({
client: redisModule.client,
ttl: meta.getSessionTTLSeconds(),
});
if (typeof callback === 'function') {
callback();
}
};
redisModule.connect = function (options, callback) { redisModule.connect = function (options, callback) {
callback = callback || function () {}; callback = callback || function () {};
var redis_socket_or_host = nconf.get('redis:host'); options = options || nconf.get('redis');
var redis_socket_or_host = options.host;
var cxn; var cxn;
var callbackCalled = false; var callbackCalled = false;
options = options || {};
if (nconf.get('redis:password')) {
options.auth_pass = nconf.get('redis:password');
}
options = _.merge(options, nconf.get('redis:options') || {}); const connOptions = redisModule.getConnectionOptions(options);
if (redis_socket_or_host && redis_socket_or_host.indexOf('/') >= 0) { if (redis_socket_or_host && redis_socket_or_host.indexOf('/') >= 0) {
/* If redis.host contains a path name character, use the unix dom sock connection. ie, /tmp/redis.sock */ /* If redis.host contains a path name character, use the unix dom sock connection. ie, /tmp/redis.sock */
cxn = redis.createClient(nconf.get('redis:host'), options); cxn = redis.createClient(options.host, connOptions);
} else { } else {
/* Else, connect over tcp/ip */ /* Else, connect over tcp/ip */
cxn = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host'), options); cxn = redis.createClient(options.port, options.host, connOptions);
} }
cxn.on('error', function (err) { cxn.on('error', function (err) {
@ -108,11 +102,11 @@ redisModule.connect = function (options, callback) {
} }
}); });
if (nconf.get('redis:password')) { if (options.password) {
cxn.auth(nconf.get('redis:password')); cxn.auth(options.password);
} }
var dbIdx = parseInt(nconf.get('redis:database'), 10); var dbIdx = parseInt(options.database, 10);
if (dbIdx >= 0) { if (dbIdx >= 0) {
cxn.select(dbIdx, function (err) { cxn.select(dbIdx, function (err) {
if (err) { if (err) {
@ -125,6 +119,20 @@ redisModule.connect = function (options, callback) {
return cxn; return cxn;
}; };
redisModule.createSessionStore = function (options, callback) {
const meta = require('../meta');
const sessionStore = require('connect-redis')(session);
const client = redisModule.connect(options);
const store = new sessionStore({
client: client,
ttl: meta.getSessionTTLSeconds(),
});
if (typeof callback === 'function') {
callback(null, store);
}
};
redisModule.createIndices = function (callback) { redisModule.createIndices = function (callback) {
setImmediate(callback); setImmediate(callback);
}; };
@ -182,8 +190,8 @@ redisModule.info = function (cxn, callback) {
redisModule.socketAdapter = function () { redisModule.socketAdapter = function () {
var redisAdapter = require('socket.io-redis'); var redisAdapter = require('socket.io-redis');
var pub = redisModule.connect(); var pub = redisModule.connect(nconf.get('redis'));
var sub = redisModule.connect(); var sub = redisModule.connect(nconf.get('redis'));
return redisAdapter({ return redisAdapter({
key: 'db:' + nconf.get('redis:database') + ':adapter_key', key: 'db:' + nconf.get('redis:database') + ':adapter_key',
pubClient: pub, pubClient: pub,

Loading…
Cancel
Save