Files
a-share-analysis/server/apps/api/src/health.controller.ts
T

24 lines
941 B
TypeScript

import { Controller, Get, HttpException, HttpStatus, Inject } from '@nestjs/common';
import { ApiOkResponse, ApiServiceUnavailableResponse, ApiTags } from '@nestjs/swagger';
import { HealthResponseDto } from '../../../libs/http-contracts/src/health.dto';
import { HealthService } from './health.service';
@ApiTags('health')
@Controller('health')
/** 将内部健康结果映射为 200/503,同时保持两种响应共用同一 OpenAPI DTO。 */
export class HealthController {
constructor(@Inject(HealthService) private readonly healthService: HealthService) {}
@Get()
@ApiOkResponse({ type: HealthResponseDto })
@ApiServiceUnavailableResponse({ type: HealthResponseDto })
async getHealth(): Promise<HealthResponseDto> {
const response = await this.healthService.check();
if (response.status === 'unavailable') {
throw new HttpException(response, HttpStatus.SERVICE_UNAVAILABLE);
}
return response;
}
}