Skip to content

Commit

Permalink
refactor: moved healthchecks to extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaxelr committed Sep 23, 2024
1 parent 23cc0ba commit 6d8fabb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
29 changes: 29 additions & 0 deletions Content/src/Extensions/WebApplication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;

namespace CarterService.Extensions
{
public static class WebApplicationExtensions
{
internal static WebApplication MapSwagger(this WebApplication app)
{
app.UseSwagger();
app.UseSwaggerUI();

return app;
}

internal static WebApplication UseHealthChecks(this WebApplication app)
{
app.UseHealthChecks("/healthcheck", new HealthCheckOptions()
{
AllowCachingResponses = false,
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});

return app;
}
}
}
14 changes: 14 additions & 0 deletions Content/src/Extensions/WebApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using CarterService.Entities;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.OpenApi.Models;

namespace CarterService.Extensions;
Expand Down Expand Up @@ -37,4 +38,17 @@ internal static WebApplicationBuilder AddSwagger(this WebApplicationBuilder buil

return builder;
}

internal static WebApplicationBuilder AddHealthChecks(this WebApplicationBuilder builder, AppSettings settings)
{
builder.Services.AddHealthChecks()
.AddCheck
(
settings.HealthDefinition.Name,
() => HealthCheckResult.Healthy(settings.HealthDefinition.HealthyMessage),
tags: settings.HealthDefinition.Tags
);

return builder;
}
}
22 changes: 3 additions & 19 deletions Content/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
using CarterService.Entities;
using CarterService.Extensions;
using CarterService.Repository;
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using Serilog;

Expand Down Expand Up @@ -46,14 +43,7 @@
builder.Services.AddSingleton(settings); //typeof(AppSettings)
builder.Services.AddSingleton<IHelloRepository, HelloRepository>();

//HealthChecks
builder.Services.AddHealthChecks()
.AddCheck
(
settings.HealthDefinition.Name,
() => HealthCheckResult.Healthy(settings.HealthDefinition.HealthyMessage),
tags: settings.HealthDefinition.Tags
);
builder.AddHealthChecks(settings);

var app = builder.Build();

Expand All @@ -71,15 +61,9 @@
app.UseHttpsRedirection();
app.UseRouting();

app.UseHealthChecks("/healthcheck", new HealthCheckOptions()
{
AllowCachingResponses = false,
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
app.UseHealthChecks();

app.UseSwagger();
app.UseSwaggerUI();
app.MapSwagger();

app.UseCarterCaching();
app.MapCarter();
Expand Down

0 comments on commit 6d8fabb

Please sign in to comment.