Files
Heimdall/src/Heimdall.Web/Controllers/HealthController.cs
T

91 lines
3.5 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System.Reflection;
namespace Heimdall.Api.Controllers;
[ApiController]
public class HealthController : ControllerBase
{
/// <summary>
/// Liveness probe — always returns OK if the process is running.
/// </summary>
[HttpGet("/health")]
[HttpGet("/healthz")]
[ProducesResponseType(typeof(void), StatusCodes.Status200OK)]
public IActionResult Health() => Ok(new { status = "healthy", timestamp = DateTime.UtcNow });
/// <summary>
/// Readiness probe — checks dependencies (PostgreSQL, Alarik S3).
/// 503 when a required dependency is down.
/// </summary>
[HttpGet("/health/ready")]
[HttpGet("/readyz")]
[ProducesResponseType(typeof(void), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(void), StatusCodes.Status503ServiceUnavailable)]
public async Task<IActionResult> 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 });
}
/// <summary>
/// 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).
/// </summary>
[HttpGet("/api/version")]
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
public IActionResult Version()
{
var assembly = typeof(HealthController).Assembly;
var informational = assembly
.GetCustomAttribute<System.Reflection.AssemblyInformationalVersionAttribute>()?
.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,
});
}
/// <summary>
/// Live probe — ready + no active AI degradation. 503 + degradationLevel
/// when CodeProject.AI / Ollama is configured but unreachable.
/// </summary>
[HttpGet("/health/live")]
[ProducesResponseType(typeof(void), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(void), StatusCodes.Status503ServiceUnavailable)]
public async Task<IActionResult> 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,
});
}
}