|
1 | | -import { ValidationPipe, VersioningType } from '@nestjs/common' |
2 | | -import { ConfigService } from '@nestjs/config' |
3 | | -import { NestFactory } from '@nestjs/core' |
4 | | -import { NestExpressApplication } from '@nestjs/platform-express' |
5 | | -import helmet from 'helmet' |
6 | | -import i18n from 'i18n' |
| 1 | +import type { NestExpressApplication } from '@nestjs/platform-express'; |
| 2 | +import { NestFactory } from '@nestjs/core'; |
| 3 | +import { ConfigService } from '@nestjs/config'; |
| 4 | +import { Logger } from '@nestjs/common'; |
7 | 5 |
|
8 | | -import { AppModule } from './app.module' |
| 6 | +import { AppModule } from './app.module'; |
9 | 7 |
|
10 | | -async function bootstrap() { |
11 | | - // Hybrid application |
12 | | - // can be used as a microservice or a web service both |
| 8 | +async function bootstrap(): Promise<void> { |
| 9 | + const logger = new Logger('Bootstrap'); |
13 | 10 | const app = await NestFactory.create<NestExpressApplication>(AppModule, { |
14 | | - bufferLogs: true |
15 | | - }) |
16 | | - |
17 | | - // Initialize i18n |
18 | | - app.use(i18n.init) |
19 | | - |
20 | | - // global service prefix |
21 | | - const prefix = app.get(ConfigService).get('http.prefix') ?? '' |
22 | | - app.setGlobalPrefix(prefix) |
23 | | - |
24 | | - // Version control like v1 v2 |
25 | | - app.enableVersioning({ |
26 | | - type: VersioningType.URI |
27 | | - }) |
28 | | - |
29 | | - // DTO pipe settings |
30 | | - app.useGlobalPipes( |
31 | | - new ValidationPipe({ |
32 | | - transform: true, |
33 | | - whitelist: true |
34 | | - }) |
35 | | - ) |
36 | | - |
37 | | - // avoid attack |
38 | | - app.use(helmet()) |
39 | | - |
40 | | - const port = app.get(ConfigService).get('http.port') ?? 8082 |
41 | | - // As a web service |
42 | | - await app |
43 | | - .listen(port, '0.0.0.0') |
44 | | - .catch((error) => { |
45 | | - console.error(`MS_Document start error:${error}`) |
46 | | - }) |
| 11 | + bufferLogs: true, |
| 12 | + }); |
| 13 | + const configService = app.get(ConfigService) as ConfigService; |
| 14 | + app.setGlobalPrefix(configService.get('http.prefix')!); |
| 15 | + const port = configService.get('http.port'); |
| 16 | + |
| 17 | + try { |
| 18 | + await app.listen(port, '0.0.0.0'); |
| 19 | + logger.log(`document-service running at http://localhost:${port}`); |
| 20 | + } catch (error) { |
| 21 | + logger.error('Failed to start document-service', error); |
| 22 | + throw new Error( |
| 23 | + `Bootstrap failed: ${error instanceof Error ? error.message : error}`, |
| 24 | + ); |
| 25 | + } |
47 | 26 | } |
48 | | -bootstrap() |
| 27 | + |
| 28 | +bootstrap(); |
0 commit comments