mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-08-25 21:59:40 +00:00
51 lines
1.6 KiB
TypeScript
51 lines
1.6 KiB
TypeScript
import { INestApplication, RequestMethod } from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { NestFactory } from '@nestjs/core';
|
|
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
|
import { Logger } from 'nestjs-pino';
|
|
import { initializeTransactionalContext } from 'typeorm-transactional';
|
|
import { AppModule } from './app.module';
|
|
const DEFAULT_PORT = 3000;
|
|
async function bootstrap() {
|
|
initializeTransactionalContext();
|
|
const app = await NestFactory.create(AppModule);
|
|
|
|
app.useLogger(app.get(Logger));
|
|
app.enableCors({
|
|
origin: '*',
|
|
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
|
|
preflightContinue: false,
|
|
optionsSuccessStatus: 204,
|
|
});
|
|
app.setGlobalPrefix('api', {
|
|
exclude: [
|
|
{ path: 'health', method: RequestMethod.GET },
|
|
{ path: 'health/details', method: RequestMethod.GET },
|
|
],
|
|
});
|
|
const config = app.get(ConfigService);
|
|
const swaggerDocument = await createSwagger(app);
|
|
|
|
if (config.getOrThrow<string>('NODE_ENV') === 'dev') {
|
|
SwaggerModule.setup(config.getOrThrow<string>('SWAGGER_API_DOCS_PATH'), app, swaggerDocument, {
|
|
swaggerOptions: {
|
|
docExpansion: 'none',
|
|
},
|
|
});
|
|
}
|
|
await app.listen(process.env.PORT ?? DEFAULT_PORT);
|
|
}
|
|
|
|
function createSwagger(app: INestApplication) {
|
|
const swaggerConfig = new DocumentBuilder()
|
|
.setTitle('Zod APIs')
|
|
.setDescription('API documentation for Zod app')
|
|
.setVersion('1.0')
|
|
.addBearerAuth({ in: 'header', type: 'http' })
|
|
.build();
|
|
|
|
return SwaggerModule.createDocument(app, swaggerConfig);
|
|
}
|
|
|
|
bootstrap();
|