2017-03-27 15:36:45 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const config = require('config');
|
2017-04-05 16:39:42 +08:00
|
|
|
const tools = require('./tools');
|
2017-03-27 15:36:45 +08:00
|
|
|
const mongodb = require('mongodb');
|
2017-04-05 16:39:42 +08:00
|
|
|
const redis = require('redis');
|
2017-03-27 15:36:45 +08:00
|
|
|
const MongoClient = mongodb.MongoClient;
|
|
|
|
|
2017-04-25 03:59:38 +08:00
|
|
|
module.exports.senderDb = false;
|
2017-03-27 15:36:45 +08:00
|
|
|
module.exports.database = false;
|
2017-04-05 16:39:42 +08:00
|
|
|
module.exports.redis = false;
|
2017-05-02 21:17:37 +08:00
|
|
|
module.exports.redisConfig = false;
|
2017-03-27 15:36:45 +08:00
|
|
|
|
|
|
|
module.exports.connect = callback => {
|
|
|
|
MongoClient.connect(config.mongo, (err, database) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
module.exports.database = database;
|
2017-05-02 21:17:37 +08:00
|
|
|
module.exports.redisConfig = tools.redisConfig(config.redis);
|
|
|
|
module.exports.redis = redis.createClient(module.exports.redisConfig);
|
2017-04-24 21:51:50 +08:00
|
|
|
|
2017-04-25 03:59:38 +08:00
|
|
|
if (!config.sender.enabled) {
|
2017-04-24 21:51:50 +08:00
|
|
|
return callback(null, database);
|
|
|
|
}
|
|
|
|
|
2017-04-25 03:59:38 +08:00
|
|
|
if (!config.sender.mongo) {
|
|
|
|
module.exports.senderDb = database;
|
2017-04-24 21:51:50 +08:00
|
|
|
return callback(null, database);
|
|
|
|
}
|
|
|
|
|
2017-04-25 03:59:38 +08:00
|
|
|
MongoClient.connect(config.sender.mongo, (err, forwarderDatabase) => {
|
2017-04-24 21:51:50 +08:00
|
|
|
if (err) {
|
|
|
|
database.close();
|
|
|
|
return callback(err);
|
|
|
|
}
|
2017-04-25 03:59:38 +08:00
|
|
|
module.exports.senderDb = forwarderDatabase;
|
2017-04-24 21:51:50 +08:00
|
|
|
return callback(null, database);
|
|
|
|
});
|
2017-03-27 15:36:45 +08:00
|
|
|
});
|
|
|
|
};
|