import { loggerManager } from "@logger"; import { Router, Response, NextFunction, Request } from "express"; import PromiseRouter from "express-promise-router"; import { WebhookController } from "@server/controllers/webhook.controller"; import { Gauge, Registry } from "prom-client"; export class WebhookRouter { readonly router: Router; private registry: Registry; private gauge: Gauge; public num:number=0; constructor(private readonly webhookController: WebhookController) { this.registry = new Registry(); this.gauge = new Gauge({ name: "received_webhook_counter" + this.num++, help: "When receiving a webhook inc 1" }); this.router = PromiseRouter(); this.setupPrometheus(); this.setupRouters(); } private setupRouters() { this.router.get("/metrics", this.sendPrometheusMatrices.bind(this)); this.router.post("/webhook", this.incGauge.bind(this), (req, res) => this.webhookController.insert(req, res)); } private setupPrometheus() { this.registry.registerMetric(this.gauge); } private async sendPrometheusMatrices(_req: Request, res: Response, _next: NextFunction) { res.setHeader("Content-Type", this.registry.contentType); res.send(await this.registry.metrics()); this.gauge.reset(); } public remove(){ this.registry.clear(); } private async incGauge(_request: Request, _response: Response, next: NextFunction) { this.gauge.inc(); next(); } }