| | | 1 | | using System.Reflection; |
| | | 2 | | using Chronicis.Api.Services; |
| | | 3 | | using Chronicis.Shared.DTOs; |
| | | 4 | | using Microsoft.AspNetCore.Mvc; |
| | | 5 | | using Microsoft.Data.SqlClient; |
| | | 6 | | |
| | | 7 | | namespace Chronicis.Api.Controllers; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// API endpoints for health checks. |
| | | 11 | | /// These endpoints do NOT require authentication. |
| | | 12 | | /// </summary> |
| | | 13 | | [ApiController] |
| | | 14 | | [Route("health")] |
| | | 15 | | public class HealthController : ControllerBase |
| | | 16 | | { |
| | | 17 | | private readonly ILogger<HealthController> _logger; |
| | | 18 | | private readonly IConfiguration _configuration; |
| | | 19 | | private readonly ISystemHealthService _systemHealthService; |
| | | 20 | | private readonly IHealthReadinessService _healthReadinessService; |
| | | 21 | | |
| | 1 | 22 | | public HealthController( |
| | 1 | 23 | | ILogger<HealthController> logger, |
| | 1 | 24 | | IConfiguration configuration, |
| | 1 | 25 | | ISystemHealthService systemHealthService, |
| | 1 | 26 | | IHealthReadinessService healthReadinessService) |
| | | 27 | | { |
| | 1 | 28 | | _logger = logger; |
| | 1 | 29 | | _configuration = configuration; |
| | 1 | 30 | | _systemHealthService = systemHealthService; |
| | 1 | 31 | | _healthReadinessService = healthReadinessService; |
| | 1 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// GET /api/health - Basic health check endpoint. |
| | | 36 | | /// Returns 200 OK if the API is running. |
| | | 37 | | /// </summary> |
| | | 38 | | [HttpGet] |
| | | 39 | | public IActionResult GetHealth() |
| | | 40 | | { |
| | 1 | 41 | | _logger.LogTraceSanitized("Health Endpoint Called"); |
| | 1 | 42 | | return Ok(new |
| | 1 | 43 | | { |
| | 1 | 44 | | status = "healthy", |
| | 1 | 45 | | timestamp = DateTime.UtcNow, |
| | 1 | 46 | | version = GetApiVersion() |
| | 1 | 47 | | }); |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// GET /api/health/ready - Readiness check including database connectivity. |
| | | 52 | | /// Returns 200 OK if the API and database are ready. |
| | | 53 | | /// </summary> |
| | | 54 | | [HttpGet("ready")] |
| | | 55 | | public async Task<IActionResult> GetReadiness() |
| | | 56 | | { |
| | | 57 | | try |
| | | 58 | | { |
| | | 59 | | var readiness = await _healthReadinessService.GetReadinessAsync(); |
| | | 60 | | if (!readiness.IsHealthy) |
| | | 61 | | { |
| | | 62 | | _logger.LogWarningSanitized("Health check failed: Cannot connect to database"); |
| | | 63 | | return StatusCode(503, new |
| | | 64 | | { |
| | | 65 | | status = "unhealthy", |
| | | 66 | | timestamp = DateTime.UtcNow, |
| | | 67 | | checks = new |
| | | 68 | | { |
| | | 69 | | database = readiness.DatabaseStatus |
| | | 70 | | } |
| | | 71 | | }); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | // Get connection string info for diagnostics (mask password) |
| | | 75 | | var connStr = _configuration.GetConnectionString("ChronicisDb") ?? ""; |
| | | 76 | | var maskedConnStr = MaskConnectionString(connStr); |
| | | 77 | | |
| | | 78 | | |
| | | 79 | | _logger.LogTraceSanitized("Readiness endpoint succeeded"); |
| | | 80 | | |
| | | 81 | | return Ok(new |
| | | 82 | | { |
| | | 83 | | status = "healthy", |
| | | 84 | | timestamp = DateTime.UtcNow, |
| | | 85 | | version = GetApiVersion(), |
| | | 86 | | checks = new |
| | | 87 | | { |
| | | 88 | | database = readiness.DatabaseStatus, |
| | | 89 | | connectionInfo = maskedConnStr |
| | | 90 | | } |
| | | 91 | | }); |
| | | 92 | | } |
| | | 93 | | catch (Exception ex) |
| | | 94 | | { |
| | | 95 | | _logger.LogErrorSanitized(ex, "Health check failed with exception"); |
| | | 96 | | return StatusCode(503, new |
| | | 97 | | { |
| | | 98 | | status = "unhealthy", |
| | | 99 | | timestamp = DateTime.UtcNow, |
| | | 100 | | error = ex.Message |
| | | 101 | | }); |
| | | 102 | | } |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// GET /api/health/status - Comprehensive system health status. |
| | | 107 | | /// Returns the health status of all system dependencies. |
| | | 108 | | /// </summary> |
| | | 109 | | [HttpGet("status")] |
| | | 110 | | public async Task<ActionResult<SystemHealthStatusDto>> GetSystemStatus() |
| | | 111 | | { |
| | | 112 | | _logger.LogTraceSanitized("System health status endpoint called"); |
| | | 113 | | |
| | | 114 | | var systemHealth = await _systemHealthService.GetSystemHealthAsync(); |
| | | 115 | | systemHealth.ApiVersion = GetApiVersion(); |
| | | 116 | | |
| | | 117 | | // Return appropriate HTTP status code based on overall health |
| | | 118 | | var statusCode = systemHealth.OverallStatus switch |
| | | 119 | | { |
| | | 120 | | HealthStatus.Healthy => 200, |
| | | 121 | | HealthStatus.Degraded => 200, // Still operational |
| | | 122 | | HealthStatus.Unhealthy => 503, |
| | | 123 | | _ => 200 |
| | | 124 | | }; |
| | | 125 | | |
| | | 126 | | return StatusCode(statusCode, systemHealth); |
| | | 127 | | } |
| | | 128 | | |
| | 2 | 129 | | internal static string GetApiVersion() => GetApiVersion(Assembly.GetExecutingAssembly()); |
| | | 130 | | |
| | | 131 | | internal static string GetApiVersion(Assembly assembly) => |
| | 3 | 132 | | assembly |
| | 3 | 133 | | .GetCustomAttribute<AssemblyInformationalVersionAttribute>() |
| | 3 | 134 | | ?.InformationalVersion ?? "0.0.0"; |
| | | 135 | | |
| | | 136 | | private static string MaskConnectionString(string connectionString) |
| | | 137 | | { |
| | 5 | 138 | | if (string.IsNullOrEmpty(connectionString)) |
| | 2 | 139 | | return "(empty)"; |
| | | 140 | | |
| | | 141 | | try |
| | | 142 | | { |
| | 3 | 143 | | var builder = new SqlConnectionStringBuilder(connectionString); |
| | 2 | 144 | | var hasPassword = !string.IsNullOrEmpty(builder.Password); |
| | 2 | 145 | | var hasUserId = !string.IsNullOrEmpty(builder.UserID); |
| | | 146 | | |
| | 2 | 147 | | return $"Server={builder.DataSource}; Database={builder.InitialCatalog}; " + |
| | 2 | 148 | | $"User={(!hasUserId ? "(none)" : "****")}; Password={(!hasPassword ? "(none)" : "****")}; " + |
| | 2 | 149 | | $"MARS={builder.MultipleActiveResultSets}; Encrypt={builder.Encrypt}"; |
| | | 150 | | } |
| | 1 | 151 | | catch |
| | | 152 | | { |
| | 1 | 153 | | return "(invalid connection string format)"; |
| | | 154 | | } |
| | 3 | 155 | | } |
| | | 156 | | } |