From a8138a3ff1c3b6d5fab6e5431ca5d0057a51c936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andi=20Br=C3=A4u?= Date: Sun, 23 Jun 2024 02:56:16 +0200 Subject: [PATCH] add a health endpoint (#448) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add a health endpoint Signed-off-by: Andreas Bräu * make health endpoint configurable Signed-off-by: Andreas Bräu --------- Signed-off-by: Andreas Bräu --- config-development.json | 5 +- config-production.json | 5 +- .../docker-compose/config.json.template | 5 +- routes/health.js | 54 +++++++++++++++++++ routes/index.js | 4 ++ system/index.js | 12 +++++ 6 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 routes/health.js diff --git a/config-development.json b/config-development.json index 31eed09..cab9985 100644 --- a/config-development.json +++ b/config-development.json @@ -9,7 +9,10 @@ "maxFiles" : "7d", "level" : "debug", "morganOption" : "dev" - } + }, + "healthEndpoint" : { + "enabled" : true + } }, "express":{ "key" : "some express key" diff --git a/config-production.json b/config-production.json index 5f0c64b..226c689 100644 --- a/config-production.json +++ b/config-production.json @@ -11,7 +11,10 @@ "morganOption" : null }, "subDomainCookies": false, - "muteNotifications": false + "muteNotifications": false, + "healthEndpoint" : { + "enabled" : false + } }, "express":{ "key" : "some express key" diff --git a/deployment/docker-compose/config.json.template b/deployment/docker-compose/config.json.template index 543d89d..1311751 100644 --- a/deployment/docker-compose/config.json.template +++ b/deployment/docker-compose/config.json.template @@ -6,7 +6,10 @@ "logger" : { "type": "console" }, - "subDomainCookies": false + "subDomainCookies": false, + "healthEndpoint" : { + "enabled" : false + } }, "express":{ "key" : "${EXPRESS_KEY}" diff --git a/routes/health.js b/routes/health.js new file mode 100644 index 0000000..3cf744c --- /dev/null +++ b/routes/health.js @@ -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; + } + } +}; diff --git a/routes/index.js b/routes/index.js index 6c20a57..875b9fa 100644 --- a/routes/index.js +++ b/routes/index.js @@ -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'), @@ -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); diff --git a/system/index.js b/system/index.js index 9146ae3..802397e 100644 --- a/system/index.js +++ b/system/index.js @@ -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();