Convert-it/Controllers/HealthController.cs
Ricardo Carneiro 84b058904f
All checks were successful
Deploy ASP.NET MVC to OCI / build-and-deploy (push) Successful in 9m32s
fix: adiversos ajustes e mais 1 conversor
2025-09-14 21:26:16 -03:00

100 lines
3.8 KiB
C#

using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
namespace Convert_It_Online.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class HealthController : ControllerBase
{
private readonly ILogger<HealthController> _logger;
private static readonly DateTime _startTime = DateTime.UtcNow;
public HealthController(ILogger<HealthController> logger)
{
_logger = logger;
}
[HttpGet]
[Route("")]
[Route("check")]
public IActionResult HealthCheck()
{
try
{
var uptimeMinutes = (DateTime.UtcNow - _startTime).TotalMinutes;
var memoryBytes = GC.GetTotalMemory(false);
var memoryMB = Math.Round(memoryBytes / 1024.0 / 1024.0, 2);
_logger.LogInformation("[HEALTH] Memory: {memoryBytes} MemoryMB: {memoryMB} ThreadPool: {threadPool} ProcessId: {processId} ActiveConnections: {activeConnections} UptimeMinutes: {uptimeMinutes}",
memoryBytes,
memoryMB,
ThreadPool.ThreadCount,
Process.GetCurrentProcess().Id,
HttpContext.Connection?.Id ?? "null",
Math.Round(uptimeMinutes, 1));
var response = new
{
status = "healthy",
timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss UTC"),
uptime = new
{
minutes = Math.Round(uptimeMinutes, 1),
hours = Math.Round(uptimeMinutes / 60, 1),
days = Math.Round(uptimeMinutes / 60 / 24, 1)
},
memory = new
{
bytes = memoryBytes,
mb = memoryMB,
kb = Math.Round(memoryBytes / 1024.0, 2)
},
system = new
{
processId = Process.GetCurrentProcess().Id,
threadPool = ThreadPool.ThreadCount,
environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Unknown",
machineName = Environment.MachineName,
framework = Environment.Version.ToString()
},
connection = new
{
id = HttpContext.Connection?.Id ?? "null",
remoteIp = HttpContext.Connection?.RemoteIpAddress?.ToString() ?? "unknown",
localIp = HttpContext.Connection?.LocalIpAddress?.ToString() ?? "unknown"
}
};
return Ok(response);
}
catch (Exception ex)
{
_logger.LogError(ex, "[HEALTH] Health check failed");
return StatusCode(500, new { status = "unhealthy", error = ex.Message });
}
}
[HttpGet("uptime-kuma")]
public IActionResult UptimeKuma()
{
try
{
var uptimeMinutes = (DateTime.UtcNow - _startTime).TotalMinutes;
_logger.LogInformation("[HEALTH-KUMA] Uptime check: {uptimeMinutes} minutes", Math.Round(uptimeMinutes, 1));
return Ok(new {
status = "ok",
uptime = Math.Round(uptimeMinutes, 1),
timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss UTC")
});
}
catch (Exception ex)
{
_logger.LogError(ex, "[HEALTH-KUMA] Uptime Kuma check failed");
return StatusCode(500, new { status = "error", message = ex.Message });
}
}
}
}