Files
evidencija-rezija/mailgun-webhook/src/middleware/InitLocalsMiddleware.ts
Knee Cola 7aeea9353d feat: implement MailGun webhook service for logging email events
Implemented a production-ready TypeScript/Express.js service to receive
and log MailGun webhook events (delivered, failed, opened, clicked, etc.).

Key features:
- Webhook endpoint (POST /webhook) with comprehensive event logging
- Full TypeScript type definitions for all MailGun event types
- Prometheus metrics integration for monitoring
- Health check endpoint (GET /ping)
- Comprehensive Jest test suite with 87.76% coverage
- Docker containerization with build scripts

Removed template/example code:
- All SQL/MSSQL dependencies and related code
- Example auth router and middleware
- PRTG metrics support (simplified to Prometheus only)
- Unused middleware (CORS, IP whitelist, request parsing/validation)
- Template documentation (kept only MailGun webhook API spec)

The service is clean, minimal, and focused solely on receiving and
logging MailGun webhook events to the console.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-02 20:56:22 +01:00

43 lines
1.8 KiB
TypeScript

import { Response, Request, NextFunction } from 'express';
import { AppLocals } from '../types/AppLocals';
import { requestDurationHistogram, totalRequestCounter } from '../lib/metricsCounters';
import { SupportedRoutes } from '../types/enums/SupportedRoutes';
/**
* Middleware initializes infrastructure objects which will be used throughout the request lifecycle
* @param req - Express request object
* @param res - Express response object
* @param next - Express next middleware function
*/
export const InitLocalsMiddleware = (req: Request, res: Response, next: NextFunction) => {
try {
switch(req.path) {
// for metrics routes, no prometheus timer is needed
case SupportedRoutes.metricsPath:
case '/favicon.ico':
// placeholder method to avoid checking if timer is initialized
(res.locals as AppLocals).stopPrometheusTimer = (labels) => 0;
break;
// all other routes get prometheus metrics
default:
// The request must be processed even if Prometheus does not work
// That's why here we wrap the Prometheus calls in try/catch
try {
// counting all received requests
totalRequestCounter.inc({ path: req.path });
// starting a timer to measure request processing duration
// this timer will be stopped in the route handler
(res.locals as AppLocals).stopPrometheusTimer = requestDurationHistogram.startTimer();
} catch(ex:any) {
console.error(ex);
}
break;
}
next();
} catch(ex:any) {
console.error('Error in InitLocalsMiddleware:', ex);
next(ex);
}
}