7d250ec342
Co-Authored-By: Claude Code <noreply@anthropic.com>
38 lines
1.7 KiB
TypeScript
38 lines
1.7 KiB
TypeScript
// services/api/src/app-cn.module.ts
|
||
// 根模块:日志(nestjs-pino / pino-http)、request-id 中间件、全局异常过滤器、
|
||
// /v1/health。功能模块(auth/albums/uploads/photos/public)自 Task 12+ 起逐个加入。
|
||
// 过滤器经 APP_FILTER、中间件经 NestModule 在模块层注册——e2e 直接 import 本模块
|
||
// 即可获得与生产一致的管线(全局前缀除外,由入口设置)。
|
||
import { type MiddlewareConsumer, Module, type NestModule } from '@nestjs/common';
|
||
import { APP_FILTER } from '@nestjs/core';
|
||
import { LoggerModule } from 'nestjs-pino';
|
||
import type { IncomingMessage, ServerResponse } from 'node:http';
|
||
import { AllExceptionsFilter } from './all-exceptions.filter';
|
||
import { HealthController } from './health.controller';
|
||
import { RequestIdMiddleware, resolveRequestId } from './request-id.middleware';
|
||
|
||
@Module({
|
||
imports: [
|
||
LoggerModule.forRoot({
|
||
pinoHttp: {
|
||
// 与 RequestIdMiddleware 共用同一解析函数:优先透传 x-request-id,
|
||
// 其次复用已生成的 req.id,否则生成 UUID;并回写响应头。
|
||
genReqId: (req: IncomingMessage, res: ServerResponse) =>
|
||
resolveRequestId(req, res),
|
||
redact: {
|
||
paths: ['req.headers.authorization', 'res.headers.authorization'],
|
||
censor: '[REDACTED]',
|
||
},
|
||
level: process.env.LOG_LEVEL ?? 'info',
|
||
},
|
||
}),
|
||
],
|
||
controllers: [HealthController],
|
||
providers: [{ provide: APP_FILTER, useClass: AllExceptionsFilter }],
|
||
})
|
||
export class AppCnModule implements NestModule {
|
||
configure(consumer: MiddlewareConsumer): void {
|
||
consumer.apply(RequestIdMiddleware).forRoutes('*');
|
||
}
|
||
}
|