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

67 lines
1.7 KiB
TypeScript

import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
// 全局验证管道
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
transform: true,
forbidNonWhitelisted: true,
}),
);
// CORS
app.enableCors({
origin: process.env.CORS_ORIGIN || '*',
credentials: true,
});
// 全局前缀
app.setGlobalPrefix('api/v1');
// Swagger 文档
const config = new DocumentBuilder()
.setTitle('Mining Service API')
.setDescription('挖矿服务 API 文档 - 积分股分配与销毁')
.setVersion('1.0')
.addBearerAuth()
.addTag('Mining', '挖矿相关')
.addTag('Price', '价格相关')
.addTag('Health', '健康检查')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api/docs', app, document);
// 连接 Kafka 微服务
const kafkaBrokers = process.env.KAFKA_BROKERS || 'localhost:9092';
app.connectMicroservice<MicroserviceOptions>({
transport: Transport.KAFKA,
options: {
client: {
clientId: 'mining-service',
brokers: kafkaBrokers.split(','),
},
consumer: {
groupId: 'mining-service-group',
},
},
});
await app.startAllMicroservices();
const port = process.env.PORT || 3021;
await app.listen(port);
console.log(`Mining Service is running on port ${port}`);
console.log(`Swagger docs: http://localhost:${port}/api/docs`);
}
bootstrap();