🍒 refactor(HttpRemote): 迁移远程请求配置到业务层
This commit is contained in:
parent
d57291dc30
commit
c8d55084d7
20
Admin.NET/Admin.NET.Application/Option/HttpRemotesOptions.cs
Normal file
20
Admin.NET/Admin.NET.Application/Option/HttpRemotesOptions.cs
Normal file
@ -0,0 +1,20 @@
|
||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||
//
|
||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
using Furion.ConfigurableOptions;
|
||||
|
||||
namespace Admin.NET.Application;
|
||||
|
||||
/// <summary>
|
||||
/// 远程请求配置
|
||||
/// </summary>
|
||||
public sealed class HttpRemotesOptions : IConfigurableOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// 企业微信
|
||||
/// </summary>
|
||||
public HttpRemoteItem WorkWeixin { get; set; }
|
||||
}
|
||||
@ -26,6 +26,9 @@ public class Startup : AppStartup
|
||||
{
|
||||
options.BackgroundServiceExceptionBehavior = BackgroundServiceExceptionBehavior.Ignore;
|
||||
});
|
||||
|
||||
// 添加远程请求服务配置
|
||||
services.AddConfigurableOptions<HttpRemotesOptions>();
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
|
||||
@ -4,6 +4,8 @@
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
using HandlebarsDotNet;
|
||||
|
||||
namespace Admin.NET.Core;
|
||||
|
||||
/// <summary>
|
||||
@ -23,10 +25,9 @@ public static class HttpRemotesExtension
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection AddHttpRemoteClientService(this IServiceCollection services)
|
||||
{
|
||||
var options = App.GetOptions<HttpRemotesOptions>() ?? throw new Exception("未正确配置HttpRemotes.json");
|
||||
foreach (var prop in options.GetType().GetProperties())
|
||||
var options = App.Configuration.GetSection("HttpRemotes").GetChildren().Select(u => u.Get<HttpRemoteItem>());
|
||||
foreach (var opt in options)
|
||||
{
|
||||
if (prop.GetValue(options) is not HttpRemoteItem opt) continue;
|
||||
services.AddHttpClient(opt.HttpName, client =>
|
||||
{
|
||||
client.BaseAddress = new Uri(opt.BaseAddress);
|
||||
@ -112,4 +113,45 @@ public static class HttpRemotesExtension
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 远程请求配置项
|
||||
/// </summary>
|
||||
public sealed class HttpRemoteItem
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否启用日志
|
||||
/// </summary>
|
||||
public bool EnabledLog { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用代理
|
||||
/// </summary>
|
||||
public bool EnabledProxy { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 服务名称
|
||||
/// </summary>
|
||||
public string HttpName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 服务地址
|
||||
/// </summary>
|
||||
public string BaseAddress { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 请求超时时间
|
||||
/// </summary>
|
||||
public int Timeout { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否自动处理Cookie
|
||||
/// </summary>
|
||||
public bool UseCookies { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 请求头
|
||||
/// </summary>
|
||||
public Dictionary<string, string> Headers { get; set; }
|
||||
}
|
||||
@ -12,9 +12,7 @@ namespace Admin.NET.Core;
|
||||
public class HttpLoggingHandler : DelegatingHandler, ITransient
|
||||
{
|
||||
private static readonly Lazy<UserManager> UserManager = new(() => App.GetService<UserManager>());
|
||||
private static readonly string HttpNameKey = "__HTTP_CLIENT_NAME__";
|
||||
|
||||
private readonly Dictionary<string, bool> _enabledLogMap;
|
||||
private readonly Dictionary<string, HttpRemoteItem> _optionMap;
|
||||
private readonly SysConfigService _sysConfigService;
|
||||
private readonly IEventPublisher _eventPublisher;
|
||||
|
||||
@ -22,12 +20,9 @@ public class HttpLoggingHandler : DelegatingHandler, ITransient
|
||||
{
|
||||
_eventPublisher = eventPublisher;
|
||||
_sysConfigService = sysConfigService;
|
||||
|
||||
var options = App.GetOptions<HttpRemotesOptions>() ?? throw new Exception("[HttpRemote] 未正确配置HttpRemotes.json");
|
||||
_enabledLogMap = options.GetType().GetProperties()
|
||||
.Where(u => u.PropertyType == typeof(HttpRemoteItem))
|
||||
.ToDictionary(u => u.GetValue(options) is HttpRemoteItem opt ? opt.HttpName : throw new Exception("[HttpRemote] 未正确配置HttpName"),
|
||||
u => u.GetValue(options) is HttpRemoteItem { EnabledLog: true });
|
||||
_optionMap = App.Configuration.GetSection("HttpRemotes").GetChildren()
|
||||
.Select(u => u.Get<HttpRemoteItem>())
|
||||
.ToDictionary(opt => opt.HttpName, opt => opt);
|
||||
}
|
||||
|
||||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
|
||||
@ -40,7 +35,7 @@ public class HttpLoggingHandler : DelegatingHandler, ITransient
|
||||
// 判断当前配置日志开关
|
||||
var httpClientName = request.GetHttpClientName();
|
||||
var attr = request.GetHttpRemoteApiAttr();
|
||||
if (!string.IsNullOrWhiteSpace(httpClientName)) enabledLog = _enabledLogMap.GetOrDefault(httpClientName);
|
||||
if (!string.IsNullOrWhiteSpace(httpClientName)) enabledLog = _optionMap.GetOrDefault(httpClientName).EnabledLog;
|
||||
if (!enabledLog || attr?.IgnoreLog == true) return await base.SendAsync(request, cancellationToken);
|
||||
|
||||
var stopWatch = Stopwatch.StartNew();
|
||||
@ -84,7 +79,7 @@ public class HttpLoggingHandler : DelegatingHandler, ITransient
|
||||
finally
|
||||
{
|
||||
sysLogHttp.Elapsed = stopWatch.ElapsedMilliseconds;
|
||||
await _eventPublisher.PublishAsync(nameof(AppEventSubscriber.CreateHttpLog), sysLogHttp);
|
||||
await _eventPublisher.PublishAsync(nameof(AppEventSubscriber.CreateHttpLog), sysLogHttp, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,59 +0,0 @@
|
||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||
//
|
||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
namespace Admin.NET.Core;
|
||||
|
||||
/// <summary>
|
||||
/// 远程请求配置
|
||||
/// </summary>
|
||||
public sealed class HttpRemotesOptions : IConfigurableOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// 企业微信
|
||||
/// </summary>
|
||||
public HttpRemoteItem WorkWeixin { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 远程请求配置项
|
||||
/// </summary>
|
||||
public sealed class HttpRemoteItem
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否启用日志
|
||||
/// </summary>
|
||||
public bool EnabledLog { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用代理
|
||||
/// </summary>
|
||||
public bool EnabledProxy { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 服务名称
|
||||
/// </summary>
|
||||
public string HttpName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 服务地址
|
||||
/// </summary>
|
||||
public string BaseAddress { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 请求超时时间
|
||||
/// </summary>
|
||||
public int Timeout { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否自动处理Cookie
|
||||
/// </summary>
|
||||
public bool UseCookies { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 请求头
|
||||
/// </summary>
|
||||
public Dictionary<string, string> Headers { get; set; }
|
||||
}
|
||||
@ -39,7 +39,6 @@ public static class ProjectOptions
|
||||
services.AddConfigurableOptions<RabbitMqConsumerOptions>();
|
||||
services.AddConfigurableOptions<AlipayOptions>();
|
||||
services.AddConfigurableOptions<MqttOptions>();
|
||||
services.AddConfigurableOptions<HttpRemotesOptions>();
|
||||
services.Configure<IpRateLimitOptions>(App.Configuration.GetSection("IpRateLimiting"));
|
||||
services.Configure<IpRateLimitPolicies>(App.Configuration.GetSection("IpRateLimitPolicies"));
|
||||
services.Configure<ClientRateLimitOptions>(App.Configuration.GetSection("ClientRateLimiting"));
|
||||
|
||||
@ -20,7 +20,7 @@ public class WorkWxBaseService(
|
||||
SysConfigService sysConfigService,
|
||||
IHttpRemoteService httpRemoteService) : ITransient
|
||||
{
|
||||
private static readonly Lazy<HttpRemoteItem> Options = new(() => App.GetOptions<HttpRemotesOptions>().WorkWeixin);
|
||||
private static readonly Lazy<HttpRemoteItem> Options = new(() => App.GetConfig<HttpRemoteItem>("HttpRemotes:WorkWeixin", true));
|
||||
|
||||
/// <summary>
|
||||
/// 获取企业微信接口凭证
|
||||
|
||||
Loading…
Reference in New Issue
Block a user