80 lines
2.5 KiB
TypeScript
80 lines
2.5 KiB
TypeScript
import { NestFactory } from '@nestjs/core';
|
|
import { ValidationPipe } from '@nestjs/common';
|
|
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import helmet from 'helmet';
|
|
import { AppModule } from './app.module';
|
|
import { writeFileSync } from 'fs';
|
|
import { join } from 'path';
|
|
|
|
async function bootstrap() {
|
|
const app = await NestFactory.create(AppModule);
|
|
const configService = app.get(ConfigService);
|
|
|
|
// Security
|
|
app.use(helmet());
|
|
app.enableCors({
|
|
origin: [
|
|
'http://localhost:3000', // local Swagger UI
|
|
'https://editor.swagger.io', // Swagger Editor online
|
|
],
|
|
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
|
|
credentials: true,
|
|
});
|
|
|
|
// ✅ Global prefix
|
|
const globalPrefix = configService.get('API_PREFIX', 'api/v1');
|
|
app.setGlobalPrefix(globalPrefix);
|
|
|
|
// Request logging middleware
|
|
app.use((req, res, next) => {
|
|
console.log(`📝 ${new Date().toISOString()} - ${req.method} ${req.url}`);
|
|
next();
|
|
});
|
|
|
|
// Global validation pipe
|
|
app.useGlobalPipes(
|
|
new ValidationPipe({
|
|
whitelist: true,
|
|
forbidNonWhitelisted: true,
|
|
transform: true,
|
|
transformOptions: {
|
|
enableImplicitConversion: true,
|
|
},
|
|
}),
|
|
);
|
|
const port = configService.get('PORT', 3000);
|
|
|
|
// Swagger documentation (only in development)
|
|
if (configService.get('NODE_ENV') !== 'production') {
|
|
const config = new DocumentBuilder()
|
|
.setTitle('NestJS Serverless User CRUD API')
|
|
.setDescription('Professional NestJS serverless application with user CRUD and authentication')
|
|
.setVersion(configService.get('API_VERSION', '1.0.0'))
|
|
.addBearerAuth()
|
|
.addServer(`${process.env.SERVER_URL}/`, 'Local Server')
|
|
.build();
|
|
|
|
const document = SwaggerModule.createDocument(app, config);
|
|
SwaggerModule.setup('api/docs', app, document);
|
|
|
|
const outputPath = join(process.cwd(), 'swagger.json');
|
|
writeFileSync(outputPath, JSON.stringify(document, null, 2), 'utf8');
|
|
console.log(`✅ Swagger JSON file generated at: ${outputPath}`);
|
|
}
|
|
|
|
// const port = configService.get('PORT', 3000);
|
|
await app.listen(port);
|
|
|
|
console.log(`🚀 Application is running on: http://localhost:${port}`);
|
|
if (configService.get('NODE_ENV') !== 'production') {
|
|
console.log(`📚 Swagger documentation: http://localhost:${port}/api/docs`);
|
|
}
|
|
}
|
|
|
|
// Only bootstrap if not running in Lambda
|
|
if (process.env.AWS_LAMBDA_FUNCTION_NAME === undefined) {
|
|
bootstrap();
|
|
}
|
|
|