2026-09-17 19:14:41 +08:00
|
|
|
|
// services/api/src/app-cn.module.ts
|
2026-09-17 19:51:56 +08:00
|
|
|
|
// 根模块:Prisma(@Global,封装 @photo/db-cn)、日志(nestjs-pino / pino-http)、
|
|
|
|
|
|
// request-id 中间件、全局异常过滤器、/v1/health。
|
|
|
|
|
|
// 功能模块(auth/albums/uploads/photos/public)自 Task 12+ 起逐个加入。
|
2026-09-17 19:14:41 +08:00
|
|
|
|
// 过滤器经 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';
|
2026-09-17 19:51:56 +08:00
|
|
|
|
import { PrismaModule } from './prisma/prisma.module';
|
2026-09-17 19:14:41 +08:00
|
|
|
|
import { RequestIdMiddleware, resolveRequestId } from './request-id.middleware';
|
|
|
|
|
|
|
|
|
|
|
|
@Module({
|
|
|
|
|
|
imports: [
|
2026-09-17 19:51:56 +08:00
|
|
|
|
// @Global:功能模块(Task 12+)直接注入 PrismaService,无需 re-import。
|
|
|
|
|
|
PrismaModule,
|
2026-09-17 19:14:41 +08:00
|
|
|
|
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('*');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|