-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
80 lines (66 loc) · 2.01 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
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
/**
* * Dependencies
*/
import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import rateLimit from 'express-rate-limit';
import morgan from 'morgan';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import helmet from 'helmet';
/**
* * Modules
*/
import { AppModule } from './app.module';
/**
* * Serivces
*/
import { ConfigService } from './core/config/config.service';
import { UtilsService } from './core/utils/utils.service';
/**
* * App info
*/
import { version, name as AppName } from '../package.json';
import { UAT } from './core/data/@types/enitity';
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule);
const configService: ConfigService = app.get(ConfigService);
const utilsService: UtilsService = app.get(UtilsService);
// ? hide 'x-powered-by' header
app.use(helmet.hidePoweredBy());
// ? cors
app.enableCors();
// ? get the real IP in case this app will run behind a reverse proxy
app.set('trust proxy', 1);
// ? rate limit protection
app.use(
rateLimit({
windowMs: utilsService.stringToMs(
configService.get('WINDOWMS_RATE_LIMIT'),
),
max: parseInt(configService.get('RATE_LIMIT_MAX_REQ')), // ? limit each IP to x requests per windowMs
}),
);
// ? logger
app.use(
morgan(
':method :url Req-Header :req[header] Status :status Content-Length :res[content-length] Res-Time :response-time ms IP :remote-addr',
),
);
// ? swagger
const config = new DocumentBuilder()
.setTitle(AppName)
.setDescription('API documentation')
.setVersion(version)
.build();
const document = SwaggerModule.createDocument(app, config, {
extraModels: [UAT],
});
SwaggerModule.setup(`documentation`, app, document);
// ? start the server
await app.listen(configService.get('PORT'), () => {
console.log(
`app up in ${process.env.NODE_ENV} at ${configService.get('PORT')}`,
);
});
}
bootstrap();