2026-07-20 10:30:01 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
2026-08-12 11:29:03 +02:00
|
|
|
using System.Reflection;
|
2026-07-20 10:30:01 +00:00
|
|
|
|
|
|
|
|
namespace Heimdall.Api.Controllers;
|
|
|
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class HealthController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Liveness probe — always returns OK if the process is running.
|
|
|
|
|
/// </summary>
|
2026-08-12 09:54:37 +02:00
|
|
|
[HttpGet("/health")]
|
2026-07-20 10:30:01 +00:00
|
|
|
[HttpGet("/healthz")]
|
|
|
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status200OK)]
|
|
|
|
|
public IActionResult Health() => Ok(new { status = "healthy", timestamp = DateTime.UtcNow });
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-08-12 09:54:37 +02:00
|
|
|
/// Readiness probe — checks dependencies (PostgreSQL, Alarik S3).
|
|
|
|
|
/// 503 when a required dependency is down.
|
2026-07-20 10:30:01 +00:00
|
|
|
/// </summary>
|
2026-08-12 09:54:37 +02:00
|
|
|
[HttpGet("/health/ready")]
|
2026-07-20 10:30:01 +00:00
|
|
|
[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(
|
2026-08-12 09:54:37 +02:00
|
|
|
reg => reg.Tags.Contains("ready"), ct);
|
2026-07-20 10:30:01 +00:00
|
|
|
|
|
|
|
|
return report.Status == HealthStatus.Healthy
|
|
|
|
|
? Ok(new { status = "healthy", timestamp = DateTime.UtcNow })
|
|
|
|
|
: StatusCode(503, new { status = "unhealthy", errors = report.Entries });
|
|
|
|
|
}
|
2026-08-12 09:54:37 +02:00
|
|
|
|
2026-08-12 11:29:03 +02:00
|
|
|
/// <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,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-12 09:54:37 +02:00
|
|
|
/// <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,
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-07-20 10:30:01 +00:00
|
|
|
}
|