forked from mtusi2026/mtusi2026lab2
feat: lab2 done
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
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<AppConfig>,
|
||||
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<string>('METRICS_TOKEN');
|
||||
if (!token || authorization !== `Bearer ${token}`)
|
||||
throw new UnauthorizedException({
|
||||
message: 'Unauthorized',
|
||||
challenge: 'Bearer realm="metrics"',
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user