refactor: 健康检查使用本地接口

This commit is contained in:
Argo Window10 2019-11-15 18:51:33 +08:00
parent 75bfb7881a
commit 161442c841
8 changed files with 61 additions and 5 deletions

View File

@ -3,7 +3,10 @@ using Bootstrap.Security;
using Bootstrap.Security.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
namespace Bootstrap.Admin.Controllers
{
@ -66,5 +69,25 @@ namespace Bootstrap.Admin.Controllers
if (string.IsNullOrEmpty(args.AppId) || string.IsNullOrEmpty(args.UserName)) return new BootstrapMenu[0];
return MenuHelper.RetrieveAppMenus(args.AppId, args.UserName, args.Url);
}
/// <summary>
/// 发送健康检查结果
/// </summary>
/// <param name="httpClient"></param>
/// <param name="config"></param>
/// <param name="message"></param>
/// <returns></returns>
[HttpPost]
public async Task<bool> Healths([FromServices]GiteeHttpClient httpClient, [FromServices]IConfiguration config, [FromBody]string message)
{
var ret = false;
var url = config.GetValue("HealthsCloudUrl", "");
if (!string.IsNullOrEmpty(url))
{
try { await httpClient.HttpClient.PostAsJsonAsync(url, message); ret = true; }
catch { }
}
return ret;
}
}
}

View File

@ -54,7 +54,7 @@ namespace Microsoft.Extensions.DependencyInjection
{
if (!string.IsNullOrEmpty(option.Url))
{
try { await httpClient.PostAsJsonAsync(option.Url, message); }
try { await httpClient.PostAsJsonAsync(option.Url, message).ConfigureAwait(false); }
catch { }
}
});

View File

@ -65,6 +65,7 @@
],
"SwaggerPathBase": "",
"AllowOrigins": "http://localhost:49185",
"HealthsCloudUrl": "https://client.sdgxgz.com/api/Interface/Healths",
"FileStorageOptions": {
"Enabled": true,
"Folder": "TaskStorage",

View File

@ -62,6 +62,7 @@
"AppId": "BA",
"SwaggerPathBase": "",
"AllowOrigins": "http://localhost,http://ba.sdgxgz.cn",
"HealthsCloudUrl": "https://client.sdgxgz.com/api/Interface/Healths",
"GiteeHealthChecks": false,
"Sentry": {
"Dsn": "https://70bdfff562e84fa7b9a43d65924ab9ad@sentry.io/1469396"

View File

@ -1,7 +1,7 @@
(function ($) {
$.extend({
sendHealths: function (data) {
$.bc({ url: 'http://client.sdgxgz.com/api/Interface/Log', data: JSON.stringify(data), method: 'post' });
$.bc({ url: 'api/Interface/Healths', data: JSON.stringify(data), method: 'post' });
}
});
$.fn.extend({

View File

@ -21,12 +21,35 @@ namespace Bootstrap.Client.Controllers.Api
/// <param name="config"></param>
/// <param name="message"></param>
/// <returns></returns>
[HttpPost]
public async Task<bool> Log([FromServices]IConfiguration config, [FromBody]string message)
{
return await SendMailAsync(config, message);
return await SendMailAsync(config, "BootstrapAdmin Exception", message);
}
private async Task<bool> SendMailAsync(IConfiguration config, string message)
/// <summary>
/// 邮件发送健康检查方法
/// </summary>
/// <param name="config"></param>
/// <param name="message"></param>
/// <returns></returns>
[HttpPost]
public async Task<bool> Healths([FromServices]IConfiguration config, [FromBody]string message)
{
return await SendMailAsync(config, "Healths Report", message);
}
/// <summary>
/// 跨域握手协议
/// </summary>
/// <returns></returns>
[HttpOptions]
public string Options()
{
return string.Empty;
}
private async Task<bool> SendMailAsync(IConfiguration config, string title, string message)
{
var section = config.GetSection("SmtpClient");
var smtpHost = section.GetValue("Host", "smtp.163.com");
@ -40,7 +63,7 @@ namespace Bootstrap.Client.Controllers.Api
{
Credentials = new NetworkCredential(from, password)
};
await mailSender.SendMailAsync(from, to, "BootstrapAdmin Exception", message);
await mailSender.SendMailAsync(from, to, title, message);
}
return true;
}

View File

@ -43,5 +43,12 @@ namespace Bootstrap.Admin.Api.SqlServer
var ret = await Client.PostAsJsonAsync<AppMenuOption, IEnumerable<BootstrapMenu>>("RetrieveAppMenus", new AppMenuOption() { AppId = "Demo", UserName = "Admin", Url = "~/Admin/Index" });
Assert.NotEmpty(ret);
}
[Fact]
public async void Healths_Ok()
{
var ret = await Client.PostAsJsonAsync<string, bool>("Healths", "UnitTest");
Assert.False(ret);
}
}
}

View File

@ -51,6 +51,7 @@
],
"AppId": "BA",
"GiteeHealthChecks": true,
"HealthsCloudUrl": "api/Interface/Healths",
"GiteeOptions": {
"Enabled": false
},