rwadurian/backend/services/blockchain-service/src/main.ts

74 lines
2.1 KiB
TypeScript

import { NestFactory } from '@nestjs/core';
import { ValidationPipe, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { AppModule } from './app.module';
async function bootstrap() {
const logger = new Logger('Bootstrap');
const app = await NestFactory.create(AppModule);
const configService = app.get(ConfigService);
// 全局验证管道
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
transform: true,
forbidNonWhitelisted: true,
}),
);
// CORS
app.enableCors();
// 全局 API 前缀
const apiPrefix = configService.get<string>('app.apiPrefix', 'api/v1');
app.setGlobalPrefix(apiPrefix);
// Swagger 文档
const swaggerConfig = new DocumentBuilder()
.setTitle('Blockchain Service API')
.setDescription('RWA 区块链基础设施服务 API')
.setVersion('1.0')
.addTag('Health', '健康检查')
.addTag('Balance', '余额查询')
.addTag('Internal', '内部接口')
.build();
const document = SwaggerModule.createDocument(app, swaggerConfig);
SwaggerModule.setup('api', app, document);
// Kafka 微服务
const kafkaBrokers = configService.get<string[]>('kafka.brokers') || ['localhost:9092'];
const kafkaGroupId = configService.get<string>('kafka.groupId') || 'blockchain-service-group';
app.connectMicroservice<MicroserviceOptions>({
transport: Transport.KAFKA,
options: {
client: {
clientId: 'blockchain-service',
brokers: kafkaBrokers,
},
consumer: {
groupId: kafkaGroupId,
},
},
});
// 启动微服务
await app.startAllMicroservices();
logger.log('Kafka microservice started');
// 启动 HTTP 服务
const port = configService.get<number>('app.port', 3012);
await app.listen(port);
logger.log(`Blockchain service is running on port ${port}`);
logger.log(`Swagger docs available at http://localhost:${port}/api`);
}
bootstrap();