Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a health endpoint #448

Merged
merged 2 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" : {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why this can't be a single value, like "healthEndpointEnabled" : true ? Are you thinking there would be more options available in the future for the health endpoint that would warrent its own section/object ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, for example we could add another option to configure not to return error details, but just the health status.
Or configure, as I said in the other answer to check for other modules/dependencies.

"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 = [];
digitaldan marked this conversation as resolved.
Show resolved Hide resolved
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 @@
var 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 @@ -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();