Skip to content

Commit 1f78272

Browse files
Refactor startup for health check urls (#2)
1 parent f820ee4 commit 1f78272

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using HealthChecks.UI.Client;
2+
using HttpHealthCheckDashboard.HealthChecks;
3+
using Microsoft.AspNetCore.Builder;
4+
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
5+
using Microsoft.AspNetCore.Routing;
6+
using Microsoft.Extensions.DependencyInjection;
7+
8+
namespace HttpHealthCheckDashboard
9+
{
10+
public static class HealthCheckExtensions
11+
{
12+
public static IHealthChecksBuilder AddHealthChecksUrls(this IServiceCollection services) =>
13+
services
14+
.AddHealthChecks()
15+
.AddCheck<MicrosoftHealthCheck>("Microsoft")
16+
.AddCheck<GoogleHealthCheck>("Google")
17+
.AddCheck<InactiveUrlHealthCheck>("InactiveUrl")
18+
.AddCheck<InvalidUrlHealthCheck>("InvalidUrl");
19+
20+
public static void MapHealthChecksUrls(this IEndpointRouteBuilder endpoints)
21+
{
22+
endpoints.MapHealthChecks("/microsoft-hc", new HealthCheckOptions()
23+
{
24+
Predicate = r => r.Name.Contains("Microsoft"),
25+
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
26+
});
27+
endpoints.MapHealthChecks("/google-hc", new HealthCheckOptions()
28+
{
29+
Predicate = r => r.Name.Contains("Google"),
30+
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
31+
});
32+
endpoints.MapHealthChecks("/inactiveurl-hc", new HealthCheckOptions()
33+
{
34+
Predicate = r => r.Name.Contains("InactiveUrl"),
35+
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
36+
});
37+
endpoints.MapHealthChecks("/invalidurl-hc", new HealthCheckOptions()
38+
{
39+
Predicate = r => r.Name.Contains("InvalidUrl"),
40+
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
41+
});
42+
}
43+
}
44+
}

HttpHealthCheckDashboard/Startup.cs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using ArnabDeveloper.HttpHealthCheck;
22
using ArnabDeveloper.HttpHealthCheck.DI;
3-
using HealthChecks.UI.Client;
43
using HttpHealthCheckDashboard.HealthChecks;
54
using Microsoft.AspNetCore.Builder;
6-
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
75
using Microsoft.AspNetCore.Hosting;
86
using Microsoft.AspNetCore.Http;
97
using Microsoft.Extensions.Configuration;
@@ -50,11 +48,7 @@ public void ConfigureServices(IServiceCollection services)
5048
}
5149
return urlDetails.AsEnumerable();
5250
})
53-
.AddHealthChecks()
54-
.AddCheck<MicrosoftHealthCheck>("Microsoft")
55-
.AddCheck<GoogleHealthCheck>("Google")
56-
.AddCheck<InactiveUrlHealthCheck>("InactiveUrl")
57-
.AddCheck<InvalidUrlHealthCheck>("InvalidUrl");
51+
.AddHealthChecksUrls();
5852

5953
services
6054
.AddHealthChecksUI()
@@ -75,26 +69,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
7569

7670
app.UseEndpoints(endpoints =>
7771
{
78-
endpoints.MapHealthChecks("/microsoft-hc", new HealthCheckOptions()
79-
{
80-
Predicate = r => r.Name.Contains("Microsoft"),
81-
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
82-
});
83-
endpoints.MapHealthChecks("/google-hc", new HealthCheckOptions()
84-
{
85-
Predicate = r => r.Name.Contains("Google"),
86-
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
87-
});
88-
endpoints.MapHealthChecks("/inactiveurl-hc", new HealthCheckOptions()
89-
{
90-
Predicate = r => r.Name.Contains("InactiveUrl"),
91-
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
92-
});
93-
endpoints.MapHealthChecks("/invalidurl-hc", new HealthCheckOptions()
94-
{
95-
Predicate = r => r.Name.Contains("InvalidUrl"),
96-
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
97-
});
72+
endpoints.MapHealthChecksUrls();
9873
endpoints.MapGet("/", async context =>
9974
{
10075
await context.Response.WriteAsync("Hello World!");

0 commit comments

Comments
 (0)