forked from DeviaVir/zenbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Intro "global" eventEmitter for loosely coupled inter-module communic…
…ation (DeviaVir#1324) * setup basics for eventEmitter driven module communication * get rid of unused dependency * document silent option * put conf loading before command line overrides
- Loading branch information
Showing
9 changed files
with
199 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,63 @@ | ||
var _ = require('lodash') | ||
var path = require('path') | ||
var minimist = require('minimist') | ||
var minimist = require('minimist') | ||
var version = require('./package.json').version | ||
var EventEmitter = require('events') | ||
|
||
module.exports = function (cb) { | ||
var zenbot = require('./') | ||
var c = getConfiguration() | ||
var zenbot = { version } | ||
var args = minimist(process.argv.slice(3)) | ||
var conf | ||
|
||
var defaults = require('./conf-sample') | ||
Object.keys(defaults).forEach(function (k) { | ||
if (typeof c[k] === 'undefined') { | ||
c[k] = defaults[k] | ||
if(!_.isUndefined(args.conf)){ | ||
try { | ||
conf = require(path.resolve(process.cwd(), args.conf)) | ||
} catch (err) { | ||
console.log('Fall back to conf.js, ' + err) | ||
conf = require('./conf') | ||
} | ||
}) | ||
zenbot.conf = c | ||
|
||
function withMongo () { | ||
cb(null, zenbot) | ||
} else { | ||
conf = require('./conf') | ||
} | ||
|
||
var authStr = '', authMechanism | ||
|
||
if(c.mongo.username){ | ||
authStr = encodeURIComponent(c.mongo.username) | ||
var defaults = require('./conf-sample') | ||
_.defaultsDeep(conf, defaults) | ||
zenbot.conf = conf | ||
|
||
var eventBus = new EventEmitter() | ||
conf.eventBus = eventBus | ||
|
||
var authStr = '', authMechanism, connectionString | ||
|
||
if(c.mongo.password) authStr += ':' + encodeURIComponent(c.mongo.password) | ||
if(conf.mongo.username){ | ||
authStr = encodeURIComponent(conf.mongo.username) | ||
|
||
if(conf.mongo.password) authStr += ':' + encodeURIComponent(conf.mongo.password) | ||
|
||
authStr += '@' | ||
|
||
// authMechanism could be a conf.js parameter to support more mongodb authentication methods | ||
authMechanism = 'DEFAULT' | ||
} | ||
|
||
var u = (function() { | ||
if (c.mongo.connectionString) { | ||
return c.mongo.connectionString | ||
} | ||
|
||
return 'mongodb://' + authStr + c.mongo.host + ':' + c.mongo.port + '/' + c.mongo.db + '?' + | ||
(c.mongo.replicaSet ? '&replicaSet=' + c.mongo.replicaSet : '' ) + | ||
if (conf.mongo.connectionString) { | ||
connectionString = conf.mongo.connectionString | ||
} else { | ||
connectionString = 'mongodb://' + authStr + conf.mongo.host + ':' + conf.mongo.port + '/' + conf.mongo.db + '?' + | ||
(conf.mongo.replicaSet ? '&replicaSet=' + conf.mongo.replicaSet : '' ) + | ||
(authMechanism ? '&authMechanism=' + authMechanism : '' ) | ||
})() | ||
require('mongodb').MongoClient.connect(u, function (err, client) { | ||
} | ||
|
||
require('mongodb').MongoClient.connect(connectionString, function (err, client) { | ||
if (err) { | ||
//zenbot.set('zenbot:db.mongo', null) | ||
console.error('WARNING: MongoDB Connection Error: ', err) | ||
console.error('WARNING: without MongoDB some features (such as backfilling/simulation) may be disabled.') | ||
console.error('Attempted authentication string: ' + u) | ||
return withMongo() | ||
console.error('Attempted authentication string: ' + connectionString) | ||
cb(null, zenbot) | ||
return | ||
} | ||
var db = client.db(c.mongo.db) | ||
var db = client.db(conf.mongo.db) | ||
_.set(zenbot, 'conf.db.mongo', db) | ||
withMongo() | ||
cb(null, zenbot) | ||
}) | ||
|
||
function getConfiguration() { | ||
var args = minimist(process.argv.slice(3)) | ||
var conf = undefined | ||
|
||
try { | ||
if(!_.isUndefined(args.conf)){ | ||
try { | ||
conf = require(path.resolve(process.cwd(), args.conf)) | ||
} catch (ee) { | ||
console.log('Fall back to conf.js, ' + ee) | ||
conf = require('./conf') | ||
} | ||
} else { | ||
conf = require('./conf') | ||
} | ||
} | ||
catch (e) { | ||
console.log('Fall back to sample-conf.js, ' + e) | ||
conf = {} | ||
} | ||
|
||
// prevent modifying cached module with a clone | ||
return _.cloneDeep(conf) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.