using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Diagnostics.HealthChecks; using System; using System.Collections.Generic; using System.Diagnostics; using System.Net.Http; using System.Threading; using System.Threading.Tasks; namespace Bootstrap.Admin.HealthChecks { /// /// Gitee 接口检查器 /// public class GiteeHttpHealthCheck : IHealthCheck { private readonly HttpClient _client; /// /// 构造函数 /// /// /// public GiteeHttpHealthCheck(IHttpClientFactory factory, IHttpContextAccessor accessor) { _client = factory.CreateClient(); _client.BaseAddress = new Uri($"{accessor.HttpContext.Request.Scheme}://{accessor.HttpContext.Request.Host}/{accessor.HttpContext.Request.PathBase}"); } /// /// 异步检查方法 /// /// /// /// public async Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) { var urls = new string[] { "Issues", "Pulls", "Releases", "Builds" }; var data = new Dictionary(); var sw = Stopwatch.StartNew(); foreach (var url in urls) { sw.Restart(); await _client.GetStringAsync($"api/Gitee/{url}").ConfigureAwait(false); sw.Stop(); data.Add(url, sw.Elapsed); } return HealthCheckResult.Healthy("Ok", data); } } }