- helmet 安全头 + CORS_ORIGIN 白名单 + Swagger 仅非生产 - @nestjs/throttler 全局限流(60s/100 次) - @nestjs/schedule 定时清理过期 token 与旧日志 - API 时间统一 Unix 毫秒(TransformInterceptor 递归转换) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
36 lines
1.3 KiB
TypeScript
36 lines
1.3 KiB
TypeScript
import 'dotenv/config';
|
|
import { NestFactory } from '@nestjs/core';
|
|
import { ValidationPipe } from '@nestjs/common';
|
|
import { Logger } from 'nestjs-pino';
|
|
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
|
import helmet from 'helmet';
|
|
import { AppModule } from './app.module.js';
|
|
|
|
async function bootstrap(): Promise<void> {
|
|
const app = await NestFactory.create(AppModule, { bufferLogs: true });
|
|
app.setGlobalPrefix('api/v1');
|
|
app.use(helmet());
|
|
const corsOrigin = process.env.CORS_ORIGIN
|
|
? process.env.CORS_ORIGIN.split(',').map((s) => s.trim())
|
|
: ['http://localhost:5173'];
|
|
app.enableCors({ origin: corsOrigin, credentials: true });
|
|
app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }));
|
|
app.useLogger(app.get(Logger));
|
|
|
|
// Swagger 文档仅非生产环境开放(生产默认关闭)
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
const swaggerConfig = new DocumentBuilder()
|
|
.setTitle('系统管理 API')
|
|
.setDescription('投云科技系统管理 API 文档')
|
|
.setVersion('1.0')
|
|
.addBearerAuth()
|
|
.build();
|
|
SwaggerModule.setup('docs', app, SwaggerModule.createDocument(app, swaggerConfig));
|
|
}
|
|
|
|
const port = Number(process.env.PORT ?? 3000);
|
|
await app.listen(port);
|
|
app.get(Logger).log(`服务启动成功,端口 ${port}`);
|
|
}
|
|
await bootstrap();
|