39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
import { ValidationPipe } from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { NestFactory } from '@nestjs/core';
|
|
import compression from 'compression';
|
|
import { AppModule } from './app.module';
|
|
import { EnvironmentVariables } from './common/env.validation';
|
|
|
|
async function bootstrap() {
|
|
const app = await NestFactory.create(AppModule);
|
|
app.use(compression());
|
|
|
|
const config = app.get(ConfigService<EnvironmentVariables, true>);
|
|
const corsOrigins = config.get('CORS_ALLOWED_ORIGINS');
|
|
const corsAllowedOrigins = corsOrigins
|
|
? corsOrigins.split(',').map((origin: string) => origin.trim()).filter(Boolean)
|
|
: true;
|
|
|
|
// CORS настраивается через env, чтобы один build работал локально и в окружениях деплоя.
|
|
app.enableCors({
|
|
origin: corsAllowedOrigins,
|
|
credentials: true,
|
|
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
|
|
allowedHeaders: ['Content-Type', 'Authorization', 'x-api-key'],
|
|
});
|
|
|
|
// Whitelist защищает API от лишних полей в DTO и делает контракты запросов строгими.
|
|
app.useGlobalPipes(
|
|
new ValidationPipe({
|
|
transform: true,
|
|
whitelist: true,
|
|
forbidNonWhitelisted: true,
|
|
}),
|
|
);
|
|
|
|
await app.listen(config.get('PORT'));
|
|
}
|
|
|
|
void bootstrap();
|