Skip to content

Commit

Permalink
add a health endpoint (#448)
Browse files Browse the repository at this point in the history
* add a health endpoint

Signed-off-by: Andreas Bräu <ab@andi95.de>

* make health endpoint configurable

Signed-off-by: Andreas Bräu <ab@andi95.de>

---------

Signed-off-by: Andreas Bräu <ab@andi95.de>
  • Loading branch information
andibraeu authored Jun 23, 2024
1 parent 5e0dc80 commit a8138a3
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 3 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" : false
}
},
"express":{
"key" : "${EXPRESS_KEY}"
Expand Down
54 changes: 54 additions & 0 deletions routes/health.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var system = require('../system');

var mongoose = require('mongoose');

const Errors = {
DBERROR: 'DBERROR'
}

exports.gethealth = function (req, res) {
const isHealthEndpointEnabled = system.isHealthEndpointEnabled();
if (!isHealthEndpointEnabled) {
return res.status(404).send("not found");
} else {
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;
}
}
};
4 changes: 4 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const system = require('../system'),
homepage = require('./homepage'),
health_routes = require('./health'),
passport = require('passport'),
account_routes = require('./account'),
devices_routes = require('./devices'),
Expand Down Expand Up @@ -57,6 +58,9 @@ Routes.prototype.setupGeneralRoutes = function (app) {
// General homepage
app.get('/', this.setOpenhab, homepage.index);

// Health page
app.get('/health', this.setOpenhab, health_routes.gethealth);

// Events
app.get('/events', this.ensureAuthenticated, this.setOpenhab, events_routes.eventsget);

Expand Down
12 changes: 12 additions & 0 deletions system/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,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 a8138a3

Please sign in to comment.