From a1f10b9c09337117a0586b44bed6673c071ea958 Mon Sep 17 00:00:00 2001 From: xRain Date: Tue, 16 Apr 2024 06:57:56 +0800 Subject: [PATCH] =?UTF-8?q?class=20=E6=B7=BB=E5=8A=A0=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E6=96=B9=E6=B3=95,=E6=94=AF=E6=8C=81=E8=80=81=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Communications/SimApiBaseResponse.cs | 17 +++++++++++++++-- Configurations/SimApiDocOptions.cs | 2 +- Configurations/SimApiOptions.cs | 4 ++-- Configurations/SimApiStorageOptions.cs | 2 +- Configurations/SimApiSynapseOptions.cs | 2 +- Helpers/SimApiStorage.cs | 5 +++-- SimApiExtensions.cs | 4 +++- Synapse/Synapse.cs | 11 ++++++++++- 8 files changed, 36 insertions(+), 11 deletions(-) diff --git a/Communications/SimApiBaseResponse.cs b/Communications/SimApiBaseResponse.cs index 460cc03..e36186c 100644 --- a/Communications/SimApiBaseResponse.cs +++ b/Communications/SimApiBaseResponse.cs @@ -52,19 +52,32 @@ public class SimApiBaseResponse(int code = 200, string message = "成功") /// 动态内容分页 /// /// -public class SimApiBasePageResponse : SimApiBaseResponse +public class SimApiBasePageResponse() : SimApiBaseResponse { public T List { get; set; } public int Page { get; set; } = 1; public int Count { get; set; } = 20; public int Total { get; set; } + + public SimApiBasePageResponse(T list, int page, int count, int total) : this() + { + List = list; + Page = page; + Count = count; + Total = total; + } } /// /// 动态Data返回 /// /// -public class SimApiBaseResponse : SimApiBaseResponse +public class SimApiBaseResponse() : SimApiBaseResponse { public T Data { get; set; } + + public SimApiBaseResponse(T data) : this() + { + Data = data; + } } \ No newline at end of file diff --git a/Configurations/SimApiDocOptions.cs b/Configurations/SimApiDocOptions.cs index 2f833cc..7112892 100644 --- a/Configurations/SimApiDocOptions.cs +++ b/Configurations/SimApiDocOptions.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using Swashbuckle.AspNetCore.SwaggerUI; -namespace SimApi.Configs; +namespace SimApi.Configurations; /// /// 文档组配置 diff --git a/Configurations/SimApiOptions.cs b/Configurations/SimApiOptions.cs index 31e8e58..92d3e32 100644 --- a/Configurations/SimApiOptions.cs +++ b/Configurations/SimApiOptions.cs @@ -1,6 +1,6 @@ using System; -namespace SimApi.Configs; +namespace SimApi.Configurations; public class SimApiOptions { @@ -11,7 +11,7 @@ public class SimApiOptions public bool EnableCors { get; set; } = true; /// - /// 启用SimapiAuth,一个简单的基于Header Token的认证方式。 + /// 启用SimApiAuth,一个简单的基于Header Token的认证方式。 /// default: false /// public bool EnableSimApiAuth { get; set; } = false; diff --git a/Configurations/SimApiStorageOptions.cs b/Configurations/SimApiStorageOptions.cs index d3203cb..d58f717 100644 --- a/Configurations/SimApiStorageOptions.cs +++ b/Configurations/SimApiStorageOptions.cs @@ -1,4 +1,4 @@ -namespace SimApi.Configs; +namespace SimApi.Configurations; public class SimApiStorageOptions { diff --git a/Configurations/SimApiSynapseOptions.cs b/Configurations/SimApiSynapseOptions.cs index a4b6866..1bee12c 100644 --- a/Configurations/SimApiSynapseOptions.cs +++ b/Configurations/SimApiSynapseOptions.cs @@ -1,4 +1,4 @@ -namespace SimApi.Configs; +namespace SimApi.Configurations; public class SimApiSynapseOptions { diff --git a/Helpers/SimApiStorage.cs b/Helpers/SimApiStorage.cs index 53884d7..96f9d4c 100644 --- a/Helpers/SimApiStorage.cs +++ b/Helpers/SimApiStorage.cs @@ -2,7 +2,7 @@ using System.IO; using Microsoft.AspNetCore.Http; using Minio; -using SimApi.Configs; +using SimApi.Configurations; namespace SimApi.Helpers; @@ -14,7 +14,7 @@ public class SimApiStorage private string ServeUrl { get; } - public string Bucket { get; } + private string Bucket { get; } private IHttpContextAccessor HttpContextAccessor { get; } @@ -46,6 +46,7 @@ public class SimApiStorage { mcb = mcb.WithSSL(); } + Mc = mcb.Build(); var found = Mc.BucketExistsAsync(new BucketExistsArgs().WithBucket(Bucket)).Result; diff --git a/SimApiExtensions.cs b/SimApiExtensions.cs index 0a345a1..4e6dbf8 100644 --- a/SimApiExtensions.cs +++ b/SimApiExtensions.cs @@ -6,7 +6,7 @@ using Microsoft.OpenApi.Models; using SimApi.Middlewares; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.Extensions.Logging; -using SimApi.Configs; +using SimApi.Configurations; using SimApi.Logger; namespace SimApi; @@ -30,6 +30,7 @@ public static class SimApiExtensions logger.AddProvider(new SimApiLoggerProvider()); }); } + // 是否使用 AUTH if (simApiOptions.EnableSimApiAuth) { @@ -199,6 +200,7 @@ public static class SimApiExtensions var logger = builder.ApplicationServices.GetRequiredService>(); + logger.LogInformation("当前时区: {LocalId}", TimeZoneInfo.Local.Id); if (options.EnableForwardHeaders) { logger.LogInformation("开始配置ForwardedHeaders..."); diff --git a/Synapse/Synapse.cs b/Synapse/Synapse.cs index 18c6dfc..6fe9b30 100644 --- a/Synapse/Synapse.cs +++ b/Synapse/Synapse.cs @@ -8,7 +8,7 @@ using RabbitMQ.Client; using RabbitMQ.Client.Exceptions; using SimApi.Attributes; using SimApi.Communications; -using SimApi.Configs; +using SimApi.Configurations; using SimApi.Helpers; namespace SimApi; @@ -50,6 +50,7 @@ public partial class Synapse { Logger.LogCritical("Synapse初始化失败: AppName or SysName 错误"); } + Options.AppId ??= Guid.NewGuid().ToString(); Logger.LogInformation("System Name: {SysName}\nApp Name: {AppName}\nAppId: {AppId}", Options.SysName, Options.AppName, Options.AppId); @@ -76,10 +77,12 @@ public partial class Synapse RunRpcClient(); Logger.LogInformation("Rpc Client Ready, Client Timeout: {OptionsRpcTimeout}s", Options.RpcTimeout); } + if (RpcRegistry.Count > 0) { RunRpcServer(); } + if (EventRegistry.Count > 0) { RunEventServer(); @@ -99,6 +102,7 @@ public partial class Synapse var data = FireRpc(appName, method, param); res = JsonSerializer.Deserialize>(data, SimApiUtil.JsonOption); } + return res as SimApiBaseResponse; } @@ -152,12 +156,14 @@ public partial class Synapse channel.BasicQos(0, processNum, false); log += $"最大处理器数量: {processNum}"; } + Logger.LogInformation(log); } catch (ConnectFailureException e) { Logger.LogError("Channel [{{Desc}}] 创建失败...\n {0}", e); } + return channel; } @@ -173,6 +179,7 @@ public partial class Synapse { Logger.LogError("Failed to declare Exchange.\n {Err}", e); } + channel.Close(); Logger.LogDebug("Exchange Channel Closed"); } @@ -204,6 +211,7 @@ public partial class Synapse }); } } + if (method.IsDefined(typeof(SynapseRpcAttribute), false)) { var attribute = @@ -220,6 +228,7 @@ public partial class Synapse } } } + var events = EventRegistry.Aggregate(string.Empty, (current, ev) => current + $"\n |- {ev.Key} -> {ev.Method}@{ev.Class.Name}"); var rpcList = RpcRegistry.Aggregate(string.Empty,