File size: 2,676 Bytes
97dab2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import { NorthwindapiModule } from './northwindapi.module';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { ApploggerService } from '@bpm/common';
import * as dotenv from 'dotenv';
import * as express from 'express';

// Load environment variables from .env file
dotenv.config();

async function bootstrap() {
  const context = 'northwindapi';
  const logger = new ApploggerService(context);

  logger.log('πŸ› οΈ  Starting up the NestJS Application...\n', context);
  logger.log('πŸ“¦ Loading modules...\n', context);

  const app = await NestFactory.create(NorthwindapiModule);

  // enable dto validation
  app.useGlobalPipes(new ValidationPipe({
    transform: true, // enables class-transformer
  }));

  //disable the dto validation
  // app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }));


  app.enableCors({
    origin: [/http:\/\/localhost:\d+$/], // Allow all requests from localhost on any port
    credentials: true, // Enable credentials if needed (cookies, auth headers, etc.)
    methods: ['GET','POST','PUT','DELETE'],
    allowedHeaders: '*',
  });

  app.setGlobalPrefix('api'); // πŸ‘ˆ This sets /api as the prefix

  app.use(express.json());


  const config = new DocumentBuilder()
    .setTitle('Northwind API')
    .setDescription('Northwind API description')
    .setVersion('1.0')
    .addTag('northwind')
    .build();
  const documentFactory = () => SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('swagger', app, documentFactory, {
    jsonDocumentUrl: 'swagger/json',
  });

  const port = process.env.PORT || 3002;

  try {
    await app.listen(port, () => {
      logger.log('βœ… App Modules initialized successfully\n', context);
      logger.log('🌐 Enabling global configurations...\n', context);

      logger.log(
        `πŸš€ Application is running at: http://localhost:${port}\n`,
        context,
      );
      logger.log('πŸ“‘ Ready to accept incoming requests!\n', context);
      logger.log('🧠 Powered by NestJS ❀️\n', context);
    });
  } catch (err) {
    logger.error('❌ Failed to start application\n', err.stack);
    if (app) {
      await app.close();
    }
  }

  // OS signal listeners (optional)
  process.on('SIGINT', async () => {
    logger.warn('πŸ›‘ SIGINT received. Gracefully shutting down...', context);
    await app.close();
    process.exit(1);
  });

  process.on('SIGTERM', async () => {
    logger.warn('πŸ›‘ SIGTERM received. Gracefully shutting down...', context);
    await app.close();
    process.exit(1);
  });
}
bootstrap();