Skip to content

Commit

Permalink
add a health endpoint
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 17, 2024
1 parent 9a2324b commit ce702f1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
49 changes: 49 additions & 0 deletions routes/health.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var app = require('../app');

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
});
} 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 @@
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

0 comments on commit ce702f1

Please sign in to comment.