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}" }