using Cleanuparr.Infrastructure.Health; using Microsoft.AspNetCore.Mvc; namespace Cleanuparr.Api.Controllers; /// /// Controller for checking the health of download clients /// [ApiController] [Route("api/health")] public class HealthCheckController : ControllerBase { private readonly ILogger _logger; private readonly IHealthCheckService _healthCheckService; /// /// Initializes a new instance of the class /// public HealthCheckController( ILogger logger, IHealthCheckService healthCheckService) { _logger = logger; _healthCheckService = healthCheckService; } /// /// Gets the health status of all download clients /// [HttpGet] public IActionResult GetAllHealth() { try { var healthStatuses = _healthCheckService.GetAllClientHealth(); return Ok(healthStatuses); } catch (Exception ex) { _logger.LogError(ex, "Error retrieving client health statuses"); return StatusCode(500, new { Error = "An error occurred while retrieving client health statuses" }); } } /// /// Gets the health status of a specific download client /// [HttpGet("{id:guid}")] public IActionResult GetClientHealth(Guid id) { try { var healthStatus = _healthCheckService.GetClientHealth(id); if (healthStatus == null) { return NotFound(new { Message = $"Health status for client with ID '{id}' not found" }); } return Ok(healthStatus); } catch (Exception ex) { _logger.LogError(ex, "Error retrieving health status for client {id}", id); return StatusCode(500, new { Error = "An error occurred while retrieving the client health status" }); } } /// /// Triggers a health check for all download clients /// [HttpPost("check")] public async Task CheckAllHealth() { try { var results = await _healthCheckService.CheckAllClientsHealthAsync(); return Ok(results); } catch (Exception ex) { _logger.LogError(ex, "Error checking health for all clients"); return StatusCode(500, new { Error = "An error occurred while checking client health" }); } } /// /// Triggers a health check for a specific download client /// [HttpPost("check/{id:guid}")] public async Task CheckClientHealth(Guid id) { try { var result = await _healthCheckService.CheckClientHealthAsync(id); return Ok(result); } catch (Exception ex) { _logger.LogError(ex, "Error checking health for client {id}", id); return StatusCode(500, new { Error = "An error occurred while checking client health" }); } } }