Skip to content

Commit

Permalink
migrate route mapping to extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaed Parkar committed Sep 18, 2023
1 parent e87cd45 commit 017c209
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 37 deletions.
45 changes: 45 additions & 0 deletions AdminWebsite/AdminWebsite/Health/HealthCheckExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using AdminWebsite.Configuration;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;

namespace AdminWebsite.Health;

Expand Down Expand Up @@ -37,4 +44,42 @@ public static IServiceCollection AddVhHealthChecks(this IServiceCollection servi
tags: new[] {"startup", "readiness"});
return services;
}

public static IEndpointRouteBuilder AddVhHealthCheckRouteMaps(this IEndpointRouteBuilder endpoints)
{
endpoints.MapHealthChecks("/healthcheck/liveness", new HealthCheckOptions()
{
Predicate = check => check.Tags.Contains("self"),
ResponseWriter = HealthCheckResponseWriter
});

endpoints.MapHealthChecks("/healthcheck/startup", new HealthCheckOptions()
{
Predicate = check => check.Tags.Contains("startup"),
ResponseWriter = HealthCheckResponseWriter
});

endpoints.MapHealthChecks("/healthcheck/readiness", new HealthCheckOptions()
{
Predicate = check => check.Tags.Contains("readiness"),
ResponseWriter = HealthCheckResponseWriter
});

return endpoints;
}

private static async Task HealthCheckResponseWriter(HttpContext context, HealthReport report)
{
var result = JsonConvert.SerializeObject(new
{
status = report.Status.ToString(),
details = report.Entries.Select(e => new
{
key = e.Key, value = Enum.GetName(typeof(HealthStatus), e.Value.Status),
error = e.Value.Exception?.Message
})
});
context.Response.ContentType = "application/json";
await context.Response.WriteAsync(result);
}
}
39 changes: 2 additions & 37 deletions AdminWebsite/AdminWebsite/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using AdminWebsite.Configuration;
using AdminWebsite.Contracts.Responses;
using AdminWebsite.Extensions;
Expand All @@ -10,16 +8,12 @@
using FluentValidation.AspNetCore;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Logging;
using Newtonsoft.Json;

namespace AdminWebsite
{
Expand Down Expand Up @@ -140,23 +134,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
endpoints.MapDefaultControllerRoute();

endpoints.MapHealthChecks("/healthcheck/liveness", new HealthCheckOptions()
{
Predicate = check => check.Tags.Contains("self"),
ResponseWriter = HealthCheckResponseWriter
});

endpoints.MapHealthChecks("/healthcheck/startup", new HealthCheckOptions()
{
Predicate = check => check.Tags.Contains("startup"),
ResponseWriter = HealthCheckResponseWriter
});

endpoints.MapHealthChecks("/healthcheck/readiness", new HealthCheckOptions()
{
Predicate = check => check.Tags.Contains("readiness"),
ResponseWriter = HealthCheckResponseWriter
});
endpoints.AddVhHealthCheckRouteMaps();
});

app.UseSpa(spa =>
Expand All @@ -174,19 +152,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
});
}

private async Task HealthCheckResponseWriter(HttpContext context, HealthReport report)
{
var result = JsonConvert.SerializeObject(new
{
status = report.Status.ToString(),
details = report.Entries.Select(e => new
{
key = e.Key, value = Enum.GetName(typeof(HealthStatus), e.Value.Status),
error = e.Value.Exception?.Message
})
});
context.Response.ContentType = "application/json";
await context.Response.WriteAsync(result);
}

}
}

0 comments on commit 017c209

Please sign in to comment.