import { NestFactory } from '@nestjs/core'; import { ValidationPipe, Logger } from '@nestjs/common'; import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; import { MicroserviceOptions, Transport } from '@nestjs/microservices'; import { ConfigService } from '@nestjs/config'; import { AppModule } from './app.module'; async function bootstrap() { const logger = new Logger('Bootstrap'); const app = await NestFactory.create(AppModule); const configService = app.get(ConfigService); const port = configService.get('PORT', 3005); const appName = configService.get('APP_NAME', 'reward-service'); // 全局验证管道 app.useGlobalPipes( new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true, }), ); // 跨域配置 app.enableCors(); // API 前缀 app.setGlobalPrefix('api/v1'); // Swagger 配置 const config = new DocumentBuilder() .setTitle('Reward Service API') .setDescription('RWA榴莲皇后平台 - 权益奖励微服务 API 文档') .setVersion('1.0') .addBearerAuth() .build(); const document = SwaggerModule.createDocument(app, config); SwaggerModule.setup('api', app, document); // Kafka 微服务 - 用于 @MessagePattern 消费消息 const kafkaBrokers = process.env.KAFKA_BROKERS?.split(',') || ['localhost:9092']; const kafkaGroupId = process.env.KAFKA_GROUP_ID || 'reward-service-group'; app.connectMicroservice({ transport: Transport.KAFKA, options: { client: { clientId: 'reward-service', brokers: kafkaBrokers, }, consumer: { groupId: kafkaGroupId, }, }, }); await app.startAllMicroservices(); logger.log('Kafka microservice started'); await app.listen(port); logger.log(`${appName} is running on port ${port}`); logger.log(`Swagger documentation available at http://localhost:${port}/api`); } bootstrap();