| | | 1 | | using Microsoft.AspNetCore.Diagnostics.HealthChecks; |
| | | 2 | | using ProjectTemplate.Web.HealthChecks; |
| | | 3 | | |
| | | 4 | | namespace ProjectTemplate.Web.Extensions; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides extension methods for registering and mapping application health check endpoints. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class HealthCheckExtensions |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Registers baseline ASP.NET Core health check services. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="services">The service collection used to register health checks.</param> |
| | | 15 | | /// <returns>The original <see cref="IServiceCollection"/> for chaining.</returns> |
| | | 16 | | public static IServiceCollection AddApplicationHealthChecks(this IServiceCollection services) |
| | | 17 | | { |
| | 114 | 18 | | services.AddHealthChecks(); |
| | | 19 | | |
| | 114 | 20 | | return services; |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Maps baseline health check endpoints for infrastructure, reverse proxies, and hosting platforms. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <remarks> |
| | | 27 | | /// <c>/health/ready</c> runs only checks tagged <see cref="ApplicationHealthCheckTags.Ready"/>, and |
| | | 28 | | /// <c>/health/audit-integrity</c> runs only checks tagged <see cref="ApplicationHealthCheckTags.Audit"/>. Audit int |
| | | 29 | | /// reported separately so an integrity finding alerts operators without removing every replica from rotation. |
| | | 30 | | /// </remarks> |
| | | 31 | | /// <param name="app">The web application used to map health check endpoints.</param> |
| | | 32 | | /// <returns>The original <see cref="WebApplication"/> for chaining.</returns> |
| | | 33 | | public static WebApplication MapApplicationHealthChecks(this WebApplication app) |
| | | 34 | | { |
| | 98 | 35 | | app.MapHealthChecks("/health") |
| | 98 | 36 | | .AllowAnonymous(); |
| | | 37 | | |
| | 98 | 38 | | app.MapHealthChecks("/health/ready", new HealthCheckOptions |
| | 98 | 39 | | { |
| | 98 | 40 | | Predicate = healthCheck => healthCheck.Tags.Contains(ApplicationHealthCheckTags.Ready) |
| | 98 | 41 | | }) |
| | 98 | 42 | | .AllowAnonymous(); |
| | | 43 | | |
| | 98 | 44 | | app.MapHealthChecks("/health/audit-integrity", new HealthCheckOptions |
| | 98 | 45 | | { |
| | 98 | 46 | | Predicate = healthCheck => healthCheck.Tags.Contains(ApplicationHealthCheckTags.Audit) |
| | 98 | 47 | | }) |
| | 98 | 48 | | .AllowAnonymous(); |
| | | 49 | | |
| | 98 | 50 | | app.MapHealthChecks("/health/live", new HealthCheckOptions |
| | 98 | 51 | | { |
| | 98 | 52 | | Predicate = _ => false |
| | 98 | 53 | | }) |
| | 98 | 54 | | .AllowAnonymous(); |
| | | 55 | | |
| | 98 | 56 | | return app; |
| | | 57 | | } |
| | | 58 | | } |