- not_configured 하드코딩 제거 - RedisConnection 주입 후 PingAsync()로 실제 연결 상태 확인 - 응답에 latency 포함 Closes #156
99 lines
3.0 KiB
C#
99 lines
3.0 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.Caching;
|
|
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 RedisConnection _redisConnection;
|
|
private readonly RabbitMQConnection _rabbitConnection;
|
|
private readonly RabbitMQInitializer _rabbitInitializer;
|
|
|
|
public PublicController(
|
|
AppDbContext dbContext,
|
|
RedisConnection redisConnection,
|
|
RabbitMQConnection rabbitConnection,
|
|
RabbitMQInitializer rabbitInitializer)
|
|
{
|
|
_dbContext = dbContext;
|
|
_redisConnection = redisConnection;
|
|
_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 연결 확인
|
|
try
|
|
{
|
|
var db = await _redisConnection.GetDatabaseAsync();
|
|
var pong = await db.PingAsync();
|
|
checks["redis"] = new { status = "healthy", latency = $"{pong.TotalMilliseconds:F1}ms" };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
checks["redis"] = new { status = "unhealthy", error = ex.Message };
|
|
allHealthy = false;
|
|
}
|
|
|
|
// 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
|
|
});
|
|
}
|
|
}
|