Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c40f8d664d | ||
|
|
a1f10b9c09 |
@@ -52,19 +52,32 @@ public class SimApiBaseResponse(int code = 200, string message = "成功")
|
|||||||
/// 动态内容分页
|
/// 动态内容分页
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
public class SimApiBasePageResponse<T> : SimApiBaseResponse
|
public class SimApiBasePageResponse<T>() : SimApiBaseResponse
|
||||||
{
|
{
|
||||||
public T List { get; set; }
|
public T List { get; set; }
|
||||||
public int Page { get; set; } = 1;
|
public int Page { get; set; } = 1;
|
||||||
public int Count { get; set; } = 20;
|
public int Count { get; set; } = 20;
|
||||||
public int Total { get; set; }
|
public int Total { get; set; }
|
||||||
|
|
||||||
|
public SimApiBasePageResponse(T list, int page, int count, int total) : this()
|
||||||
|
{
|
||||||
|
List = list;
|
||||||
|
Page = page;
|
||||||
|
Count = count;
|
||||||
|
Total = total;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 动态Data返回
|
/// 动态Data返回
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
public class SimApiBaseResponse<T> : SimApiBaseResponse
|
public class SimApiBaseResponse<T>() : SimApiBaseResponse
|
||||||
{
|
{
|
||||||
public T Data { get; set; }
|
public T Data { get; set; }
|
||||||
|
|
||||||
|
public SimApiBaseResponse(T data) : this()
|
||||||
|
{
|
||||||
|
Data = data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Swashbuckle.AspNetCore.SwaggerUI;
|
using Swashbuckle.AspNetCore.SwaggerUI;
|
||||||
|
|
||||||
namespace SimApi.Configs;
|
namespace SimApi.Configurations;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 文档组配置
|
/// 文档组配置
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace SimApi.Configs;
|
namespace SimApi.Configurations;
|
||||||
|
|
||||||
public class SimApiOptions
|
public class SimApiOptions
|
||||||
{
|
{
|
||||||
@@ -11,7 +11,7 @@ public class SimApiOptions
|
|||||||
public bool EnableCors { get; set; } = true;
|
public bool EnableCors { get; set; } = true;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 启用SimapiAuth,一个简单的基于Header Token的认证方式。
|
/// 启用SimApiAuth,一个简单的基于Header Token的认证方式。
|
||||||
/// default: false
|
/// default: false
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool EnableSimApiAuth { get; set; } = false;
|
public bool EnableSimApiAuth { get; set; } = false;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
namespace SimApi.Configs;
|
namespace SimApi.Configurations;
|
||||||
|
|
||||||
public class SimApiStorageOptions
|
public class SimApiStorageOptions
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
namespace SimApi.Configs;
|
namespace SimApi.Configurations;
|
||||||
|
|
||||||
public class SimApiSynapseOptions
|
public class SimApiSynapseOptions
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ public class SimApiBaseController : Controller
|
|||||||
/// <param name="condition">检测条件</param>
|
/// <param name="condition">检测条件</param>
|
||||||
/// <param name="code">错误代码</param>
|
/// <param name="code">错误代码</param>
|
||||||
/// <param name="message">错误描述</param>
|
/// <param name="message">错误描述</param>
|
||||||
protected static void ErrorWhen(bool condition, int code = 500, string message = "")
|
protected static void ErrorWhen(bool condition, int code = 400, string message = "")
|
||||||
{
|
{
|
||||||
if (condition)
|
if (condition)
|
||||||
{
|
{
|
||||||
@@ -70,7 +70,7 @@ public class SimApiBaseController : Controller
|
|||||||
/// <param name="condition">检测条件</param>
|
/// <param name="condition">检测条件</param>
|
||||||
/// <param name="code">错误代码</param>
|
/// <param name="code">错误代码</param>
|
||||||
/// <param name="message">错误描述</param>
|
/// <param name="message">错误描述</param>
|
||||||
protected static void ErrorWhenTrue(bool condition, int code = 500, string message = "")
|
protected static void ErrorWhenTrue(bool condition, int code = 400, string message = "")
|
||||||
{
|
{
|
||||||
ErrorWhen(condition, code, message);
|
ErrorWhen(condition, code, message);
|
||||||
}
|
}
|
||||||
@@ -82,7 +82,7 @@ public class SimApiBaseController : Controller
|
|||||||
/// <param name="condition"></param>
|
/// <param name="condition"></param>
|
||||||
/// <param name="code"></param>
|
/// <param name="code"></param>
|
||||||
/// <param name="message"></param>
|
/// <param name="message"></param>
|
||||||
protected static void ErrorWhenFalse(bool condition, int code = 500, string message = "")
|
protected static void ErrorWhenFalse(bool condition, int code = 400, string message = "")
|
||||||
{
|
{
|
||||||
ErrorWhen(!condition, code, message);
|
ErrorWhen(!condition, code, message);
|
||||||
}
|
}
|
||||||
@@ -93,7 +93,7 @@ public class SimApiBaseController : Controller
|
|||||||
/// <param name="condition">检测条件</param>
|
/// <param name="condition">检测条件</param>
|
||||||
/// <param name="code">错误代码</param>
|
/// <param name="code">错误代码</param>
|
||||||
/// <param name="message">错误描述</param>
|
/// <param name="message">错误描述</param>
|
||||||
protected static void ErrorWhenNull(object condition, int code = 404, string message = "")
|
protected static void ErrorWhenNull(object condition, int code = 404, string message = "请求的资源不存在")
|
||||||
{
|
{
|
||||||
ErrorWhen(condition == null, code, message);
|
ErrorWhen(condition == null, code, message);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Minio;
|
using Minio;
|
||||||
using SimApi.Configs;
|
using SimApi.Configurations;
|
||||||
|
|
||||||
namespace SimApi.Helpers;
|
namespace SimApi.Helpers;
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@ public class SimApiStorage
|
|||||||
|
|
||||||
private string ServeUrl { get; }
|
private string ServeUrl { get; }
|
||||||
|
|
||||||
public string Bucket { get; }
|
private string Bucket { get; }
|
||||||
|
|
||||||
private IHttpContextAccessor HttpContextAccessor { get; }
|
private IHttpContextAccessor HttpContextAccessor { get; }
|
||||||
|
|
||||||
@@ -46,6 +46,7 @@ public class SimApiStorage
|
|||||||
{
|
{
|
||||||
mcb = mcb.WithSSL();
|
mcb = mcb.WithSSL();
|
||||||
}
|
}
|
||||||
|
|
||||||
Mc = mcb.Build();
|
Mc = mcb.Build();
|
||||||
|
|
||||||
var found = Mc.BucketExistsAsync(new BucketExistsArgs().WithBucket(Bucket)).Result;
|
var found = Mc.BucketExistsAsync(new BucketExistsArgs().WithBucket(Bucket)).Result;
|
||||||
|
|||||||
@@ -51,16 +51,15 @@ public static class SimApiUtil
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string Md5(string source, string mode = "x2")
|
public static string Md5(string source, string mode = "x2")
|
||||||
{
|
{
|
||||||
var sor = Encoding.UTF8.GetBytes(source);
|
var sourceBytes = Encoding.UTF8.GetBytes(source);
|
||||||
var md5 = MD5.Create();
|
var result = MD5.HashData(sourceBytes);
|
||||||
var result = md5.ComputeHash(sor);
|
var stringBuilder = new StringBuilder(40);
|
||||||
var strbul = new StringBuilder(40);
|
|
||||||
foreach (var t in result)
|
foreach (var t in result)
|
||||||
{
|
{
|
||||||
strbul.Append(t.ToString(mode));
|
stringBuilder.Append(t.ToString(mode));
|
||||||
}
|
}
|
||||||
|
|
||||||
return strbul.ToString();
|
return stringBuilder.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -24,14 +24,19 @@ public class SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExcep
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
await next(context);
|
await next(context);
|
||||||
if (context.Response.StatusCode != 200)
|
switch (context.Response.StatusCode)
|
||||||
{
|
|
||||||
if (!new[] { 301, 302 }.Contains(context.Response.StatusCode))
|
|
||||||
{
|
{
|
||||||
|
case 200:
|
||||||
|
break;
|
||||||
|
case 404:
|
||||||
|
throw new SimApiException(context.Response.StatusCode, "请求的接口不存在");
|
||||||
|
case 301:
|
||||||
|
case 302:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
throw new SimApiException(context.Response.StatusCode);
|
throw new SimApiException(context.Response.StatusCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
catch (SimApiException ex)
|
catch (SimApiException ex)
|
||||||
{
|
{
|
||||||
response = string.IsNullOrEmpty(ex.Message)
|
response = string.IsNullOrEmpty(ex.Message)
|
||||||
@@ -42,8 +47,8 @@ public class SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExcep
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
log.LogError(ex.Message);
|
log.LogError("{Msg}", ex.Message);
|
||||||
log.LogError(ex.StackTrace);
|
log.LogError("{Msg}", ex.StackTrace);
|
||||||
response = new SimApiBaseResponse(500, ex.Message);
|
response = new SimApiBaseResponse(500, ex.Message);
|
||||||
ErrorResponse(context, response);
|
ErrorResponse(context, response);
|
||||||
}
|
}
|
||||||
@@ -54,13 +59,11 @@ public class SimApiExceptionMiddleware(RequestDelegate next, ILogger<SimApiExcep
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="context"></param>
|
/// <param name="context"></param>
|
||||||
/// <param name="response"></param>
|
/// <param name="response"></param>
|
||||||
private void ErrorResponse(HttpContext context, SimApiBaseResponse response)
|
private static void ErrorResponse(HttpContext context, SimApiBaseResponse response)
|
||||||
{
|
|
||||||
if (!context.Response.HasStarted)
|
|
||||||
{
|
{
|
||||||
|
if (context.Response.HasStarted) return;
|
||||||
context.Response.StatusCode = 200;
|
context.Response.StatusCode = 200;
|
||||||
context.Response.Headers.Append("Content-Type", "application/json");
|
context.Response.Headers.Append("Content-Type", "application/json");
|
||||||
context.Response.WriteAsync(response.ToString());
|
context.Response.WriteAsync(response.ToString());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
+3
-1
@@ -6,7 +6,7 @@ using Microsoft.OpenApi.Models;
|
|||||||
using SimApi.Middlewares;
|
using SimApi.Middlewares;
|
||||||
using Microsoft.AspNetCore.HttpOverrides;
|
using Microsoft.AspNetCore.HttpOverrides;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using SimApi.Configs;
|
using SimApi.Configurations;
|
||||||
using SimApi.Logger;
|
using SimApi.Logger;
|
||||||
|
|
||||||
namespace SimApi;
|
namespace SimApi;
|
||||||
@@ -30,6 +30,7 @@ public static class SimApiExtensions
|
|||||||
logger.AddProvider(new SimApiLoggerProvider());
|
logger.AddProvider(new SimApiLoggerProvider());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 是否使用 AUTH
|
// 是否使用 AUTH
|
||||||
if (simApiOptions.EnableSimApiAuth)
|
if (simApiOptions.EnableSimApiAuth)
|
||||||
{
|
{
|
||||||
@@ -199,6 +200,7 @@ public static class SimApiExtensions
|
|||||||
|
|
||||||
var logger = builder.ApplicationServices.GetRequiredService<ILogger<SimApiOptions>>();
|
var logger = builder.ApplicationServices.GetRequiredService<ILogger<SimApiOptions>>();
|
||||||
|
|
||||||
|
logger.LogInformation("当前时区: {LocalId}", TimeZoneInfo.Local.Id);
|
||||||
if (options.EnableForwardHeaders)
|
if (options.EnableForwardHeaders)
|
||||||
{
|
{
|
||||||
logger.LogInformation("开始配置ForwardedHeaders...");
|
logger.LogInformation("开始配置ForwardedHeaders...");
|
||||||
|
|||||||
+10
-1
@@ -8,7 +8,7 @@ using RabbitMQ.Client;
|
|||||||
using RabbitMQ.Client.Exceptions;
|
using RabbitMQ.Client.Exceptions;
|
||||||
using SimApi.Attributes;
|
using SimApi.Attributes;
|
||||||
using SimApi.Communications;
|
using SimApi.Communications;
|
||||||
using SimApi.Configs;
|
using SimApi.Configurations;
|
||||||
using SimApi.Helpers;
|
using SimApi.Helpers;
|
||||||
|
|
||||||
namespace SimApi;
|
namespace SimApi;
|
||||||
@@ -50,6 +50,7 @@ public partial class Synapse
|
|||||||
{
|
{
|
||||||
Logger.LogCritical("Synapse初始化失败: AppName or SysName 错误");
|
Logger.LogCritical("Synapse初始化失败: AppName or SysName 错误");
|
||||||
}
|
}
|
||||||
|
|
||||||
Options.AppId ??= Guid.NewGuid().ToString();
|
Options.AppId ??= Guid.NewGuid().ToString();
|
||||||
Logger.LogInformation("System Name: {SysName}\nApp Name: {AppName}\nAppId: {AppId}", Options.SysName,
|
Logger.LogInformation("System Name: {SysName}\nApp Name: {AppName}\nAppId: {AppId}", Options.SysName,
|
||||||
Options.AppName, Options.AppId);
|
Options.AppName, Options.AppId);
|
||||||
@@ -76,10 +77,12 @@ public partial class Synapse
|
|||||||
RunRpcClient();
|
RunRpcClient();
|
||||||
Logger.LogInformation("Rpc Client Ready, Client Timeout: {OptionsRpcTimeout}s", Options.RpcTimeout);
|
Logger.LogInformation("Rpc Client Ready, Client Timeout: {OptionsRpcTimeout}s", Options.RpcTimeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (RpcRegistry.Count > 0)
|
if (RpcRegistry.Count > 0)
|
||||||
{
|
{
|
||||||
RunRpcServer();
|
RunRpcServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (EventRegistry.Count > 0)
|
if (EventRegistry.Count > 0)
|
||||||
{
|
{
|
||||||
RunEventServer();
|
RunEventServer();
|
||||||
@@ -99,6 +102,7 @@ public partial class Synapse
|
|||||||
var data = FireRpc(appName, method, param);
|
var data = FireRpc(appName, method, param);
|
||||||
res = JsonSerializer.Deserialize<SimApiBaseResponse<T>>(data, SimApiUtil.JsonOption);
|
res = JsonSerializer.Deserialize<SimApiBaseResponse<T>>(data, SimApiUtil.JsonOption);
|
||||||
}
|
}
|
||||||
|
|
||||||
return res as SimApiBaseResponse<T>;
|
return res as SimApiBaseResponse<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,12 +156,14 @@ public partial class Synapse
|
|||||||
channel.BasicQos(0, processNum, false);
|
channel.BasicQos(0, processNum, false);
|
||||||
log += $"最大处理器数量: {processNum}";
|
log += $"最大处理器数量: {processNum}";
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.LogInformation(log);
|
Logger.LogInformation(log);
|
||||||
}
|
}
|
||||||
catch (ConnectFailureException e)
|
catch (ConnectFailureException e)
|
||||||
{
|
{
|
||||||
Logger.LogError("Channel [{{Desc}}] 创建失败...\n {0}", e);
|
Logger.LogError("Channel [{{Desc}}] 创建失败...\n {0}", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return channel;
|
return channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,6 +179,7 @@ public partial class Synapse
|
|||||||
{
|
{
|
||||||
Logger.LogError("Failed to declare Exchange.\n {Err}", e);
|
Logger.LogError("Failed to declare Exchange.\n {Err}", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
channel.Close();
|
channel.Close();
|
||||||
Logger.LogDebug("Exchange Channel Closed");
|
Logger.LogDebug("Exchange Channel Closed");
|
||||||
}
|
}
|
||||||
@@ -204,6 +211,7 @@ public partial class Synapse
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (method.IsDefined(typeof(SynapseRpcAttribute), false))
|
if (method.IsDefined(typeof(SynapseRpcAttribute), false))
|
||||||
{
|
{
|
||||||
var attribute =
|
var attribute =
|
||||||
@@ -220,6 +228,7 @@ public partial class Synapse
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var events = EventRegistry.Aggregate(string.Empty,
|
var events = EventRegistry.Aggregate(string.Empty,
|
||||||
(current, ev) => current + $"\n |- {ev.Key} -> {ev.Method}@{ev.Class.Name}");
|
(current, ev) => current + $"\n |- {ev.Key} -> {ev.Method}@{ev.Class.Name}");
|
||||||
var rpcList = RpcRegistry.Aggregate(string.Empty,
|
var rpcList = RpcRegistry.Aggregate(string.Empty,
|
||||||
|
|||||||
Reference in New Issue
Block a user