import 'reflect-metadata';
import { json } from 'express';
import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { PrismaExceptionFilter } from './common/prisma-exception.filter';
import { getAppConfig } from './config/env';
import { AppModule } from './app.module';

async function bootstrap() {
  const config = getAppConfig();
  const app = await NestFactory.create(AppModule);
  app.use(
    json({
      verify: (request: { rawBody?: Buffer } & object, _response, buffer) => {
        request.rawBody = Buffer.from(buffer);
      },
    }),
  );
  app.setGlobalPrefix('api');
  app.getHttpAdapter().getInstance().disable('x-powered-by');
  app.enableCors({
    origin: config.corsOrigins,
    credentials: true,
  });
  app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true,
      forbidNonWhitelisted: true,
      transform: true,
    }),
  );
  app.useGlobalFilters(new PrismaExceptionFilter());
  app.enableShutdownHooks();
  await app.listen(config.port);
}
bootstrap();
