UNPKG

dino-express

Version:

DinO enabled REST framework based on express

69 lines 2.73 kB
"use strict"; // Copyright 2018 Quirino Brizi [quirino.brizi@gmail.com] // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. Object.defineProperty(exports, "__esModule", { value: true }); exports.ApplicationStateMonitor = void 0; const dino_core_1 = require("dino-core"); /** * The health monitor handler used to define the express error handling behaviour * @public */ class ApplicationStateMonitor { monitors; applicationContext; constructor(applicationContext) { this.monitors = []; this.applicationContext = applicationContext; } doMonitor(res) { const monitors = this.getApplicationStateMonitors(); let properties = { default: [{ name: 'status', value: 'UP' }] }; if (dino_core_1.ObjectHelper.isDefined(monitors) && monitors.length > 0) { properties = monitors.reduce((answer, monitor) => { const state = monitor.execute(); answer[monitor.getName()] = Array.from(state.getProperties()).map((entry) => { return { name: entry[0], value: entry[1] }; }); return answer; }, properties); } return res.status(200).type('application/json').send(JSON.stringify(properties)); } getApplicationStateMonitors() { if (dino_core_1.ObjectHelper.isDefined(this.monitors)) { try { // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion const monitors = this.applicationContext.resolveAll(dino_core_1.Monitor); this.monitors.concat(monitors); } catch (e) { // Ignore, there is no application state monitor defined, we'll use the default behaviour... } } return this.monitors; } /** * Allows to add a monitor * @param {Monitor} monitor */ addMonitor(monitor) { this.monitors.push(monitor); return this; } middleware() { return (_req, res, _next) => this.doMonitor.bind(this)(res); } } exports.ApplicationStateMonitor = ApplicationStateMonitor; //# sourceMappingURL=application.state.monitor.js.map