CartoDB-SQL-API/app/controllers/health_check_controller.js

41 lines
1.1 KiB
JavaScript
Raw Normal View History

'use strict';
const HealthCheckBackend = require('../monitoring/health_check');
module.exports = class HealthCheckController {
constructor () {
this.healthCheckBackend = new HealthCheckBackend(global.settings.disabled_file);
}
route (app) {
app.get(`${global.settings.base_url}/health`, healthCheck({ healthCheckBackend: this.healthCheckBackend }));
}
};
function healthCheck ({ healthCheckBackend }) {
return function healthCheckMiddleware (req, res) {
const healthConfig = global.settings.health || {};
if (!healthConfig.enabled) {
return res.status(200).send({enabled: false, ok: true});
}
const startTime = Date.now();
healthCheckBackend.check((err) => {
const ok = !err;
const response = {
enabled: true,
ok,
elapsed: Date.now() - startTime
};
if (err) {
response.err = err.message;
}
res.status(ok ? 200 : 503).send(response);
});
};
}