| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- import { RouterOptions, json } from 'express';
- import { ServiceCheckData } from '../../../common/lib/http-check-data.module';
- import { HttpStatusException } from '../../../common/lib/http-status.exception';
- import { ControllerPool } from '../ctrl/controller-pool.interface';
- import { ServiceChangedStatus } from '../ctrl/database.class';
- import { WebHandler } from './web-handler.base';
- export class ServicesAPIHandler extends WebHandler {
- constructor(protected ctrlPool: ControllerPool, options?: RouterOptions) {
- super(ctrlPool, options);
- this.router.use(json());
- this.router.use(this.avoidCache);
- this.router.get('/:serverID', async (req, res, next) => {
- try {
- const serverID = this.validateNumber(req.params.serverID, 'server id');
- const services = await this.ctrlPool.db.getHttpCheckConfigs(serverID);
- res.send(services);
- } catch (err) {
- next(err);
- }
- });
- this.router.put('/:serverID', async (req, res, next) => {
- try {
- const serverID = this.validateNumber(req.params.serverID, 'server id');
- const result = await this.ctrlPool.db.saveHttpCheckConfig(serverID, req.body);
- if (result.status !== ServiceChangedStatus.None) {
- await this.ctrlPool.httpChecks.updateCheck(result.status, result.result);
- }
- res.send(result.result);
- } catch (err) {
- next(err);
- }
- });
- this.router.delete('/:serverID/:serviceID', async (req, res, next) => {
- try {
- const serverID = this.validateNumber(req.params.serverID, 'server id');
- const serviceID = this.validateNumber(req.params.serviceID, 'service id');
- const deleted = await this.ctrlPool.db.deleteHealthCheckConfig(serverID, serviceID);
- if (deleted) {
- await this.ctrlPool.httpChecks.updateCheck(ServiceChangedStatus.Deactivated, { id: serviceID } as HttpCheckConfig);
- }
- res.send({ deleted });
- } catch (err) {
- next(err);
- }
- });
- this.router.get('/:serverID/:serviceID', async (req, res, next) => {
- try {
- const serverID = this.validateNumber(req.params.serverID, 'server id');
- const serviceID = this.validateNumber(req.params.serviceID, 'service id');
- const result = await this.ctrlPool.db.getHttpCheckConfigByID(serverID, serviceID);
- res.send(result);
- } catch (err) {
- next(err);
- }
- });
- this.router.get('/:serverID/:serviceID/data', async (req, res, next) => {
- try {
- const serverID = this.validateNumber(req.params.serverID, 'server id');
- const serviceID = this.validateNumber(req.params.serviceID, 'service id');
- const qStart = (req.query.start || '').toString();
- const qEnd = (req.query.end || '').toString();
- if (!qStart || !qEnd) throw new HttpStatusException("QueryParams 'start' and 'end' are mandatory.", 400);
- const start = new Date(qStart);
- const end = new Date(qEnd);
- if ([start.toString(), end.toString()].includes('Invalid Date')) {
- throw new HttpStatusException("QueryParams 'start' and 'end' must be parseable dates or unix epoch timestamps (ms).", 400);
- }
- const data = await this.ctrlPool.db.queryServiceCheckData(serverID, serviceID, start, end);
- res.send({
- start,
- end,
- data
- } as QueryResponse<ServiceCheckData[]>);
- } catch (err) {
- next(err);
- }
- });
- this.router.get('/:serverID/:serviceID/logs', async (req, res, next) => {
- try {
- const serverID = this.validateNumber(req.params.serverID, 'server id');
- const serviceID = this.validateNumber(req.params.serviceID, 'service id');
- const qStart = (req.query.start || '').toString();
- const qEnd = (req.query.end || '').toString();
- if (!qStart || !qEnd) throw new HttpStatusException("QueryParams 'start' and 'end' are mandatory.", 400);
- const start = new Date(qStart);
- const end = new Date(qEnd);
- if ([start.toString(), end.toString()].includes('Invalid Date')) {
- throw new HttpStatusException("QueryParams 'start' and 'end' must be parseable dates or unix epoch timestamps (ms).", 400);
- }
- const data = await this.ctrlPool.db.queryServiceCheckLogs(serverID, serviceID, start, end);
- res.send({
- start,
- end,
- data
- } as QueryResponse<ServiceCheckData[]>);
- } catch (err) {
- next(err);
- }
- });
- }
- private validateNumber(id: string, field: string) {
- const num = Number(id);
- if (Number.isNaN(num)) {
- throw new HttpStatusException(`Not a valid ${field}: ${id}`, 400);
- }
- return num;
- }
- }
|