diff --git a/Bootstrap.Admin/Controllers/Api/GiteeController.cs b/Bootstrap.Admin/Controllers/Api/GiteeController.cs index 507a35b4..1f7c25c8 100644 --- a/Bootstrap.Admin/Controllers/Api/GiteeController.cs +++ b/Bootstrap.Admin/Controllers/Api/GiteeController.cs @@ -20,18 +20,16 @@ namespace Bootstrap.Admin.Controllers.Api /// /// 获取 Gitee 网站 Issues 信息 /// - /// + /// /// /// /// /// /// [HttpGet] - public async Task Issues([FromServices]IHttpClientFactory httpClientFactory, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange") + public async Task Issues([FromServices]GiteeHttpClient client, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange") { - var client = httpClientFactory.CreateClient(); - client.Timeout = TimeSpan.FromMilliseconds(2000); - var ret = await GetJsonAsync(() => client.GetStringAsync($"https://gitee.com/{userName}/{repoName}/issues"), content => + var ret = await GetJsonAsync(() => client.HttpClient.GetStringAsync($"https://gitee.com/{userName}/{repoName}/issues"), content => { var regex = Regex.Matches(content, "
([\\d]+)
", RegexOptions.IgnoreCase); var labels = new string[] { "open", "progressing", "closed", "rejected" }; @@ -45,18 +43,16 @@ namespace Bootstrap.Admin.Controllers.Api /// /// 获取 Gitee 网站 Pulls 信息 /// - /// + /// /// /// /// /// /// [HttpGet] - public async Task Pulls([FromServices]IHttpClientFactory httpClientFactory, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange") + public async Task Pulls([FromServices]GiteeHttpClient client, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange") { - var client = httpClientFactory.CreateClient(); - client.Timeout = TimeSpan.FromMilliseconds(2000); - var ret = await GetJsonAsync(() => client.GetStringAsync($"https://gitee.com/{userName}/{repoName}/pulls"), content => + var ret = await GetJsonAsync(() => client.HttpClient.GetStringAsync($"https://gitee.com/{userName}/{repoName}/pulls"), content => { var regex = Regex.Matches(content, "
([\\d]+)
", RegexOptions.IgnoreCase); var labels = new string[] { "open", "merged", "closed" }; @@ -69,18 +65,16 @@ namespace Bootstrap.Admin.Controllers.Api /// /// 获取 Gitee 网站 Releases 信息 /// - /// + /// /// /// /// /// /// [HttpGet] - public async Task Releases([FromServices]IHttpClientFactory httpClientFactory, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange") + public async Task Releases([FromServices]GiteeHttpClient client, [FromQuery]string userName = "LongbowEnterprise", [FromQuery]string repoName = "BootstrapAdmin", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange") { - var client = httpClientFactory.CreateClient(); - client.Timeout = TimeSpan.FromMilliseconds(2000); - var ret = await GetJsonAsync(() => client.GetStringAsync($"https://gitee.com/{userName}/{repoName}/releases"), content => + var ret = await GetJsonAsync(() => client.HttpClient.GetStringAsync($"https://gitee.com/{userName}/{repoName}/releases"), content => { var regex = Regex.Match(content, $"", RegexOptions.IgnoreCase); return string.IsNullOrEmpty(content) ? "unknown" : regex.Groups[1].Value; @@ -91,7 +85,7 @@ namespace Bootstrap.Admin.Controllers.Api /// /// 获取 Gitee 网站 Builds 信息 /// - /// + /// /// /// /// @@ -99,10 +93,9 @@ namespace Bootstrap.Admin.Controllers.Api /// /// [HttpGet] - public async Task Builds([FromServices]IHttpClientFactory httpClientFactory, [FromQuery]string userName = "ArgoZhang", [FromQuery]string projName = "bootstrapadmin", [FromQuery]string branchName = "master", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange") + public async Task Builds([FromServices]GiteeHttpClient client, [FromQuery]string userName = "ArgoZhang", [FromQuery]string projName = "bootstrapadmin", [FromQuery]string branchName = "master", [FromQuery]string label = "custom badge", [FromQuery]string color = "orange") { - var client = httpClientFactory.CreateClient(); - var ret = await GetJsonAsync(() => client.GetAsJsonAsync($"https://ci.appveyor.com/api/projects/{userName}/{projName}/branch/{branchName}", null, new CancellationTokenSource(2000).Token), content => + var ret = await GetJsonAsync(() => client.HttpClient.GetAsJsonAsync($"https://ci.appveyor.com/api/projects/{userName}/{projName}/branch/{branchName}", null, new CancellationTokenSource(2000).Token), content => { return content == null ? "unknown" : content.Build.Version; }); diff --git a/Bootstrap.Admin/HealthChecks/GiteeHttpClient.cs b/Bootstrap.Admin/HealthChecks/GiteeHttpClient.cs new file mode 100644 index 00000000..2eb2d228 --- /dev/null +++ b/Bootstrap.Admin/HealthChecks/GiteeHttpClient.cs @@ -0,0 +1,27 @@ +using System; +using System.Net.Http; + +namespace Bootstrap.Admin +{ + /// + /// Gitee HttpClient 操作类 + /// + public class GiteeHttpClient + { + /// + /// HttpClient 实例 + /// + public HttpClient HttpClient { get; private set; } + + /// + /// 构造函数 + /// + /// + public GiteeHttpClient(HttpClient client) + { + client.Timeout = TimeSpan.FromSeconds(10); + client.DefaultRequestHeaders.Connection.Add("keep-alive"); + HttpClient = client; + } + } +} diff --git a/Bootstrap.Admin/HealthChecks/GiteeHttpHealthCheck.cs b/Bootstrap.Admin/HealthChecks/GiteeHttpHealthCheck.cs index 37190086..3aa807a5 100644 --- a/Bootstrap.Admin/HealthChecks/GiteeHttpHealthCheck.cs +++ b/Bootstrap.Admin/HealthChecks/GiteeHttpHealthCheck.cs @@ -15,16 +15,16 @@ namespace Bootstrap.Admin.HealthChecks /// public class GiteeHttpHealthCheck : IHealthCheck { - private readonly HttpClient _client; + private readonly GiteeHttpClient _client; /// /// 构造函数 /// - /// + /// /// - public GiteeHttpHealthCheck(IHttpClientFactory factory, IHttpContextAccessor accessor) + public GiteeHttpHealthCheck(GiteeHttpClient client, IHttpContextAccessor accessor) { - _client = factory.CreateClient(); - _client.BaseAddress = new Uri($"{accessor.HttpContext.Request.Scheme}://{accessor.HttpContext.Request.Host}{accessor.HttpContext.Request.PathBase}"); + _client = client; + _client.HttpClient.BaseAddress = new Uri($"{accessor.HttpContext.Request.Scheme}://{accessor.HttpContext.Request.Host}{accessor.HttpContext.Request.PathBase}"); } /// @@ -42,7 +42,7 @@ namespace Bootstrap.Admin.HealthChecks { var sw = Stopwatch.StartNew(); Exception error = null; - var result = await _client.GetAsJsonAsync($"/api/Gitee/{url}", ex => error = ex, cancellationToken); + var result = await _client.HttpClient.GetAsJsonAsync($"/api/Gitee/{url}", ex => error = ex, cancellationToken); sw.Stop(); data.Add(url, error == null ? $"{result} Elapsed: {sw.Elapsed}" : $"{result} Elapsed: {sw.Elapsed} Exception: {error}"); })).ToArray()); diff --git a/Bootstrap.Admin/Startup.cs b/Bootstrap.Admin/Startup.cs index b880f414..c5a770b0 100644 --- a/Bootstrap.Admin/Startup.cs +++ b/Bootstrap.Admin/Startup.cs @@ -64,6 +64,7 @@ namespace Bootstrap.Admin services.AddSwagger(); services.AddButtonAuthorization(); services.AddDemoTask(); + services.AddHttpClient(); services.AddHealthChecks().AddBootstrapAdminHealthChecks(); services.AddMvc(options => {