using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Diagnostics.HealthChecks; using System.Reflection; namespace Heimdall.Api.Controllers; [ApiController] public class HealthController : ControllerBase { /// /// Liveness probe — always returns OK if the process is running. /// [HttpGet("/health")] [HttpGet("/healthz")] [ProducesResponseType(typeof(void), StatusCodes.Status200OK)] public IActionResult Health() => Ok(new { status = "healthy", timestamp = DateTime.UtcNow }); /// /// Readiness probe — checks dependencies (PostgreSQL and active object storage provider). /// 503 when a required dependency is down. /// [HttpGet("/health/ready")] [HttpGet("/readyz")] [ProducesResponseType(typeof(void), StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status503ServiceUnavailable)] public async Task Readiness( [FromServices] HealthCheckService healthCheckService, CancellationToken ct) { var report = await healthCheckService.CheckHealthAsync( reg => reg.Tags.Contains("ready"), ct); return report.Status == HealthStatus.Healthy ? Ok(new { status = "healthy", timestamp = DateTime.UtcNow }) : StatusCode(503, new { status = "unhealthy", errors = report.Entries }); } /// /// Version — build version injected by GitVersion (informational version). /// Public (no auth) on purpose: used by agents and external tooling for /// version checks / auto-update (tickets #95 #96 #97). /// [HttpGet("/api/version")] [ProducesResponseType(typeof(object), StatusCodes.Status200OK)] public IActionResult Version() { var assembly = typeof(HealthController).Assembly; var informational = assembly .GetCustomAttribute()? .InformationalVersion; var version = assembly.GetName().Version?.ToString() ?? "0.0.0"; return Ok(new { version = informational?.Split('+')[0] ?? version, informational, environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production", timestamp = DateTime.UtcNow, }); } /// /// Live probe — ready + no active AI degradation. 503 + degradationLevel /// when CodeProject.AI / Ollama is configured but unreachable. /// [HttpGet("/health/live")] [ProducesResponseType(typeof(void), StatusCodes.Status200OK)] [ProducesResponseType(typeof(void), StatusCodes.Status503ServiceUnavailable)] public async Task Live( [FromServices] HealthCheckService healthCheckService, CancellationToken ct) { var report = await healthCheckService.CheckHealthAsync( reg => reg.Tags.Contains("live"), ct); if (report.Status == HealthStatus.Healthy) return Ok(new { status = "healthy", degradationLevel = 0, timestamp = DateTime.UtcNow }); // Dégradation IA : ready OK mais backends IA (live) down → 503. var degradedEntries = report.Entries .Where(e => e.Value.Status != HealthStatus.Healthy) .ToDictionary(e => e.Key, e => e.Value.Description ?? e.Value.Status.ToString()); return StatusCode(503, new { status = "degraded", degradationLevel = 1, errors = degradedEntries, timestamp = DateTime.UtcNow, }); } }