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