import express, { Express, NextFunction, Request, Response } from 'express'; import path from 'path'; import { HttpStatusException } from '../../common/lib/http-status.exception'; import { Logger } from '../../common/util/logger.class'; import { ControllerPool } from './ctrl/controller-pool.interface'; import { ServerAPIHandler } from './webhdl/server-api-handler.class'; import { ServicesAPIHandler } from './webhdl/services-api-handler.class'; export class Webserver { private app: Express; constructor(private port: number, ctrlPool: ControllerPool) { this.app = express(); const serverApi = new ServerAPIHandler(ctrlPool); this.app.use('/server', serverApi.router); const servicesApi = new ServicesAPIHandler(ctrlPool); this.app.use('/services', servicesApi.router); this.app.use('/', express.static(process.env.STATIC_DIR || 'public')); this.app.use('**', express.static(path.join(process.env.STATIC_DIR || 'public', 'index.html'))); 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 Webserver started at http://localhost:${this.port}`); }); } }