Files
studio-admin-be/src/main.ts
T
2026-07-01 17:30:04 +09:00

60 lines
1.5 KiB
TypeScript

import { RequestMethod, ValidationPipe } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const configService = app.get(ConfigService);
const port = configService.get<number>('PORT', 4500);
const corsOrigin = configService.get<string>(
'CORS_ORIGIN',
'http://localhost:5173,http://localhost:5174',
);
app.setGlobalPrefix('api', {
exclude: [
{ path: 'health', method: RequestMethod.GET },
{ path: 'health/db', method: RequestMethod.GET },
],
});
app.enableCors({
origin: resolveCorsOrigin(corsOrigin),
credentials: true,
});
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
transform: true,
forbidNonWhitelisted: true,
}),
);
await app.listen(port, '0.0.0.0');
}
void bootstrap();
function resolveCorsOrigin(corsOrigin: string) {
if (corsOrigin === '*') {
return true;
}
const allowedOrigins = corsOrigin.split(',').map((origin) => origin.trim());
return (origin: string | undefined, callback: (error: Error | null, allow?: boolean) => void) => {
if (!origin) {
callback(null, true);
return;
}
if (allowedOrigins.includes(origin) || /^http:\/\/(localhost|127\.0\.0\.1):\d+$/.test(origin)) {
callback(null, true);
return;
}
callback(new Error(`CORS origin is not allowed: ${origin}`));
};
}