Skip to content

Commit

Permalink
Altered creation of connections to mongo to allow for replications se…
Browse files Browse the repository at this point in the history
…ts to be defined in config.js.
  • Loading branch information
roblperry committed Oct 1, 2013
1 parent e5031b7 commit eab986a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
11 changes: 11 additions & 0 deletions api/config.sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ var countlyConfig = {
port: 27017,
max_pool_size: 1000
},
/* or for a replication set
mongodb: {
replSetServers : [ '192.168.3.1:27017/?auto_reconnect=true',
'192.168.3.2:27017/?auto_reconnect=true' ],
db: "countly",
max_pool_size: 1000
},
*/
/* or define as a url
mongodb: "localhost:27017/countly?auto_reconnect=true"
*/
api: {
workers: 0,
port: 3001,
Expand Down
13 changes: 11 additions & 2 deletions api/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,17 @@ var common = {},
'has_ongoing_session': 'hos'
};

var connectionString = (typeof countlyConfig.mongodb === "string")? countlyConfig.mongodb : (countlyConfig.mongodb.host + ':' + countlyConfig.mongodb.port + '/' + countlyConfig.mongodb.db + '?auto_reconnect=true');
common.db = mongo.db(connectionString, {safe:false, maxPoolSize: countlyConfig.mongodb.max_pool_size || 1000});
var dbName;
var dbOptions = { safe:false, maxPoolSize: countlyConfig.mongodb.max_pool_size || 1000 };
if (typeof countlyConfig.mongodb === "string" ){
dbName = countlyConfig.mongodb;
} else if ( typeof countlyConfig.mongodb.replSetServers === 'object'){
dbName = countlyConfig.mongodb.replSetServers;
dbOptions.database = countlyConfig.mongodb.db || 'countly';
} else {
dbName = (countlyConfig.mongodb.host + ':' + countlyConfig.mongodb.port + '/' + countlyConfig.mongodb.db + '?auto_reconnect=true');
}
common.db = mongo.db(dbName, dbOptions);

common.config = countlyConfig;

Expand Down
16 changes: 13 additions & 3 deletions frontend/express/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@ var http = require('http'),
request = require('request'),
countlyMail = require('../../api/parts/mgmt/mail.js'),
countlyStats = require('../../api/parts/data/stats.js'),
countlyConfig = require('./config'),
connectionString = (typeof countlyConfig.mongodb === "string")? countlyConfig.mongodb : (countlyConfig.mongodb.host + ':' + countlyConfig.mongodb.port + '/' + countlyConfig.mongodb.db + '?auto_reconnect=true&safe=true'),
countlyDb = mongo.db(connectionString, {safe:true});
countlyConfig = require('./config');

var dbName;
var dbOptions = { safe:true };
if (typeof countlyConfig.mongodb === "string" ){
dbName = countlyConfig.mongodb;
} else if ( typeof countlyConfig.mongodb.replSetServers === 'object'){
dbName = countlyConfig.mongodb.replSetServers;
dbOptions.database = countlyConfig.mongodb.db || 'countly';
} else {
dbName = (countlyConfig.mongodb.host + ':' + countlyConfig.mongodb.port + '/' + countlyConfig.mongodb.db + '?auto_reconnect=true');
}
var countlyDb = mongo.db(dbName, dbOptions);

function sha1Hash(str, addSalt) {
var salt = (addSalt) ? new Date().getTime() : "";
Expand Down
10 changes: 10 additions & 0 deletions frontend/express/config.sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ var countlyConfig = {
db: "countly",
port: 27017
},
/* or for a replication set
mongodb: {
replSetServers : [ '192.168.3.1:27017/?auto_reconnect=true',
'192.168.3.2:27017/?auto_reconnect=true' ],
db: "countly",
},
*/
/* or define as a url
mongodb: "localhost:27017/countly?auto_reconnect=true"
*/
web: {
port: 6001,
host: "localhost",
Expand Down

0 comments on commit eab986a

Please sign in to comment.