feat(api): add output cache

This commit is contained in:
Collin Barrett 2025-06-08 12:11:42 -05:00 committed by Collin Barrett
parent 3076eac77a
commit 8c22f90bab
2 changed files with 27 additions and 13 deletions

View file

@ -6,6 +6,8 @@
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(serverOptions => serverOptions.AddServerHeader = false);
builder.Services.AddRequestTimeouts();
builder.Services.AddOutputCache();
builder.AddServiceDefaults();
builder.Services.AddCors(options => CorsConfiguration.SetupAction(options, builder.Configuration));
builder.Services.AddProblemDetails();
@ -17,6 +19,8 @@
app.UseExceptionHandler();
app.UseCors();
app.UseRequestTimeouts();
app.UseOutputCache();
app.MapEndpoints();
app.MapDefaultEndpoints();
app.UseSwagger(o => o.RouteTemplate = "{documentName}/openapi.json");

View file

@ -1,6 +1,5 @@
using Azure.Monitor.OpenTelemetry.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
@ -100,6 +99,15 @@ private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builde
public static TBuilder AddDefaultHealthChecks<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
builder.Services.AddRequestTimeouts(
configure: static timeouts =>
timeouts.AddPolicy("HealthChecks", TimeSpan.FromSeconds(5)));
builder.Services.AddOutputCache(
configureOptions: static caching =>
caching.AddPolicy("HealthChecks",
build: static policy => policy.Expire(TimeSpan.FromSeconds(10))));
builder.Services.AddHealthChecks()
// Add a default liveness check to ensure app is responsive
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);
@ -109,19 +117,21 @@ public static TBuilder AddDefaultHealthChecks<TBuilder>(this TBuilder builder) w
public static WebApplication MapDefaultEndpoints(this WebApplication app)
{
// Adding health checks endpoints to applications in non-development environments has security implications.
// See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments.
// if (app.Environment.IsDevelopment())
// {
// All health checks must pass for app to be considered ready to accept traffic after starting
app.MapHealthChecks(HealthEndpointPath);
// https://aka.ms/dotnet/aspire/healthchecks
var healthChecks = app.MapGroup("");
// Only health checks tagged with the "live" tag must pass for app to be considered alive
app.MapHealthChecks(AlivenessEndpointPath, new HealthCheckOptions
{
Predicate = r => r.Tags.Contains("live")
});
// }
healthChecks
.CacheOutput("HealthChecks")
.WithRequestTimeout("HealthChecks");
// All health checks must pass for app to be considered ready to accept traffic after starting
healthChecks.MapHealthChecks(HealthEndpointPath);
// Only health checks tagged with the "live" tag must pass for app to be considered alive
healthChecks.MapHealthChecks(AlivenessEndpointPath, new()
{
Predicate = static r => r.Tags.Contains("live")
});
return app;
}