main.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { NestFactory } from '@nestjs/core';
  2. import { json } from 'express';
  3. import { AppModule } from './app.module';
  4. import * as cookieParser from 'cookie-parser';
  5. import { VersioningType } from '@nestjs/common';
  6. import * as session from 'express-session';
  7. import { emitGQLSchemaFile } from './gql-schema';
  8. import { checkEnvironmentAuthProvider } from './utils';
  9. import { ConfigService } from '@nestjs/config';
  10. async function bootstrap() {
  11. const app = await NestFactory.create(AppModule);
  12. const configService = app.get(ConfigService);
  13. console.log(`Running in production: ${configService.get('PRODUCTION')}`);
  14. console.log(`Port: ${configService.get('PORT')}`);
  15. checkEnvironmentAuthProvider(
  16. configService.get('INFRA.VITE_ALLOWED_AUTH_PROVIDERS') ??
  17. configService.get('VITE_ALLOWED_AUTH_PROVIDERS'),
  18. );
  19. app.use(
  20. session({
  21. secret: configService.get('SESSION_SECRET'),
  22. }),
  23. );
  24. // Increase fil upload limit to 50MB
  25. app.use(
  26. json({
  27. limit: '100mb',
  28. }),
  29. );
  30. if (configService.get('PRODUCTION') === 'false') {
  31. console.log('Enabling CORS with development settings');
  32. app.enableCors({
  33. origin: configService.get('WHITELISTED_ORIGINS').split(','),
  34. credentials: true,
  35. });
  36. } else {
  37. console.log('Enabling CORS with production settings');
  38. app.enableCors({
  39. origin: configService.get('WHITELISTED_ORIGINS').split(','),
  40. credentials: true,
  41. });
  42. }
  43. app.enableVersioning({
  44. type: VersioningType.URI,
  45. });
  46. app.use(cookieParser());
  47. await app.listen(configService.get('PORT') || 3170);
  48. // Graceful shutdown
  49. process.on('SIGTERM', async () => {
  50. console.info('SIGTERM signal received');
  51. await app.close();
  52. });
  53. }
  54. if (!process.env.GENERATE_GQL_SCHEMA) {
  55. bootstrap();
  56. } else {
  57. emitGQLSchemaFile();
  58. }