zhaolei
2020-11-20 4a2e5b9a21940f11757be37d99f0944e240e908b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System.Threading.Tasks;
#if !NETSTANDARD2_0
using Microsoft.AspNetCore.Routing;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
#elif NETSTANDARD2_0
using Newtonsoft.Json;
#endif
 
namespace Microsoft.AspNetCore.Builder
{
    /// <summary>
    /// BootstrapAdmin 健康检查扩展类
    /// </summary>
    public static class HealthChecksBuilderExtensions
    {
#if !NETSTANDARD2_0
        /// <summary>
        /// 启用健康检查
        /// </summary>
        /// <param name="endpoints"></param>
        /// <param name="path"></param>
        /// <param name="configure"></param>
        /// <returns></returns>
        public static IEndpointRouteBuilder MapBootstrapHealthChecks(this IEndpointRouteBuilder endpoints, PathString path = default, Action<HealthCheckOptions>? configure = null)
        {
            if (path == default) path = "/Healths";
            var option = new HealthCheckOptions()
            {
                ResponseWriter = (context, report) =>
                {
                    context.Response.ContentType = "application/json";
 
                    // 修复 TimeSpan
                    var serializeOption = new JsonSerializerOptions();
                    serializeOption.Converters.Add(new StringToTimeSpanConverter());
                    return context.Response.WriteAsync(JsonSerializer.Serialize(new { report.Entries.Keys, Report = report }, serializeOption));
                },
                ResultStatusCodes =
                {
                    [HealthStatus.Healthy] = StatusCodes.Status200OK,
                    [HealthStatus.Degraded] = StatusCodes.Status200OK,
                    [HealthStatus.Unhealthy] = StatusCodes.Status200OK
                }
            };
            configure?.Invoke(option);
            endpoints.MapHealthChecks(path, option);
            endpoints.MapGet("/healths-ui", context =>
            {
                context.Response.Redirect($"{context.Request.PathBase}/html/Healths-UI.html");
                return Task.CompletedTask;
            });
            return endpoints;
        }
#elif NETSTANDARD2_0
        /// <summary>
        /// 启用健康检查
        /// </summary>
        /// <param name="app"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseBootstrapHealthChecks(this IApplicationBuilder app, PathString path = default)
        {
            if (path == default) path = "/Healths";
            app.UseHealthChecks(path, new HealthCheckOptions()
            {
                ResponseWriter = (context, report) =>
                {
                    context.Response.ContentType = "application/json";
                    return context.Response.WriteAsync(JsonConvert.SerializeObject(new { report.Entries.Keys, Report = report }));
                },
                ResultStatusCodes =
                {
                    [HealthStatus.Healthy] = StatusCodes.Status200OK,
                    [HealthStatus.Degraded] = StatusCodes.Status200OK,
                    [HealthStatus.Unhealthy] = StatusCodes.Status200OK
                }
            });
            app.UseWhen(context => context.Request.Path == "/healths-ui", builder => builder.Run(request =>
            {
                request.Response.Redirect("/html/Healths-UI.html");
                return Task.CompletedTask;
            }));
            return app;
        }
#endif
    }
}