Files
RayLab-Core/src/main.ts
T
Rayyan 470396c2f1
Deploy / deploy (push) Successful in 34s
fix cors
2026-08-03 20:32:17 +07:00

65 lines
1.7 KiB
TypeScript

import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { ConfigService } from '@nestjs/config';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const config = app.get(ConfigService);
app.setGlobalPrefix('');
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
transform: true,
forbidNonWhitelisted: true,
}),
);
// CORS configuration: only allow configured frontend origins and enable credentials
const allowedOrigins: string[] = [];
const frontend = config.get<string>('FRONTEND_URL');
const prodFrontend = config.get<string>('PRODUCTION_FRONTEND_URL');
if (frontend) allowedOrigins.push(frontend);
if (prodFrontend) allowedOrigins.push(prodFrontend);
app.enableCors({
origin: allowedOrigins,
credentials: true,
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
allowedHeaders: [
'Content-Type',
'Authorization',
'Accept',
'Origin',
'X-Requested-With',
],
});
const swaggerEnabled = config.get<string>('SWAGGER_ENABLED') === 'true';
if (swaggerEnabled) {
const swaggerConfig = new DocumentBuilder()
.setTitle('RayLab Core API')
.setDescription('RayLab Core REST API')
.setVersion('1.0.0')
.addBearerAuth()
.build();
const document = SwaggerModule.createDocument(app, swaggerConfig);
SwaggerModule.setup('ApiList', app, document);
}
const port = config.get<number>('PORT') || 3000;
await app.listen(port);
console.log(`Server running on http://localhost:${port}`);
}
bootstrap();