2025-12-18 23:05:04 +05:30
|
|
|
import { NestFactory } from '@nestjs/core';
|
2025-12-22 11:57:16 +05:30
|
|
|
import { ValidationPipe, Logger, BadRequestException } from '@nestjs/common';
|
2025-12-18 23:05:04 +05:30
|
|
|
import { ConfigService } from '@nestjs/config';
|
|
|
|
|
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
|
2026-01-24 22:19:26 +05:30
|
|
|
import { NestExpressApplication } from '@nestjs/platform-express';
|
2025-12-18 23:05:04 +05:30
|
|
|
import helmet from 'helmet';
|
2025-12-19 01:15:19 +05:30
|
|
|
import cookieParser from 'cookie-parser';
|
2025-12-18 23:05:04 +05:30
|
|
|
import { AppModule } from './app.module';
|
|
|
|
|
import { AllExceptionsFilter } from './common/filters';
|
|
|
|
|
import { TransformInterceptor } from './common/interceptors';
|
|
|
|
|
|
|
|
|
|
async function bootstrap() {
|
|
|
|
|
const logger = new Logger('Bootstrap');
|
2026-03-07 10:09:50 +05:30
|
|
|
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
|
|
|
|
|
rawBody: true,
|
|
|
|
|
});
|
2025-12-18 23:05:04 +05:30
|
|
|
|
|
|
|
|
const configService = app.get(ConfigService);
|
|
|
|
|
const port = configService.get<number>('app.port') || 3001;
|
|
|
|
|
const appEnv = configService.get<string>('app.env') || 'development';
|
|
|
|
|
const corsOrigins = configService.get<string[]>('cors.origins') || [];
|
|
|
|
|
|
|
|
|
|
// Security
|
|
|
|
|
app.use(helmet());
|
|
|
|
|
app.use(cookieParser());
|
|
|
|
|
|
|
|
|
|
// CORS
|
|
|
|
|
app.enableCors({
|
|
|
|
|
origin: corsOrigins,
|
|
|
|
|
credentials: true,
|
|
|
|
|
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
|
|
|
|
|
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With'],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Global prefix
|
|
|
|
|
app.setGlobalPrefix('api/v1');
|
|
|
|
|
|
|
|
|
|
// Global pipes
|
|
|
|
|
app.useGlobalPipes(
|
|
|
|
|
new ValidationPipe({
|
|
|
|
|
whitelist: true,
|
|
|
|
|
forbidNonWhitelisted: true,
|
|
|
|
|
transform: true,
|
|
|
|
|
transformOptions: {
|
|
|
|
|
enableImplicitConversion: true,
|
|
|
|
|
},
|
2025-12-22 11:57:16 +05:30
|
|
|
exceptionFactory: (errors) => {
|
|
|
|
|
const messages = errors.map((error) => {
|
|
|
|
|
const constraints = error.constraints
|
|
|
|
|
? Object.values(error.constraints)
|
|
|
|
|
: [`${error.property} is invalid`];
|
|
|
|
|
return {
|
|
|
|
|
field: error.property,
|
|
|
|
|
errors: constraints,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
return new BadRequestException({
|
|
|
|
|
message: 'Validation failed',
|
|
|
|
|
errors: messages,
|
|
|
|
|
});
|
|
|
|
|
},
|
2025-12-18 23:05:04 +05:30
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Global filters
|
|
|
|
|
app.useGlobalFilters(new AllExceptionsFilter());
|
|
|
|
|
|
|
|
|
|
// Global interceptors
|
|
|
|
|
app.useGlobalInterceptors(new TransformInterceptor());
|
|
|
|
|
|
|
|
|
|
// Swagger documentation (only in development)
|
|
|
|
|
if (appEnv !== 'production') {
|
|
|
|
|
const config = new DocumentBuilder()
|
|
|
|
|
.setTitle('Real Estate Agent Platform API')
|
|
|
|
|
.setDescription('API documentation for the Real Estate Agent Platform')
|
|
|
|
|
.setVersion('1.0')
|
|
|
|
|
.addBearerAuth(
|
|
|
|
|
{
|
|
|
|
|
type: 'http',
|
|
|
|
|
scheme: 'bearer',
|
|
|
|
|
bearerFormat: 'JWT',
|
|
|
|
|
name: 'JWT',
|
|
|
|
|
description: 'Enter JWT token',
|
|
|
|
|
in: 'header',
|
|
|
|
|
},
|
|
|
|
|
'JWT-auth',
|
|
|
|
|
)
|
|
|
|
|
.addTag('Auth', 'Authentication endpoints')
|
|
|
|
|
.addTag('Users', 'User management endpoints')
|
|
|
|
|
.addTag('Agents', 'Agent management endpoints')
|
|
|
|
|
.addTag('Health', 'Health check endpoints')
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
const document = SwaggerModule.createDocument(app, config);
|
|
|
|
|
SwaggerModule.setup('api/docs', app, document, {
|
|
|
|
|
swaggerOptions: {
|
|
|
|
|
persistAuthorization: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
logger.log(`Swagger documentation available at http://localhost:${port}/api/docs`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await app.listen(port);
|
|
|
|
|
logger.log(`Application is running on: http://localhost:${port}`);
|
|
|
|
|
logger.log(`Environment: ${appEnv}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bootstrap();
|