Compare commits
3
Commits
10.26.8.7
...
10.26.8.11
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e17b9d9eed | ||
|
|
80a05ea20b | ||
|
|
33f7cff11c |
@@ -1,11 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SimApi.Configurations;
|
||||
|
||||
public class SimApiOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Redis配置
|
||||
/// </summary>
|
||||
public string? RedisConfiguration { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 原样返回给前端的配置信息
|
||||
/// </summary>
|
||||
public Dictionary<string, object>? WebConfig { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// WebConfig返回是否包含版本信息
|
||||
/// </summary>
|
||||
public bool WebConfigIncludeVersion { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用后台任务系统 *基于Hangfire
|
||||
/// </summary>
|
||||
|
||||
@@ -11,4 +11,9 @@ public class SimApiRequestLogOptions
|
||||
/// 是否打印完整的响应体
|
||||
/// </summary>
|
||||
public bool ShowFullResponse { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 请求字段显示最长长度
|
||||
/// </summary>
|
||||
public int RequestStringLogLength { get; set; } = 0;
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
public class SimApiRouteOptions
|
||||
{
|
||||
public string? VersionRoute = "/versions";
|
||||
public string? LogoutRoute = "/auth/logout";
|
||||
public string? UserInfoRoute = "/user/info";
|
||||
public string? WebConfigRoute = "/config";
|
||||
}
|
||||
@@ -1,13 +1,16 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using SimApi.Attributes;
|
||||
using SimApi.Communications;
|
||||
using SimApi.Configurations;
|
||||
using SimApi.Helpers;
|
||||
using static SimApi.Helpers.SimApiError;
|
||||
|
||||
namespace SimApi.Controllers;
|
||||
|
||||
public class SimApiCommonController : SimApiBaseController
|
||||
public class SimApiCommonController(SimApiOptions simApiOptions) : SimApiBaseController
|
||||
{
|
||||
/// <summary>
|
||||
/// 错误回馈页面
|
||||
@@ -21,15 +24,24 @@ public class SimApiCommonController : SimApiBaseController
|
||||
Error(code);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 给前端的自定义信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpPost, HttpGet]
|
||||
public Dictionary<string, string> Versions()
|
||||
public Dictionary<string, object> WebConfig()
|
||||
{
|
||||
return new Dictionary<string, string>
|
||||
var resp = simApiOptions.WebConfig!.ToDictionary();
|
||||
if (simApiOptions.WebConfigIncludeVersion)
|
||||
{
|
||||
resp.Add("Versions", new Dictionary<string, string>
|
||||
{
|
||||
{ "SimApi", SimApiUtil.SimApiVersion },
|
||||
{ "App", SimApiUtil.AppVersion }
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
return resp;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SimApi.Configurations;
|
||||
using SimApi.Helpers;
|
||||
|
||||
namespace SimApi.Middlewares;
|
||||
|
||||
@@ -53,7 +54,25 @@ public class SimApiRequestLogMiddleware(
|
||||
var requestBodyText = await new StreamReader(context.Request.Body).ReadToEndAsync();
|
||||
context.Request.Body.Seek(0, SeekOrigin.Begin);
|
||||
logMessage.AppendLine("*( RequestBody ) =>");
|
||||
if (options.RequestStringLogLength == 0)
|
||||
{
|
||||
logMessage.AppendLine(requestBodyText);
|
||||
}
|
||||
else
|
||||
{
|
||||
var reqLogs = SimApiUtil.FromJson<Dictionary<string, object>>(requestBodyText);
|
||||
foreach (var reqLog in reqLogs)
|
||||
{
|
||||
if (reqLog.Value is not JsonElement { ValueKind: JsonValueKind.String } je) continue;
|
||||
var str = je.GetString();
|
||||
if (str?.Length > options.RequestStringLogLength)
|
||||
{
|
||||
reqLogs[reqLog.Key] = str[..options.RequestStringLogLength] + $"...({str.Length})";
|
||||
}
|
||||
}
|
||||
|
||||
logMessage.AppendLine(SimApiUtil.Json(reqLogs));
|
||||
}
|
||||
|
||||
var originalBodyStream = context.Response.Body;
|
||||
using var responseBody = new MemoryStream();
|
||||
|
||||
+13
-13
@@ -36,6 +36,8 @@ public static class SimApiExtensions
|
||||
{
|
||||
var simApiOptions = new SimApiOptions();
|
||||
options?.Invoke(simApiOptions);
|
||||
simApiOptions.WebConfig ??= new();
|
||||
builder.AddSingleton(simApiOptions);
|
||||
|
||||
if (simApiOptions.RedisConfiguration != null)
|
||||
{
|
||||
@@ -350,7 +352,6 @@ public static class SimApiExtensions
|
||||
}
|
||||
|
||||
builder.AddSingleton(simApiOptions.SimApiRequestLogOptions);
|
||||
builder.AddSingleton(simApiOptions);
|
||||
return builder;
|
||||
}
|
||||
|
||||
@@ -461,18 +462,6 @@ public static class SimApiExtensions
|
||||
builder.UseMiddleware<SimApiAuthMiddleware>();
|
||||
}
|
||||
|
||||
|
||||
if (options.SimApiRouteOptions.VersionRoute != null)
|
||||
{
|
||||
logger.LogInformation("注册内置Route: Versions => {}", options.SimApiRouteOptions.LogoutRoute);
|
||||
builder.MapControllerRoute(name: "Versions", pattern: options.SimApiRouteOptions.VersionRoute,
|
||||
defaults: new
|
||||
{
|
||||
controller = "SimApiCommon",
|
||||
action = "Versions"
|
||||
});
|
||||
}
|
||||
|
||||
if (options.SimApiRouteOptions.UserInfoRoute != null)
|
||||
{
|
||||
logger.LogInformation("注册内置Route: UserInfo => {}", options.SimApiRouteOptions.UserInfoRoute);
|
||||
@@ -495,6 +484,17 @@ public static class SimApiExtensions
|
||||
});
|
||||
}
|
||||
|
||||
if (options.SimApiRouteOptions.WebConfigRoute != null)
|
||||
{
|
||||
logger.LogInformation("注册内置Route: Logout => {}", options.SimApiRouteOptions.WebConfigRoute);
|
||||
builder.MapControllerRoute(name: "WebConfig", pattern: options.SimApiRouteOptions.WebConfigRoute,
|
||||
defaults: new
|
||||
{
|
||||
controller = "SimApiCommon",
|
||||
action = "WebConfig"
|
||||
});
|
||||
}
|
||||
|
||||
if (options.EnableSimApiDoc)
|
||||
{
|
||||
logger.LogInformation("开始配置SimApiDoc...");
|
||||
|
||||
Reference in New Issue
Block a user