main.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { NestFactory } from '@nestjs/core';
  2. import { Logger, ValidationPipe } from '@nestjs/common';
  3. import { ConfigService } from '@nestjs/config';
  4. import helmet from 'helmet';
  5. import compression from 'compression';
  6. import { AppModule } from './app.module';
  7. async function bootstrap() {
  8. const app = await NestFactory.create(AppModule, {
  9. bufferLogs: true,
  10. });
  11. const logger = new Logger('Bootstrap');
  12. app.useLogger(logger);
  13. const configService = app.get(ConfigService);
  14. const host =
  15. configService.get<string>('APP_HOST') ??
  16. configService.get<string>('HOST') ??
  17. '0.0.0.0';
  18. const port =
  19. configService.get<number>('APP_PORT') ?? Number(process.env.PORT ?? 3301);
  20. const crossOrigin =
  21. configService.get<string>('APP_CROSS_ORIGIN') ??
  22. configService.get<string>('CROSS_ORIGIN') ??
  23. '*';
  24. app.enableCors({
  25. origin:
  26. crossOrigin === '*' ? true : crossOrigin.split(',').map((o) => o.trim()),
  27. methods: 'GET,PUT,PATCH,POST,DELETE',
  28. });
  29. // 👇 Important: this makes /health become /api/v1/health
  30. app.setGlobalPrefix('api/v1', {
  31. exclude: ['/'],
  32. });
  33. app.use(helmet());
  34. app.use(compression());
  35. app.enableCors({
  36. origin: true,
  37. credentials: true,
  38. });
  39. app.useGlobalPipes(
  40. new ValidationPipe({
  41. whitelist: true,
  42. transform: true,
  43. transformOptions: {
  44. enableImplicitConversion: true,
  45. },
  46. forbidNonWhitelisted: false,
  47. }),
  48. );
  49. await app.listen(port, host);
  50. const url = `http://${host}:${port}`;
  51. logger.log(`🚀 box-app-api listening on ${url} (global prefix: /api/v1)`);
  52. }
  53. bootstrap().catch((error) => {
  54. // eslint-disable-next-line no-console
  55. console.error('❌ Failed to bootstrap box-app-api', error);
  56. process.exit(1);
  57. });