feat: 添加 NestJS API 基础设施

This commit is contained in:
root
2026-09-14 12:32:46 +08:00
parent c37101e783
commit f3fb16dbc0
22 changed files with 2213 additions and 5 deletions
+40
View File
@@ -0,0 +1,40 @@
import "reflect-metadata";
import { ValidationPipe, type INestApplication } from "@nestjs/common";
import { NestFactory } from "@nestjs/core";
import helmet from "helmet";
import { AppModule } from "./app.module.js";
import { HttpResponseInterceptor } from "./common/http-response.interceptor.js";
import { DomainExceptionFilter } from "./common/domain-exception.filter.js";
export function configureApp(app: INestApplication): void {
const allowedOrigin = process.env.WEB_ORIGIN ?? "http://localhost:3000";
app.setGlobalPrefix("api/v1");
app.use(helmet());
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
transform: true,
}),
);
app.useGlobalInterceptors(new HttpResponseInterceptor());
app.useGlobalFilters(new DomainExceptionFilter());
app.enableCors({
origin(
origin: string | undefined,
callback: (error: Error | null, allow?: boolean) => void,
) {
callback(null, origin === undefined || origin === allowedOrigin);
},
credentials: true,
});
}
async function bootstrap(): Promise<void> {
const app = await NestFactory.create(AppModule);
configureApp(app);
app.enableShutdownHooks();
await app.listen(Number(process.env.PORT ?? 3001), "0.0.0.0");
}
if (process.env.NODE_ENV !== "test") void bootstrap();