import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import {
  Logger,
  RequestMethod,
  ValidationPipe,
  VersioningType,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NestExpressApplication } from '@nestjs/platform-express';
import * as bodyParser from 'body-parser';

async function bootstrap() {
  const logger = new Logger('main');

  const app = await NestFactory.create<NestExpressApplication>(AppModule);

  // configuration from .env file
  const configService = app.get(ConfigService);
  const port = configService.get<number>('PORT');

  // route body validation
  app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }));

  // prefix to all route exclude front folder
  app.setGlobalPrefix('api', {
    exclude: [
      { path: '/', method: RequestMethod.GET },
      { path: '/public', method: RequestMethod.GET },
    ],
  });

  // route version
  app.enableVersioning({
    type: VersioningType.URI,
  });

  app.use(bodyParser.json({ limit: '50mb' }));
  app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
  app.enableCors();

  await app.listen(port);

  logger.log(`=================================`);
  logger.log(`App listening on the port ${port}`);
  logger.log(`=================================`);
}
bootstrap();
