Skip to content

Commit 80a05ea

Browse files
committed
移除了version路由, 融合进config
1 parent 33f7cff commit 80a05ea

4 files changed

Lines changed: 49 additions & 23 deletions

File tree

Configurations/SimApiOptions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
using System;
2+
using System.Collections.Generic;
23

34
namespace SimApi.Configurations;
45

56
public class SimApiOptions
67
{
8+
/// <summary>
9+
/// Redis配置
10+
/// </summary>
711
public string? RedisConfiguration { get; set; }
812

13+
/// <summary>
14+
/// 原样返回给前端的配置信息
15+
/// </summary>
16+
public Dictionary<string, object>? WebConfig { get; set; }
17+
18+
/// <summary>
19+
/// WebConfig返回是否包含版本信息
20+
/// </summary>
21+
public bool WebConfigIncludeVersion { get; set; } = true;
22+
923
/// <summary>
1024
/// 是否启用后台任务系统 *基于Hangfire
1125
/// </summary>

Configurations/SimApiRouteOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class SimApiRouteOptions
44
{
5-
public string? VersionRoute = "/versions";
65
public string? LogoutRoute = "/auth/logout";
76
public string? UserInfoRoute = "/user/info";
7+
public string? WebConfigRoute = "/config";
88
}

Controllers/SimApiCommonController.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23
using Microsoft.AspNetCore.Mvc;
4+
using Microsoft.Extensions.Configuration;
35
using SimApi.Attributes;
46
using SimApi.Communications;
7+
using SimApi.Configurations;
58
using SimApi.Helpers;
69
using static SimApi.Helpers.SimApiError;
710

811
namespace SimApi.Controllers;
912

10-
public class SimApiCommonController : SimApiBaseController
13+
public class SimApiCommonController(SimApiOptions simApiOptions) : SimApiBaseController
1114
{
1215
/// <summary>
1316
/// 错误回馈页面
@@ -21,18 +24,27 @@ public void ExceptionHandler(int code)
2124
Error(code);
2225
}
2326

24-
27+
/// <summary>
28+
/// 给前端的自定义信息
29+
/// </summary>
30+
/// <returns></returns>
2531
[HttpPost, HttpGet]
26-
public Dictionary<string, string> Versions()
32+
public Dictionary<string, object> WebConfig()
2733
{
28-
return new Dictionary<string, string>
34+
var resp = simApiOptions.WebConfig!.ToDictionary();
35+
if (simApiOptions.WebConfigIncludeVersion)
2936
{
30-
{ "SimApi", SimApiUtil.SimApiVersion },
31-
{ "App", SimApiUtil.AppVersion }
32-
};
37+
resp.Add("Versions", new Dictionary<string, string>
38+
{
39+
{ "SimApi", SimApiUtil.SimApiVersion },
40+
{ "App", SimApiUtil.AppVersion }
41+
});
42+
}
43+
44+
return resp;
3345
}
34-
35-
46+
47+
3648
/// <summary>
3749
/// 获取已登录用户信息
3850
/// </summary>

SimApiExtensions.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public static IServiceCollection AddSimApi(this IServiceCollection builder,
3636
{
3737
var simApiOptions = new SimApiOptions();
3838
options?.Invoke(simApiOptions);
39+
simApiOptions.WebConfig ??= new();
40+
builder.AddSingleton(simApiOptions);
3941

4042
if (simApiOptions.RedisConfiguration != null)
4143
{
@@ -350,7 +352,6 @@ string GetSimpleTypeName(Type t, int depth = 0)
350352
}
351353

352354
builder.AddSingleton(simApiOptions.SimApiRequestLogOptions);
353-
builder.AddSingleton(simApiOptions);
354355
return builder;
355356
}
356357

@@ -461,18 +462,6 @@ public static WebApplication UseSimApi(this WebApplication builder)
461462
builder.UseMiddleware<SimApiAuthMiddleware>();
462463
}
463464

464-
465-
if (options.SimApiRouteOptions.VersionRoute != null)
466-
{
467-
logger.LogInformation("注册内置Route: Versions => {}", options.SimApiRouteOptions.VersionRoute);
468-
builder.MapControllerRoute(name: "Versions", pattern: options.SimApiRouteOptions.VersionRoute,
469-
defaults: new
470-
{
471-
controller = "SimApiCommon",
472-
action = "Versions"
473-
});
474-
}
475-
476465
if (options.SimApiRouteOptions.UserInfoRoute != null)
477466
{
478467
logger.LogInformation("注册内置Route: UserInfo => {}", options.SimApiRouteOptions.UserInfoRoute);
@@ -495,6 +484,17 @@ public static WebApplication UseSimApi(this WebApplication builder)
495484
});
496485
}
497486

487+
if (options.SimApiRouteOptions.WebConfigRoute != null)
488+
{
489+
logger.LogInformation("注册内置Route: Logout => {}", options.SimApiRouteOptions.WebConfigRoute);
490+
builder.MapControllerRoute(name: "WebConfig", pattern: options.SimApiRouteOptions.WebConfigRoute,
491+
defaults: new
492+
{
493+
controller = "SimApiCommon",
494+
action = "WebConfig"
495+
});
496+
}
497+
498498
if (options.EnableSimApiDoc)
499499
{
500500
logger.LogInformation("开始配置SimApiDoc...");

0 commit comments

Comments
 (0)