73 lines
2.0 KiB
TypeScript
73 lines
2.0 KiB
TypeScript
import { ValidationPipe, Logger } from '@nestjs/common';
|
|
import cookieParser from 'cookie-parser';
|
|
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);
|
|
const logger = new Logger('Bootstrap');
|
|
|
|
// enable cookie parser so req.cookies is populated
|
|
app.use(cookieParser());
|
|
|
|
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;
|
|
|
|
logger.log(`Allowed CORS origins: ${JSON.stringify(allowedOrigins)}`);
|
|
logger.log(`Server listening on port ${port}`);
|
|
|
|
await app.listen(port);
|
|
|
|
logger.log(`Server running on http://localhost:${port}`);
|
|
}
|
|
|
|
bootstrap(); |