| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- import express, { Express, NextFunction, Request, Response } from 'express';
- import { HttpStatusException } from '../../common/lib/http-status.exception';
- import { Logger } from '../../common/util/logger.class';
- import { Collector } from './collector.class';
- export class Webserver {
- private app: Express;
- constructor(private port: number, private collector: Collector) {
- this.app = express();
- this.app.get('/', async (req, res, next) => {
- try {
- const hdl = await this.collector.trx.start();
- if (!hdl) {
- return res.send({ hdl: null, data: [] });
- }
- const data = await this.collector.trx.read();
- res.send({
- hdl,
- data
- });
- } catch (err) {
- next(err);
- }
- });
- this.app.patch('/:hdl', async (req, res, next) => {
- try {
- const hdl = Number(req.params.hdl);
- await this.collector.trx.commit(hdl);
- res.send({ ok: true });
- } catch (err) {
- next(err);
- }
- });
- this.app.delete('/:hdl', async (req, res, next) => {
- try {
- const hdl = Number(req.params.hdl);
- await this.collector.trx.rollback(hdl);
- res.send({ ok: true });
- } catch (err) {
- next(err);
- }
- });
- this.app.use((err: any, req: Request, res: Response, next: NextFunction) => {
- if (err instanceof HttpStatusException) {
- res.status(err.statusCode).send(err.message);
- } else {
- Logger.error('[ERROR] Webservice ErrorHandler caught:', err);
- res.status(500).send(JSON.stringify(err));
- }
- });
- this.app.listen(this.port, () => {
- Logger.info(`[INFO] Monitoring Daemon Webservice started at http://localhost:${this.port}`);
- });
- }
- }
|