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