nest-project/src/main.ts

40 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-02-15 19:12:21 +08:00
import { HttpAdapterHost, NestFactory } from '@nestjs/core'
import { ValidationPipe } from '@nestjs/common'
2023-02-15 20:03:13 +08:00
import { ConfigService } from '@nestjs/config'
2023-02-15 21:26:41 +08:00
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'
2023-02-15 19:12:21 +08:00
import { PrismaClientExceptionFilter, PrismaService } from 'nestjs-prisma'
2023-02-22 01:09:26 +08:00
import { JwtExceptionsFilter } from './common/filters/jwt-exceptions.filter'
import { AppModule } from './app.module'
2023-02-15 16:49:40 +08:00
async function bootstrap() {
const app = await NestFactory.create(AppModule)
2023-02-15 19:12:21 +08:00
// Validation
app.useGlobalPipes(new ValidationPipe({ whitelist: true }))
2023-02-15 19:12:21 +08:00
// enable shutdown hook
const prismaService = app.get(PrismaService)
await prismaService.enableShutdownHooks(app)
// Prisma Client Exception Filter for unhandled exceptions
const { httpAdapter } = app.get(HttpAdapterHost)
app.useGlobalFilters(new PrismaClientExceptionFilter(httpAdapter))
2023-02-22 01:09:26 +08:00
app.useGlobalFilters(new JwtExceptionsFilter(httpAdapter))
2023-02-15 19:12:21 +08:00
2023-02-15 21:26:41 +08:00
// Swagger Api
const options = new DocumentBuilder()
2023-02-16 16:41:34 +08:00
.addBearerAuth()
2023-02-15 21:26:41 +08:00
.setTitle('Nest Project')
.setDescription('The Nest-Project API description')
.setVersion('1.0')
.build()
const document = SwaggerModule.createDocument(app, options)
SwaggerModule.setup('api', app, document)
2023-02-15 20:03:13 +08:00
const configService = app.get(ConfigService)
2023-02-21 11:13:21 +08:00
const PORT = configService.get<number>('nest.port', 12400)
2023-02-15 20:03:13 +08:00
await app.listen(PORT)
2023-02-15 16:49:40 +08:00
}
bootstrap()