import { Controller, Get, Headers, HttpCode, HttpStatus, Post, UnauthorizedException, } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import type { AppConfig } from '../config/app-config.schema.js'; import { MetricsService } from './metrics.service.js'; @Controller('internal') export class MetricsController { constructor( private readonly config: ConfigService, private readonly metrics: MetricsService, ) {} @Get('metrics') get(@Headers('authorization') authorization?: string) { this.requireToken(authorization); return this.metrics.snapshot(); } @Post('metrics/reset') @HttpCode(HttpStatus.OK) reset(@Headers('authorization') authorization?: string): { status: 'ok' } { this.requireToken(authorization); this.metrics.reset(); return { status: 'ok' }; } private requireToken(authorization?: string): void { const token = this.config.get('METRICS_TOKEN'); if (!token || authorization !== `Bearer ${token}`) throw new UnauthorizedException({ message: 'Unauthorized', challenge: 'Bearer realm="metrics"', }); } }