diff --git a/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs b/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs index 621e1b7a..736f56f5 100644 --- a/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs +++ b/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs @@ -1,7 +1,10 @@ -using Microsoft.Extensions.Diagnostics.HealthChecks; -using System.Collections.Generic; +using Bootstrap.DataAccess; +using Bootstrap.Security; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using System.Linq; using System.Threading; using System.Threading.Tasks; +using Task = System.Threading.Tasks.Task; namespace Bootstrap.Admin.HealthChecks { @@ -18,10 +21,12 @@ namespace Bootstrap.Admin.HealthChecks /// public Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) { - var data = new Dictionary(); - data.Add("Test1", "Test1"); - data.Add("Test2", "Test2"); - return Task.FromResult(HealthCheckResult.Healthy("Ok", data)); + using (var db = DbManager.Create()) + { + var connStr = db.ConnectionString; + var dicts = db.Fetch("Select * from Dicts"); + return dicts.Any() ? Task.FromResult(HealthCheckResult.Healthy("Ok")) : Task.FromResult(HealthCheckResult.Degraded("No init data in DB")); + } } } } diff --git a/Bootstrap.Admin/HealthChecks/HealthChecksAppBuilderExtensions.cs b/Bootstrap.Admin/HealthChecks/HealthChecksAppBuilderExtensions.cs index 82032a2d..3b07e2f5 100644 --- a/Bootstrap.Admin/HealthChecks/HealthChecksAppBuilderExtensions.cs +++ b/Bootstrap.Admin/HealthChecks/HealthChecksAppBuilderExtensions.cs @@ -1,4 +1,8 @@ -namespace Microsoft.AspNetCore.Builder +using Microsoft.AspNetCore.Diagnostics.HealthChecks; +using Microsoft.AspNetCore.Http; +using Newtonsoft.Json; + +namespace Microsoft.AspNetCore.Builder { /// /// BootstrapAdmin 健康检查扩展类 @@ -6,13 +10,22 @@ public static class HealthChecksAppBuilderExtensions { /// - /// + /// 启用健康检查 /// /// + /// /// - public static IApplicationBuilder UseBootstrapHealthChecks(this IApplicationBuilder app) + public static IApplicationBuilder UseBootstrapHealthChecks(this IApplicationBuilder app, PathString path = default) { - app.UseHealthChecks("Healths"); + if (path == default) path = "/Healths"; + app.UseHealthChecks(path, new HealthCheckOptions() + { + ResponseWriter = (context, report) => + { + context.Response.ContentType = "application/json"; + return context.Response.WriteAsync(JsonConvert.SerializeObject(report)); + } + }); return app; } } diff --git a/Bootstrap.Admin/HealthChecks/HealthChecksBuilderExtensions.cs b/Bootstrap.Admin/HealthChecks/HealthChecksBuilderExtensions.cs index 260a38cb..d5352528 100644 --- a/Bootstrap.Admin/HealthChecks/HealthChecksBuilderExtensions.cs +++ b/Bootstrap.Admin/HealthChecks/HealthChecksBuilderExtensions.cs @@ -1,4 +1,6 @@ -namespace Microsoft.Extensions.DependencyInjection +using Bootstrap.Admin.HealthChecks; + +namespace Microsoft.Extensions.DependencyInjection { /// /// 健康检查扩展类 @@ -12,7 +14,7 @@ /// public static IHealthChecksBuilder AddBootstrapAdminHealthChecks(this IHealthChecksBuilder builder) { - builder.AddCheck("db"); + builder.AddCheck("db"); return builder; } }