-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.ts
More file actions
76 lines (64 loc) · 2.19 KB
/
Copy pathmain.ts
File metadata and controls
76 lines (64 loc) · 2.19 KB
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
import { BadRequestException, ValidationError, ValidationPipe } from '@nestjs/common'
import { NestFactory } from '@nestjs/core'
import { SwaggerModule } from '@nestjs/swagger'
import fs from 'fs'
import Helmet from 'helmet'
import { resolve } from 'path'
import { promisify } from 'util'
import { AppModule } from './app.module'
import { UnauthorizedExceptionFilter } from './common/filters/unauthorized-exception.filter'
import { ConfigService } from './config/config.service'
import { openapiOptions } from './openapi-options'
const writeFile = promisify(fs.writeFile)
declare const module: any
async function bootstrap(): Promise<void> {
const config: ConfigService = app.get('ConfigService')
let nestConfig = {}
if (config.TLS_KEY_PATH && config.TLS_CERT_PATH) {
const fs = require('fs');
nestConfig.httpsOptions = {
key: fs.readFileSync(config.TLS_KEY_PATH),
cert: fs.readFileSync(config.TLS_CERT_PATH)
};
}
const app = await NestFactory.create(AppModule, nestConfig);
if (config.SERVE_DUCKYPANEL) {
// Write baseurl to file for DuckyPanel to find
await writeFile(
resolve('node_modules/duckypanel/DuckyPanel/config/production.json'),
`{"apiUrl":"/${config.BASE_URL}"}`,
)
}
app.setGlobalPrefix(config.BASE_URL)
if (config.DELAY) {
app.use(function (req, res, next) {
setTimeout(next, config.DELAY)
})
}
app.enableCors()
app.useGlobalFilters(new UnauthorizedExceptionFilter())
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
validationError: { target: false, value: false },
exceptionFactory: (errors: ValidationError[]): BadRequestException =>
new BadRequestException(errors, 'ValidationError'),
}),
)
app.use(Helmet())
const document = SwaggerModule.createDocument(app, openapiOptions)
SwaggerModule.setup(`${config.BASE_URL}/swagger`, app, document, {
swaggerOptions: {
defaultModelsExpandDepth: 0,
displayRequestDuration: true,
displayOperationId: true,
},
})
await app.listen(config.PORT)
if (module.hot) {
module.hot.accept()
module.hot.dispose((): Promise<void> => app.close())
}
}
bootstrap()