Windshaft-cartodb/lib/server-info-controller.js

49 lines
1.5 KiB
JavaScript
Raw Normal View History

'use strict';
2019-10-07 15:59:54 +08:00
var HealthCheck = require('./monitoring/health-check');
2015-07-08 22:08:38 +08:00
2019-10-22 01:07:24 +08:00
var WELCOME_MSG = 'This is the CartoDB Maps API, ' +
'see the documentation at http://docs.cartodb.com/cartodb-platform/maps-api.html';
2015-07-08 22:08:38 +08:00
2019-10-22 01:07:24 +08:00
function ServerInfoController (versions) {
2015-07-08 22:08:38 +08:00
this.healthConfig = global.environment.health || {};
this.healthCheck = new HealthCheck(global.environment.disabled_file);
this.versions = versions || {};
2015-07-08 22:08:38 +08:00
}
module.exports = ServerInfoController;
2019-10-22 01:07:24 +08:00
ServerInfoController.prototype.route = function (monitorRouter) {
2018-03-28 00:46:54 +08:00
monitorRouter.get('/health', this.health.bind(this));
monitorRouter.get('/', this.welcome.bind(this));
monitorRouter.get('/version', this.version.bind(this));
2015-07-08 22:08:38 +08:00
};
2019-10-22 01:07:24 +08:00
ServerInfoController.prototype.welcome = function (req, res) {
2015-09-17 08:05:25 +08:00
res.status(200).send(WELCOME_MSG);
2015-07-08 22:08:38 +08:00
};
2019-10-22 01:07:24 +08:00
ServerInfoController.prototype.version = function (req, res) {
res.status(200).send(this.versions);
2015-07-08 22:08:38 +08:00
};
2019-10-22 01:07:24 +08:00
ServerInfoController.prototype.health = function (req, res) {
if (this.healthConfig.enabled) {
2015-07-08 22:08:38 +08:00
var startTime = Date.now();
2019-10-22 01:07:24 +08:00
this.healthCheck.check(function (err) {
2015-07-08 22:08:38 +08:00
var ok = !err;
var response = {
enabled: true,
ok: ok,
elapsed: Date.now() - startTime
2015-07-08 22:08:38 +08:00
};
if (err) {
response.err = err.message;
}
2015-09-17 08:05:25 +08:00
res.status(ok ? 200 : 503).send(response);
2015-07-08 22:08:38 +08:00
});
} else {
2019-10-22 01:07:24 +08:00
res.status(200).send({ enabled: false, ok: true });
2015-07-08 22:08:38 +08:00
}
};