Merge pull request #226 from wei772/master

支持MongoDB --authenticationDatabase 连接
This commit is contained in:
suxiaoxin 2018-04-10 19:40:05 +08:00 committed by GitHub
commit a52de8d51b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 8 deletions

View File

@ -6,7 +6,8 @@
"DATABASE": "yapi", "DATABASE": "yapi",
"port": 27017, "port": 27017,
"user": "test1", "user": "test1",
"pass": "test1" "pass": "test1",
"authSource": ""
}, },
"mail": { "mail": {
"enable": true, "enable": true,

View File

@ -24,13 +24,21 @@ function connect(callback) {
} }
let db = mongoose.connect(`mongodb://${config.db.servername}:${config.db.port}/${config.db.DATABASE}`, options, function(err){ var connectString = `mongodb://${config.db.servername}:${config.db.port}/${config.db.DATABASE}`;
if(err){ if (config.db.authSource) {
yapi.commons.log(err +', mongodb Authentication failed', 'error'); connectString = connectString + `?authSource=${config.db.authSource}`;
}
//yapi.commons.log(connectString);
let db = mongoose.connect(connectString, options, function (err) {
if (err) {
yapi.commons.log(err + ', mongodb Authentication failed', 'error');
} }
}); });
db.then(function () { db.then(function () {
yapi.commons.log('mongodb load success...'); yapi.commons.log('mongodb load success...');