34 lines
1.2 KiB
C#
34 lines
1.2 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|||
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||
|
|
|
||
|
|
namespace Heimdall.Api.Controllers;
|
||
|
|
|
||
|
|
[ApiController]
|
||
|
|
public class HealthController : ControllerBase
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// Liveness probe — always returns OK if the process is running.
|
||
|
|
/// </summary>
|
||
|
|
[HttpGet("/healthz")]
|
||
|
|
[ProducesResponseType(typeof(void), StatusCodes.Status200OK)]
|
||
|
|
public IActionResult Health() => Ok(new { status = "healthy", timestamp = DateTime.UtcNow });
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Readiness probe — checks if the database is reachable.
|
||
|
|
/// Uses ASP.NET Core health checks infrastructure.
|
||
|
|
/// </summary>
|
||
|
|
[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(
|
||
|
|
_ => true, ct);
|
||
|
|
|
||
|
|
return report.Status == HealthStatus.Healthy
|
||
|
|
? Ok(new { status = "healthy", timestamp = DateTime.UtcNow })
|
||
|
|
: StatusCode(503, new { status = "unhealthy", errors = report.Entries });
|
||
|
|
}
|
||
|
|
}
|