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';
|
2026-09-17 20:54:30 +08:00
|
|
|
|
import { resolveLogLevel } from './shared/log-level';
|
|
|
|
|
|
import type { ServerRequestWithId } from './shared/request-with-id';
|
2026-09-17 19:14:41 +08:00
|
|
|
|
|
|
|
|
|
|
@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) =>
|
2026-09-17 20:54:30 +08:00
|
|
|
|
resolveRequestId(req as ServerRequestWithId, res),
|
2026-09-17 19:14:41 +08:00
|
|
|
|
redact: {
|
2026-09-17 20:54:30 +08:00
|
|
|
|
// 授权头 + Cookie(会话凭据,Task 7 遗留项③):请求/响应两侧都打码,
|
|
|
|
|
|
// 防止日志聚合层留存可重放的凭据。
|
|
|
|
|
|
paths: [
|
|
|
|
|
|
'req.headers.authorization',
|
|
|
|
|
|
'req.headers.cookie',
|
|
|
|
|
|
'res.headers.authorization',
|
|
|
|
|
|
'res.headers.set-cookie',
|
|
|
|
|
|
],
|
2026-09-17 19:14:41 +08:00
|
|
|
|
censor: '[REDACTED]',
|
|
|
|
|
|
},
|
2026-09-17 20:54:30 +08:00
|
|
|
|
// Task 7 遗留项⑤:非法 LOG_LEVEL 在模块装配期(即启动期)快速失败,
|
|
|
|
|
|
// 而不是让 pino 首次写日志时才炸或静默吞掉输出。
|
|
|
|
|
|
level: resolveLogLevel(process.env.LOG_LEVEL),
|
2026-09-17 19:14:41 +08:00
|
|
|
|
},
|
|
|
|
|
|
}),
|
|
|
|
|
|
],
|
|
|
|
|
|
controllers: [HealthController],
|
|
|
|
|
|
providers: [{ provide: APP_FILTER, useClass: AllExceptionsFilter }],
|
|
|
|
|
|
})
|
|
|
|
|
|
export class AppCnModule implements NestModule {
|
|
|
|
|
|
configure(consumer: MiddlewareConsumer): void {
|
|
|
|
|
|
consumer.apply(RequestIdMiddleware).forRoutes('*');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|