From a42f47011390791491bca859232cbb4f131bc4c0 Mon Sep 17 00:00:00 2001 From: MSuMshk <2039814060@qq.com> Date: Thu, 4 Dec 2025 22:42:46 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E5=85=B3=E9=97=AD=E7=94=9F=E4=BA=A7Sw?= =?UTF-8?q?agger=E5=B9=B6=E7=A7=BB=E9=99=A4=E7=BD=91=E5=85=B3=E8=BD=AC?= =?UTF-8?q?=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Api/TakeoutSaaS.AdminApi/Program.cs | 21 ++++++++---- src/Api/TakeoutSaaS.MiniApi/Program.cs | 21 ++++++++---- src/Api/TakeoutSaaS.UserApi/Program.cs | 21 ++++++++---- .../appsettings.Development.json | 34 ++++++------------- .../TakeoutSaaS.ApiGateway/appsettings.json | 34 ++++++------------- 5 files changed, 62 insertions(+), 69 deletions(-) diff --git a/src/Api/TakeoutSaaS.AdminApi/Program.cs b/src/Api/TakeoutSaaS.AdminApi/Program.cs index 3b0c4b6..f8fe6b9 100644 --- a/src/Api/TakeoutSaaS.AdminApi/Program.cs +++ b/src/Api/TakeoutSaaS.AdminApi/Program.cs @@ -23,6 +23,7 @@ using TakeoutSaaS.Shared.Web.Swagger; // 1. 创建构建器与日志模板 var builder = WebApplication.CreateBuilder(args); const string logTemplate = "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level:u3}] [TraceId:{TraceId}] [SpanId:{SpanId}] [Service:{Service}] {SourceContext} {Message:lj}{NewLine}{Exception}"; +var isDevelopment = builder.Environment.IsDevelopment(); // 2. 加载种子配置文件 builder.Configuration @@ -44,14 +45,17 @@ builder.Host.UseSerilog((context, _, configuration) => outputTemplate: logTemplate); }); -// 4. 注册通用 Web 能力与 Swagger +// 4. 注册通用 Web 能力,开发环境启用 Swagger builder.Services.AddSharedWebCore(); -builder.Services.AddSharedSwagger(options => +if (isDevelopment) { - options.Title = "外卖SaaS - 管理后台"; - options.Description = "管理后台 API 文档"; - options.EnableAuthorization = true; -}); + builder.Services.AddSharedSwagger(options => + { + options.Title = "外卖SaaS - 管理后台"; + options.Description = "管理后台 API 文档"; + options.EnableAuthorization = true; + }); +} // 5. 注册领域与基础设施模块 builder.Services.AddIdentityApplication(); @@ -139,7 +143,10 @@ app.UseTenantResolution(); app.UseSharedWebCore(); app.UseAuthentication(); app.UseAuthorization(); -app.UseSharedSwagger(); +if (app.Environment.IsDevelopment()) +{ + app.UseSharedSwagger(); +} app.UseSchedulerDashboard(builder.Configuration); app.MapHealthChecks("/healthz"); app.MapPrometheusScrapingEndpoint(); diff --git a/src/Api/TakeoutSaaS.MiniApi/Program.cs b/src/Api/TakeoutSaaS.MiniApi/Program.cs index a6685cb..399f187 100644 --- a/src/Api/TakeoutSaaS.MiniApi/Program.cs +++ b/src/Api/TakeoutSaaS.MiniApi/Program.cs @@ -21,6 +21,7 @@ using System.Reflection; // 1. 创建构建器与日志模板 var builder = WebApplication.CreateBuilder(args); const string logTemplate = "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level:u3}] [TraceId:{TraceId}] [SpanId:{SpanId}] [Service:{Service}] {SourceContext} {Message:lj}{NewLine}{Exception}"; +var isDevelopment = builder.Environment.IsDevelopment(); // 2. 注册雪花 ID 生成器与 Serilog builder.Services.AddSingleton(_ => new SnowflakeIdGenerator()); @@ -38,14 +39,17 @@ builder.Host.UseSerilog((_, _, configuration) => outputTemplate: logTemplate); }); -// 3. 注册通用 Web 能力与 Swagger +// 3. 注册通用 Web 能力,开发环境启用 Swagger builder.Services.AddSharedWebCore(); -builder.Services.AddSharedSwagger(options => +if (isDevelopment) { - options.Title = "外卖SaaS - 小程序端"; - options.Description = "小程序 API 文档"; - options.EnableAuthorization = true; -}); + builder.Services.AddSharedSwagger(options => + { + options.Title = "外卖SaaS - 小程序端"; + options.Description = "小程序 API 文档"; + options.EnableAuthorization = true; + }); +} builder.Services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly()); // 4. 注册 Redis 分布式缓存,供验证码等功能使用 @@ -133,7 +137,10 @@ var app = builder.Build(); app.UseCors("MiniApiCors"); app.UseTenantResolution(); app.UseSharedWebCore(); -app.UseSharedSwagger(); +if (app.Environment.IsDevelopment()) +{ + app.UseSharedSwagger(); +} app.MapHealthChecks("/healthz"); app.MapPrometheusScrapingEndpoint(); diff --git a/src/Api/TakeoutSaaS.UserApi/Program.cs b/src/Api/TakeoutSaaS.UserApi/Program.cs index d59e501..180aa83 100644 --- a/src/Api/TakeoutSaaS.UserApi/Program.cs +++ b/src/Api/TakeoutSaaS.UserApi/Program.cs @@ -12,6 +12,7 @@ using TakeoutSaaS.Shared.Web.Swagger; // 1. 创建构建器与日志模板 var builder = WebApplication.CreateBuilder(args); const string logTemplate = "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level:u3}] [TraceId:{TraceId}] [SpanId:{SpanId}] [Service:{Service}] {SourceContext} {Message:lj}{NewLine}{Exception}"; +var isDevelopment = builder.Environment.IsDevelopment(); // 2. 注册雪花 ID 生成器与 Serilog builder.Services.AddSingleton(_ => new SnowflakeIdGenerator()); @@ -29,14 +30,17 @@ builder.Host.UseSerilog((_, _, configuration) => outputTemplate: logTemplate); }); -// 3. 注册通用 Web 能力与 Swagger +// 3. 注册通用 Web 能力,开发环境启用 Swagger builder.Services.AddSharedWebCore(); -builder.Services.AddSharedSwagger(options => +if (isDevelopment) { - options.Title = "外卖SaaS - 用户端"; - options.Description = "C 端用户 API 文档"; - options.EnableAuthorization = true; -}); + builder.Services.AddSharedSwagger(options => + { + options.Title = "外卖SaaS - 用户端"; + options.Description = "C 端用户 API 文档"; + options.EnableAuthorization = true; + }); +} // 4. 注册多租户与健康检查 builder.Services.AddTenantResolution(builder.Configuration); @@ -110,7 +114,10 @@ var app = builder.Build(); app.UseCors("UserApiCors"); app.UseTenantResolution(); app.UseSharedWebCore(); -app.UseSharedSwagger(); +if (app.Environment.IsDevelopment()) +{ + app.UseSharedSwagger(); +} app.MapHealthChecks("/healthz"); app.MapPrometheusScrapingEndpoint(); diff --git a/src/Gateway/TakeoutSaaS.ApiGateway/appsettings.Development.json b/src/Gateway/TakeoutSaaS.ApiGateway/appsettings.Development.json index ebc9b30..56dd41a 100644 --- a/src/Gateway/TakeoutSaaS.ApiGateway/appsettings.Development.json +++ b/src/Gateway/TakeoutSaaS.ApiGateway/appsettings.Development.json @@ -2,30 +2,16 @@ "OpenTelemetry": { "Enabled": false }, - "ReverseProxy": { - "Routes": { - "admin-route": { - "ClusterId": "admin", - "Match": { "Path": "/api/admin/{**catch-all}" } - }, - "admin-swagger": { - "ClusterId": "admin", - "Match": { "Path": "/api/admin/swagger/{**catch-all}" }, - "Transforms": [ - { "PathRemovePrefix": "/api/admin" } - ] - }, - "admin-swagger-v": { - "ClusterId": "admin", - "Match": { "Path": "/api/admin/v{version}/swagger/{**catch-all}" }, - "Transforms": [ - { "PathPattern": "/swagger/{**catch-all}" } - ] - }, - "mini-route": { - "ClusterId": "mini", - "Match": { "Path": "/api/mini/{**catch-all}" } - }, + "ReverseProxy": { + "Routes": { + "admin-route": { + "ClusterId": "admin", + "Match": { "Path": "/api/admin/{**catch-all}" } + }, + "mini-route": { + "ClusterId": "mini", + "Match": { "Path": "/api/mini/{**catch-all}" } + }, "user-route": { "ClusterId": "user", "Match": { "Path": "/api/user/{**catch-all}" } diff --git a/src/Gateway/TakeoutSaaS.ApiGateway/appsettings.json b/src/Gateway/TakeoutSaaS.ApiGateway/appsettings.json index 41e328a..0c5d718 100644 --- a/src/Gateway/TakeoutSaaS.ApiGateway/appsettings.json +++ b/src/Gateway/TakeoutSaaS.ApiGateway/appsettings.json @@ -35,30 +35,16 @@ "ServiceName": "TakeoutSaaS.ApiGateway", "OtlpEndpoint": "http://localhost:4317" }, - "ReverseProxy": { - "Routes": { - "admin-route": { - "ClusterId": "admin", - "Match": { "Path": "/api/admin/{**catch-all}" } - }, - "admin-swagger": { - "ClusterId": "admin", - "Match": { "Path": "/api/admin/swagger/{**catch-all}" }, - "Transforms": [ - { "PathRemovePrefix": "/api/admin" } - ] - }, - "admin-swagger-v": { - "ClusterId": "admin", - "Match": { "Path": "/api/admin/v{version}/swagger/{**catch-all}" }, - "Transforms": [ - { "PathPattern": "/swagger/{**catch-all}" } - ] - }, - "mini-route": { - "ClusterId": "mini", - "Match": { "Path": "/api/mini/{**catch-all}" } - }, + "ReverseProxy": { + "Routes": { + "admin-route": { + "ClusterId": "admin", + "Match": { "Path": "/api/admin/{**catch-all}" } + }, + "mini-route": { + "ClusterId": "mini", + "Match": { "Path": "/api/mini/{**catch-all}" } + }, "user-route": { "ClusterId": "user", "Match": { "Path": "/api/user/{**catch-all}" }