SPMS_API/SPMS.API/Controllers/PublicController.cs
seonkyu.kim f70d8a8558 fix: Health check 503 응답에 상세 데이터 포함 (#126)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-10 19:27:44 +09:00

85 lines
2.5 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Swashbuckle.AspNetCore.Annotations;
using SPMS.Domain.Common;
using SPMS.Infrastructure;
using SPMS.Infrastructure.Messaging;
namespace SPMS.API.Controllers;
[ApiController]
[Route("v1/out")]
[AllowAnonymous]
[ApiExplorerSettings(GroupName = "public")]
public class PublicController : ControllerBase
{
private readonly AppDbContext _dbContext;
private readonly RabbitMQConnection _rabbitConnection;
private readonly RabbitMQInitializer _rabbitInitializer;
public PublicController(
AppDbContext dbContext,
RabbitMQConnection rabbitConnection,
RabbitMQInitializer rabbitInitializer)
{
_dbContext = dbContext;
_rabbitConnection = rabbitConnection;
_rabbitInitializer = rabbitInitializer;
}
[HttpPost("health")]
[SwaggerOperation(Summary = "서버 상태 확인", Description = "MariaDB, Redis, RabbitMQ 연결 상태를 확인합니다.")]
public async Task<IActionResult> HealthCheckAsync()
{
var checks = new Dictionary<string, object>();
var allHealthy = true;
// 1. MariaDB 연결 확인
try
{
await _dbContext.Database.ExecuteSqlRawAsync("SELECT 1");
checks["database"] = new { status = "healthy" };
}
catch (Exception ex)
{
checks["database"] = new { status = "unhealthy", error = ex.Message };
allHealthy = false;
}
// 2. Redis 연결 확인 (Phase 3-2에서 구현 예정)
checks["redis"] = new { status = "not_configured" };
// 3. RabbitMQ 연결 확인
var rabbitConnected = _rabbitConnection.IsConnected;
var rabbitInitialized = _rabbitInitializer.IsInitialized;
if (rabbitConnected && rabbitInitialized)
{
checks["rabbitmq"] = new { status = "healthy" };
}
else
{
checks["rabbitmq"] = new
{
status = "unhealthy",
connected = rabbitConnected,
initialized = rabbitInitialized
};
allHealthy = false;
}
if (allHealthy)
{
return Ok(ApiResponse<object>.Success(checks));
}
return StatusCode(503, new ApiResponse<object>
{
Result = false,
Code = ErrorCodes.InternalError,
Msg = "하나 이상의 서비스에 문제가 있습니다.",
Data = checks
});
}
}