Skip to content

Commit

Permalink
make health endpoint configurable
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Bräu <ab@andi95.de>
  • Loading branch information
andibraeu committed May 26, 2024
1 parent ce702f1 commit 733b6e2
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 41 deletions.
5 changes: 4 additions & 1 deletion config-development.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"maxFiles" : "7d",
"level" : "debug",
"morganOption" : "dev"
}
},
"healthEndpoint" : {
"enabled" : true
}
},
"express":{
"key" : "some express key"
Expand Down
5 changes: 4 additions & 1 deletion config-production.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
"morganOption" : null
},
"subDomainCookies": false,
"muteNotifications": false
"muteNotifications": false,
"healthEndpoint" : {
"enabled" : false
}
},
"express":{
"key" : "some express key"
Expand Down
5 changes: 4 additions & 1 deletion deployment/docker-compose/config.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"logger" : {
"type": "console"
},
"subDomainCookies": false
"subDomainCookies": false,
"healthEndpoint" : {
"enabled" : true
}
},
"express":{
"key" : "${EXPRESS_KEY}"
Expand Down
81 changes: 43 additions & 38 deletions routes/health.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,54 @@
var app = require('../app');
var system = require('../system');

var mongoose = require('mongoose');

const Errors = {
DBERROR: 'DBERROR'
}

exports.gethealth = function(req, res) {
const mongoose_state = mongoose.connection.readyState
var errors = collectErrors();
if (errors.length == 0) {
res.status(200).json({
status: "OK",
mongoose: mongoose_state
});
exports.gethealth = function (req, res) {
const isHealthEndpointEnabled = system.isHealthEndpointEnabled();
if (!isHealthEndpointEnabled) {
return res.status(404).send("not found");
} else {
return res.status(500).json({
status: "Not OK",
mongoose: mongoose_state,
errors: errors
});
}
const mongoose_state = mongoose.connection.readyState
var errors = collectErrors();
if (errors.length == 0) {
return res.status(200).json({
status: "OK",
mongoose: mongoose_state
});
} else {
return res.status(500).json({
status: "Not OK",
mongoose: mongoose_state,
errors: errors
});
}

function collectErrors() {
var errors = [];
switch (mongoose_state) {
case 0:
errors.push({
error: Errors.DBERROR,
message: "mongodb disconnected"
});
break;
case 2:
errors.push({
error: Errors.DBERROR,
message: "mongodb connecting"
});
break;
case 3:
errors.push({
error: Errors.DBERROR,
message: "mongodb disconnecting"
});
break;
};
return errors;
function collectErrors() {
var errors = [];
switch (mongoose_state) {
case 0:
errors.push({
error: Errors.DBERROR,
message: "mongodb disconnected"
});
break;
case 2:
errors.push({
error: Errors.DBERROR,
message: "mongodb connecting"
});
break;
case 3:
errors.push({
error: Errors.DBERROR,
message: "mongodb disconnecting"
});
break;
};
return errors;
}
}
};
12 changes: 12 additions & 0 deletions system/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,4 +443,16 @@ System.prototype.getLoggerType = function() {
}
};

/**
* Returns true, if health endpoint is enabled.
* @return {boolean}
*/
System.prototype.isHealthEndpointEnabled = function() {
try {
return this.getConfig(['system', 'healthEndpoint', 'enabled']);
} catch(e) {
return false;
}
};

module.exports = new System();

0 comments on commit 733b6e2

Please sign in to comment.