feat: 添加 NestJS API 基础设施
This commit is contained in:
@@ -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();
|
||||
Reference in New Issue
Block a user