-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
55 lines (48 loc) · 1.93 KB
/
main.ts
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
import { join } from 'path';
import { NestFactory } from '@nestjs/core';
import { ConfigService } from '@nestjs/config';
import { RequestMethod } from '@nestjs/common';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { NestExpressApplication } from '@nestjs/platform-express';
import { Logger, LoggerErrorInterceptor } from 'nestjs-pino';
import otelSDK from './otel';
import { AppModule } from './app.module';
async function bootstrap() {
await otelSDK.start();
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
bufferLogs: true,
});
app.setGlobalPrefix('api', {
exclude: [{ path: 'health', method: RequestMethod.GET }],
});
app.useLogger(app.get(Logger));
app.useGlobalInterceptors(new LoggerErrorInterceptor());
app.useStaticAssets(join(__dirname, 'static'));
const configService = app.get(ConfigService);
// Setup CORS
const allowedOrigins = configService.get<string[]>('cors.allowedOrigins');
const allowedMethods = configService.get<string[]>('cors.allowedMethods');
if (allowedOrigins && allowedOrigins.length > 0) {
app.enableCors({
origin: allowedOrigins.map((o) => new RegExp(o)),
methods: allowedMethods,
allowedHeaders: ['Origin', 'X-Requested-With', 'Content-Type', 'Accept', 'Authorization', 'X-Forwarded-for'],
preflightContinue: false,
});
}
// Setup Swagger
const devMode = configService.get<boolean>('devMode');
if (devMode) {
const swaggerConfig = new DocumentBuilder()
.setTitle('Desk Compass')
.setDescription('CRUD and management of necessary data for desk compass')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, swaggerConfig);
SwaggerModule.setup('api/swagger', app, document);
}
await app.listen(configService.get<number>('appPort'));
const appUrl = await app.getUrl();
app.get(Logger).log('Started application at ' + appUrl);
}
bootstrap();