From 27a2849619d93f45f49c53590cee282401ade540 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Mon, 11 Dec 2023 21:14:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90swagger=E5=88=86?= =?UTF-8?q?=E7=BB=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Builder/SwaggerBuilderExtensons.cs | 36 ++++++++++--------- .../SwaggerAddExtensions.cs | 29 +++++++-------- .../Repositories/SqlSugarRepository.cs | 2 +- .../SqlSugarLogAuditingStore.cs | 2 +- .../UnitOfWorkSqlsugarDbContextProvider.cs | 6 ++-- .../YiFrameworkSqlSugarCoreModule.cs | 7 ++-- Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs | 30 +++++++++------- Yi.Abp.Net8/src/Yi.Abp.Web/appsettings.json | 3 +- 8 files changed, 61 insertions(+), 54 deletions(-) diff --git a/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/Microsoft/AspNetCore/Builder/SwaggerBuilderExtensons.cs b/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/Microsoft/AspNetCore/Builder/SwaggerBuilderExtensons.cs index 4186561b..7ea50a4c 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/Microsoft/AspNetCore/Builder/SwaggerBuilderExtensons.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/Microsoft/AspNetCore/Builder/SwaggerBuilderExtensons.cs @@ -1,4 +1,7 @@ using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.Mvc; namespace Yi.Framework.AspNetCore.Microsoft.AspNetCore.Builder { @@ -6,25 +9,26 @@ namespace Yi.Framework.AspNetCore.Microsoft.AspNetCore.Builder { public static IApplicationBuilder UseYiSwagger(this IApplicationBuilder app, params SwaggerModel[] swaggerModels) { + var mvcOptions = app.ApplicationServices.GetRequiredService>().Value; + app.UseSwagger(); app.UseSwaggerUI(c => { - // 配置 Swagger UI 面板链接,添加的顺序,就是排序 - c.SwaggerEndpoint("/swagger/default/swagger.json", "default"); - c.SwaggerEndpoint("/swagger/rbac/swagger.json", "rbac"); - - //if (swaggerModels.Length == 0) - //{ - // c.SwaggerEndpoint("/swagger/v1/swagger.json", "Yi.Framework"); - //} - //else - //{ - // foreach (var k in swaggerModels) - // { - - // c.SwaggerEndpoint(k.Url, k.Name); - // } - //} + foreach (var setting in mvcOptions.ConventionalControllers.ConventionalControllerSettings) + { + c.SwaggerEndpoint($"/swagger/{setting.RemoteServiceName}/swagger.json", setting.RemoteServiceName); + } + if (mvcOptions.ConventionalControllers.ConventionalControllerSettings.Count==0&&swaggerModels.Length == 0) + { + c.SwaggerEndpoint("/swagger/v1/swagger.json", "Yi.Framework"); + } + else + { + foreach (var k in swaggerModels) + { + c.SwaggerEndpoint(k.Url, k.Name); + } + } }); return app; diff --git a/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/Microsoft/Extensions/DependencyInjection/SwaggerAddExtensions.cs b/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/Microsoft/Extensions/DependencyInjection/SwaggerAddExtensions.cs index 6c9ea299..8afd6878 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/Microsoft/Extensions/DependencyInjection/SwaggerAddExtensions.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/Microsoft/Extensions/DependencyInjection/SwaggerAddExtensions.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Mvc.Controllers; +using System.Diagnostics; +using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Microsoft.OpenApi.Models; @@ -9,7 +10,7 @@ namespace Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection { public static class SwaggerAddExtensions { - public static IServiceCollection AddYiSwaggerGen(this IServiceCollection services, Action? action) + public static IServiceCollection AddYiSwaggerGen(this IServiceCollection services, Action? action=null) { var serviceProvider = services.BuildServiceProvider(); var mvcOptions = serviceProvider.GetRequiredService>(); @@ -20,11 +21,18 @@ namespace Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection services.AddAbpSwaggerGen( options => { - - // 配置分组,还需要去重 - foreach (var setting in mvcSettings) + if (action is not null) { - options.SwaggerDoc(setting.RemoteServiceName, new OpenApiInfo { Title = setting.RemoteServiceName, Version = "v1" }); + action.Invoke(options); + } + + // 配置分组,还需要去重,支持重写,如果外部传入后,将以外部为准 + foreach (var setting in mvcSettings.OrderBy(x => x.RemoteServiceName)) + { + if (!options.SwaggerGeneratorOptions.SwaggerDocs.ContainsKey(setting.RemoteServiceName)) + { + options.SwaggerDoc(setting.RemoteServiceName, new OpenApiInfo { Title = setting.RemoteServiceName, Version = "v1" }); + } } // 根据分组名称过滤 API 文档 @@ -38,10 +46,9 @@ namespace Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection return docName == settingOrNull.RemoteServiceName; } } - return docName == "default"; + return false; }); - //options.DocInclusionPredicate((docName, description) => true); options.CustomSchemaIds(type => type.FullName); var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location); if (basePath is not null) @@ -68,12 +75,6 @@ namespace Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection { [scheme] = new string[0] }); - - - if (action is not null) - { - action.Invoke(options); - } } ); diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarRepository.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarRepository.cs index f58417fb..55d45fe8 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarRepository.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarRepository.cs @@ -29,7 +29,7 @@ namespace Yi.Framework.SqlSugarCore.Repositories { var db = (await _sugarDbContextProvider.GetDbContextAsync()).SqlSugarClient; - await Console.Out.WriteLineAsync("获取的id:" + db.ContextID); + //await Console.Out.WriteLineAsync("获取的id:" + db.ContextID); return db; } diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarLogAuditingStore.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarLogAuditingStore.cs index 9c49f5bd..a7c92d53 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarLogAuditingStore.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarLogAuditingStore.cs @@ -12,7 +12,7 @@ namespace Yi.Framework.SqlSugarCore { public Task SaveAsync(AuditLogInfo auditInfo) { - Console.WriteLine(auditInfo.ExecutionTime); + //Console.WriteLine(auditInfo.ExecutionTime); return Task.CompletedTask; } } diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs index e407c07a..2f724ef2 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs @@ -76,7 +76,7 @@ namespace Yi.Framework.SqlSugarCore.Uow { var dbContext = await CreateDbContextAsync(unitOfWork); - Console.WriteLine("111111:" + dbContext.SqlSugarClient.ContextID); + // Console.WriteLine("111111:" + dbContext.SqlSugarClient.ContextID); return dbContext; } @@ -101,14 +101,14 @@ namespace Yi.Framework.SqlSugarCore.Uow unitOfWork.AddTransactionApi(transactionApiKey, transaction); - await Console.Out.WriteLineAsync("开始新的事务"); + //await Console.Out.WriteLineAsync("开始新的事务"); Console.WriteLine(dbContext.SqlSugarClient.ContextID); await dbContext.SqlSugarClient.Ado.BeginTranAsync(); return dbContext; } else { - await Console.Out.WriteLineAsync("继续老的事务"); + // await Console.Out.WriteLineAsync("继续老的事务"); Console.WriteLine(activeTransaction.DbContext.SqlSugarClient); await activeTransaction.DbContext.SqlSugarClient.Ado.BeginTranAsync(); return (TDbContext)activeTransaction.DbContext; diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/YiFrameworkSqlSugarCoreModule.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/YiFrameworkSqlSugarCoreModule.cs index a8f4d2c7..25deb79c 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/YiFrameworkSqlSugarCoreModule.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/YiFrameworkSqlSugarCoreModule.cs @@ -61,7 +61,7 @@ namespace Yi.Framework.SqlSugarCore { CodeFirst(service); } - if (options.EnabledCodeFirst) + if (options.EnabledDbSeed) { await DataSeedAsync(service); } @@ -70,9 +70,6 @@ namespace Yi.Framework.SqlSugarCore private void CodeFirst(IServiceProvider service) { - var options = service.GetRequiredService>().Value; - if (options.EnabledCodeFirst) - { var moduleContainer = service.GetRequiredService(); var db = service.GetRequiredService().SqlSugarClient; @@ -85,7 +82,7 @@ namespace Yi.Framework.SqlSugarCore { db.CodeFirst.InitTables(types.ToArray()); } - } + } private async Task DataSeedAsync(IServiceProvider service) diff --git a/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs b/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs index 031bd4a6..fa8a7da3 100644 --- a/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs +++ b/Yi.Abp.Net8/src/Yi.Abp.Web/YiAbpWebModule.cs @@ -18,6 +18,7 @@ using Yi.Abp.SqlsugarCore; using Yi.Framework.AspNetCore; using Yi.Framework.AspNetCore.Microsoft.AspNetCore.Builder; using Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection; +using Yi.Framework.Bbs.Application; using Yi.Framework.Rbac.Application; using Yi.Framework.Rbac.Domain.Shared.Options; @@ -43,6 +44,7 @@ namespace Yi.Abp.Web { var configuration = context.Services.GetConfiguration(); var service = context.Services; + //请求日志 Configure(optios => { @@ -55,6 +57,7 @@ namespace Yi.Abp.Web { options.ConventionalControllers.Create(typeof(YiAbpApplicationModule).Assembly,options=>options.RemoteServiceName="default"); options.ConventionalControllers.Create(typeof(YiFrameworkRbacApplicationModule).Assembly, options => options.RemoteServiceName = "rbac"); + options.ConventionalControllers.Create(typeof(YiFrameworkBbsApplicationModule).Assembly, options => options.RemoteServiceName = "bbs"); }); //设置api格式 @@ -67,21 +70,13 @@ namespace Yi.Abp.Web { options.OutputDateTimeFormat = "yyyy-MM-dd HH:mm:ss"; }); - Configure(options => { options.AutoValidate = false; - }); //Swagger - context.Services.AddYiSwaggerGen( - options => - { - options.DocInclusionPredicate((docName, description) => true); - options.CustomSchemaIds(type => type.FullName); - } - ); + context.Services.AddYiSwaggerGen(); //跨域 context.Services.AddCors(options => @@ -103,9 +98,6 @@ namespace Yi.Abp.Web }); }); - - - //jwt鉴权 var section = configuration.GetSection(nameof(JwtOptions)); Configure(section); @@ -153,17 +145,29 @@ namespace Yi.Abp.Web app.UseRouting(); + + //跨域 app.UseCors(DefaultCorsPolicyName); + + //鉴权 app.UseAuthentication(); + //swagger app.UseYiSwagger(); - //app.UseYiApiHandlinge(); + + //工作单元 app.UseUnitOfWork(); + + //授权 app.UseAuthorization(); + + //审计日志 app.UseAuditing(); + //日志记录 app.UseAbpSerilogEnrichers(); + //终节点 app.UseConfiguredEndpoints(); return Task.CompletedTask; diff --git a/Yi.Abp.Net8/src/Yi.Abp.Web/appsettings.json b/Yi.Abp.Net8/src/Yi.Abp.Web/appsettings.json index 6fd733d5..cce6973b 100644 --- a/Yi.Abp.Net8/src/Yi.Abp.Web/appsettings.json +++ b/Yi.Abp.Net8/src/Yi.Abp.Web/appsettings.json @@ -20,7 +20,8 @@ "DbType": "Sqlite", "EnabledReadWrite": false, "EnabledCodeFirst": true, - "EnabledSqlLog":true + "EnabledSqlLog": true, + "EnabledDbSeed": true //读写分离地址 //"ReadUrl": [ // "DataSource=[xxxx]", //Sqlite