diff --git a/src/Monolith/ClassifiedAds.AspireAppHost/Program.cs b/src/Monolith/ClassifiedAds.AspireAppHost/Program.cs index 1e8423f93..935e67c0d 100644 --- a/src/Monolith/ClassifiedAds.AspireAppHost/Program.cs +++ b/src/Monolith/ClassifiedAds.AspireAppHost/Program.cs @@ -4,7 +4,7 @@ var background = builder.AddProject("ClassifiedAds-Background"); var graphQL = builder.AddProject("ClassifiedAds-GraphQL"); var webApi = builder.AddProject("ClassifiedAds-WebAPI"); -var webMvc = builder.AddProject("ClassifiedAds-WebMVC"); +var webMvc = builder.AddProject("ClassifiedAds-WebMVC").WithHttpsHealthCheck("/healthz"); var blazorServerSide = builder.AddProject("ClassifiedAds-BlazorServerSide"); var blazorWebAssembly = builder.AddProject("ClassifiedAds-BlazorWebAssembly"); diff --git a/src/Monolith/ClassifiedAds.Background/Program.cs b/src/Monolith/ClassifiedAds.Background/Program.cs index ac7109624..25df75fda 100644 --- a/src/Monolith/ClassifiedAds.Background/Program.cs +++ b/src/Monolith/ClassifiedAds.Background/Program.cs @@ -79,6 +79,9 @@ failureStatus: HealthStatus.Degraded) .AddMessageBusHealthCheck(appSettings.MessageBroker); + services.Configure(x => x.Interval = TimeSpan.FromMinutes(10)); + services.AddHostedService(); + services.AddHostedService>(); services.AddHostedService>(); services.AddHostedService(); @@ -86,10 +89,6 @@ services.AddHostedService(); services.AddHostedService(); services.AddHostedService(); - - services.Configure(x => x.Interval = TimeSpan.FromMinutes(10)); - services.AddHostedService(); - }) .Build() .Run(); diff --git a/src/Monolith/ClassifiedAds.Infrastructure/HealthChecks/HealthChecksResponseWriter.cs b/src/Monolith/ClassifiedAds.Infrastructure/HealthChecks/HealthChecksResponseWriter.cs new file mode 100644 index 000000000..fa0a175a7 --- /dev/null +++ b/src/Monolith/ClassifiedAds.Infrastructure/HealthChecks/HealthChecksResponseWriter.cs @@ -0,0 +1,51 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using System.IO; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; + +namespace ClassifiedAds.Infrastructure.HealthChecks; + +public static class HealthChecksResponseWriter +{ + public static Task WriteReponse(HttpContext context, HealthReport healthReport) + { + context.Response.ContentType = "application/json; charset=utf-8"; + + var options = new JsonWriterOptions { Indented = true }; + + using var memoryStream = new MemoryStream(); + using (var jsonWriter = new Utf8JsonWriter(memoryStream, options)) + { + jsonWriter.WriteStartObject(); + jsonWriter.WriteString("status", healthReport.Status.ToString()); + jsonWriter.WriteStartObject("results"); + + foreach (var healthReportEntry in healthReport.Entries) + { + jsonWriter.WriteStartObject(healthReportEntry.Key); + jsonWriter.WriteString("status", healthReportEntry.Value.Status.ToString()); + jsonWriter.WriteString("description", healthReportEntry.Value.Description ?? healthReportEntry.Value.Exception?.Message.ToString()); + jsonWriter.WriteStartObject("data"); + + foreach (var item in healthReportEntry.Value.Data) + { + jsonWriter.WritePropertyName(item.Key); + + JsonSerializer.Serialize(jsonWriter, item.Value, item.Value?.GetType() ?? typeof(object)); + } + + jsonWriter.WriteEndObject(); + + jsonWriter.WriteEndObject(); + } + + jsonWriter.WriteEndObject(); + + jsonWriter.WriteEndObject(); + } + + return context.Response.WriteAsync(Encoding.UTF8.GetString(memoryStream.ToArray())); + } +} diff --git a/src/Monolith/ClassifiedAds.WebMVC/ClassifiedAds.WebMVC.csproj b/src/Monolith/ClassifiedAds.WebMVC/ClassifiedAds.WebMVC.csproj index e7b906349..368522a59 100644 --- a/src/Monolith/ClassifiedAds.WebMVC/ClassifiedAds.WebMVC.csproj +++ b/src/Monolith/ClassifiedAds.WebMVC/ClassifiedAds.WebMVC.csproj @@ -8,9 +8,6 @@ - - - diff --git a/src/Monolith/ClassifiedAds.WebMVC/Program.cs b/src/Monolith/ClassifiedAds.WebMVC/Program.cs index b5abfca4f..430a64a81 100644 --- a/src/Monolith/ClassifiedAds.WebMVC/Program.cs +++ b/src/Monolith/ClassifiedAds.WebMVC/Program.cs @@ -2,6 +2,7 @@ using ClassifiedAds.Domain.Identity; using ClassifiedAds.Infrastructure.Configuration; using ClassifiedAds.Infrastructure.HealthChecks; +using ClassifiedAds.Infrastructure.HostedServices; using ClassifiedAds.Infrastructure.Identity; using ClassifiedAds.Infrastructure.Logging; using ClassifiedAds.Infrastructure.Monitoring; @@ -12,7 +13,6 @@ using ClassifiedAds.WebMVC.Filters; using ClassifiedAds.WebMVC.HttpMessageHandlers; using ClassifiedAds.WebMVC.Middleware; -using HealthChecks.UI.Client; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.OpenIdConnect; @@ -151,11 +151,8 @@ failureStatus: HealthStatus.Degraded) .AddStorageManagerHealthCheck(appSettings.Storage); -services.AddHealthChecksUI(setupSettings: setup => -{ - setup.AddHealthCheckEndpoint("Basic Health Check", $"{appSettings.CurrentUrl}/healthz"); - setup.DisableDatabaseMigrations(); -}).AddInMemoryStorage(); +services.Configure(x => x.Interval = TimeSpan.FromMinutes(10)); +services.AddHostedService(); services.AddSingleton(); services.AddScoped(); @@ -204,7 +201,7 @@ app.UseHealthChecks("/healthz", new HealthCheckOptions { Predicate = _ => true, - ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse, + ResponseWriter = HealthChecksResponseWriter.WriteReponse, ResultStatusCodes = { [HealthStatus.Healthy] = StatusCodes.Status200OK, @@ -213,8 +210,6 @@ }, }); -app.UseHealthChecksUI(); // /healthchecks-ui#/healthchecks - app.MapDefaultControllerRoute(); app.MapClassifiedAdsHubs(); diff --git a/src/Monolith/ClassifiedAds.WebMVC/Views/Shared/_Layout.cshtml b/src/Monolith/ClassifiedAds.WebMVC/Views/Shared/_Layout.cshtml index 76561246e..8b15ac210 100644 --- a/src/Monolith/ClassifiedAds.WebMVC/Views/Shared/_Layout.cshtml +++ b/src/Monolith/ClassifiedAds.WebMVC/Views/Shared/_Layout.cshtml @@ -61,7 +61,7 @@ }