forked from fzaninotto/uptime
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbootstrap.js
More file actions
47 lines (45 loc) · 1.64 KB
/
bootstrap.js
File metadata and controls
47 lines (45 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
var mongoose = require('mongoose');
var config = require('config');
var semver = require('semver');
// configure mongodb
var userAuthStr = '';
if (config.mongodb.user) {
userAuthStr = config.mongodb.user + ':' + config.mongodb.password + '@';
}
var connectWithRetry = function() {
mongoose.connect(config.mongodb.connectionString || 'mongodb://' + userAuthStr + config.mongodb.server + '/' + config.mongodb.database, {
server: {
auto_reconnect: true
}
}, function(err) {
if (err) {
console.error('Failed to connect to mongo on startup - retrying in 5 sec', err);
connectWithRetry();
} else {
mongoose.connection.on('open', function(err) {
mongoose.connection.db.admin().serverStatus(function(err, data) {
if (err) {
if (err.name === "MongoError" && (err.errmsg === 'need to login' || err.errmsg === 'unauthorized') && !config.mongodb.connectionString) {
console.log('Forcing MongoDB authentication');
mongoose.connection.db.authenticate(config.mongodb.user, config.mongodb.password, function(err) {
if (!err) return;
console.error(err);
//process.exit(1);
});
return;
} else {
console.error(err);
//process.exit(1);
}
}
if (!semver.satisfies(data.version, '>=2.1.0')) {
console.error('Error: Uptime requires MongoDB v2.1 minimum. The current MongoDB server uses only ' + data.version);
//process.exit(1);
}
});
});
}
});
};
connectWithRetry();
module.exports = mongoose;