using Microsoft.AspNetCore.Mvc; using System.Diagnostics; namespace Convert_It_Online.Controllers { [Route("api/[controller]")] [ApiController] public class HealthController : ControllerBase { private readonly ILogger _logger; private static readonly DateTime _startTime = DateTime.UtcNow; public HealthController(ILogger 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 }); } } } }