feat: 完成swagger分组功能

This commit is contained in:
橙子
2023-12-11 21:14:12 +08:00
parent 9d583e2e5c
commit 27a2849619
8 changed files with 61 additions and 54 deletions

View File

@@ -1,4 +1,7 @@
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Volo.Abp.AspNetCore.Mvc;
namespace Yi.Framework.AspNetCore.Microsoft.AspNetCore.Builder 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) public static IApplicationBuilder UseYiSwagger(this IApplicationBuilder app, params SwaggerModel[] swaggerModels)
{ {
var mvcOptions = app.ApplicationServices.GetRequiredService<IOptions<AbpAspNetCoreMvcOptions>>().Value;
app.UseSwagger(); app.UseSwagger();
app.UseSwaggerUI(c => app.UseSwaggerUI(c =>
{ {
// 配置 Swagger UI 面板链接,添加的顺序,就是排序 foreach (var setting in mvcOptions.ConventionalControllers.ConventionalControllerSettings)
c.SwaggerEndpoint("/swagger/default/swagger.json", "default"); {
c.SwaggerEndpoint("/swagger/rbac/swagger.json", "rbac"); c.SwaggerEndpoint($"/swagger/{setting.RemoteServiceName}/swagger.json", setting.RemoteServiceName);
}
//if (swaggerModels.Length == 0) if (mvcOptions.ConventionalControllers.ConventionalControllerSettings.Count==0&&swaggerModels.Length == 0)
//{ {
// c.SwaggerEndpoint("/swagger/v1/swagger.json", "Yi.Framework"); c.SwaggerEndpoint("/swagger/v1/swagger.json", "Yi.Framework");
//} }
//else else
//{ {
// foreach (var k in swaggerModels) foreach (var k in swaggerModels)
// { {
c.SwaggerEndpoint(k.Url, k.Name);
// c.SwaggerEndpoint(k.Url, k.Name); }
// } }
//}
}); });
return app; return app;

View File

@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc.Controllers; using System.Diagnostics;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
@@ -9,7 +10,7 @@ namespace Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection
{ {
public static class SwaggerAddExtensions public static class SwaggerAddExtensions
{ {
public static IServiceCollection AddYiSwaggerGen<Program>(this IServiceCollection services, Action<SwaggerGenOptions>? action) public static IServiceCollection AddYiSwaggerGen<Program>(this IServiceCollection services, Action<SwaggerGenOptions>? action=null)
{ {
var serviceProvider = services.BuildServiceProvider(); var serviceProvider = services.BuildServiceProvider();
var mvcOptions = serviceProvider.GetRequiredService<IOptions<AbpAspNetCoreMvcOptions>>(); var mvcOptions = serviceProvider.GetRequiredService<IOptions<AbpAspNetCoreMvcOptions>>();
@@ -20,11 +21,18 @@ namespace Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection
services.AddAbpSwaggerGen( services.AddAbpSwaggerGen(
options => options =>
{ {
if (action is not null)
// 配置分组,还需要去重
foreach (var setting in mvcSettings)
{ {
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 文档 // 根据分组名称过滤 API 文档
@@ -38,10 +46,9 @@ namespace Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection
return docName == settingOrNull.RemoteServiceName; return docName == settingOrNull.RemoteServiceName;
} }
} }
return docName == "default"; return false;
}); });
//options.DocInclusionPredicate((docName, description) => true);
options.CustomSchemaIds(type => type.FullName); options.CustomSchemaIds(type => type.FullName);
var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location); var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
if (basePath is not null) if (basePath is not null)
@@ -68,12 +75,6 @@ namespace Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection
{ {
[scheme] = new string[0] [scheme] = new string[0]
}); });
if (action is not null)
{
action.Invoke(options);
}
} }
); );

View File

@@ -29,7 +29,7 @@ namespace Yi.Framework.SqlSugarCore.Repositories
{ {
var db = (await _sugarDbContextProvider.GetDbContextAsync()).SqlSugarClient; var db = (await _sugarDbContextProvider.GetDbContextAsync()).SqlSugarClient;
await Console.Out.WriteLineAsync("获取的id" + db.ContextID); //await Console.Out.WriteLineAsync("获取的id" + db.ContextID);
return db; return db;
} }

View File

@@ -12,7 +12,7 @@ namespace Yi.Framework.SqlSugarCore
{ {
public Task SaveAsync(AuditLogInfo auditInfo) public Task SaveAsync(AuditLogInfo auditInfo)
{ {
Console.WriteLine(auditInfo.ExecutionTime); //Console.WriteLine(auditInfo.ExecutionTime);
return Task.CompletedTask; return Task.CompletedTask;
} }
} }

View File

@@ -76,7 +76,7 @@ namespace Yi.Framework.SqlSugarCore.Uow
{ {
var dbContext = await CreateDbContextAsync(unitOfWork); var dbContext = await CreateDbContextAsync(unitOfWork);
Console.WriteLine("111111" + dbContext.SqlSugarClient.ContextID); // Console.WriteLine("111111" + dbContext.SqlSugarClient.ContextID);
return dbContext; return dbContext;
} }
@@ -101,14 +101,14 @@ namespace Yi.Framework.SqlSugarCore.Uow
unitOfWork.AddTransactionApi(transactionApiKey, transaction); unitOfWork.AddTransactionApi(transactionApiKey, transaction);
await Console.Out.WriteLineAsync("开始新的事务"); //await Console.Out.WriteLineAsync("开始新的事务");
Console.WriteLine(dbContext.SqlSugarClient.ContextID); Console.WriteLine(dbContext.SqlSugarClient.ContextID);
await dbContext.SqlSugarClient.Ado.BeginTranAsync(); await dbContext.SqlSugarClient.Ado.BeginTranAsync();
return dbContext; return dbContext;
} }
else else
{ {
await Console.Out.WriteLineAsync("继续老的事务"); // await Console.Out.WriteLineAsync("继续老的事务");
Console.WriteLine(activeTransaction.DbContext.SqlSugarClient); Console.WriteLine(activeTransaction.DbContext.SqlSugarClient);
await activeTransaction.DbContext.SqlSugarClient.Ado.BeginTranAsync(); await activeTransaction.DbContext.SqlSugarClient.Ado.BeginTranAsync();
return (TDbContext)activeTransaction.DbContext; return (TDbContext)activeTransaction.DbContext;

View File

@@ -61,7 +61,7 @@ namespace Yi.Framework.SqlSugarCore
{ {
CodeFirst(service); CodeFirst(service);
} }
if (options.EnabledCodeFirst) if (options.EnabledDbSeed)
{ {
await DataSeedAsync(service); await DataSeedAsync(service);
} }
@@ -70,9 +70,6 @@ namespace Yi.Framework.SqlSugarCore
private void CodeFirst(IServiceProvider service) private void CodeFirst(IServiceProvider service)
{ {
var options = service.GetRequiredService<IOptions<DbConnOptions>>().Value;
if (options.EnabledCodeFirst)
{
var moduleContainer = service.GetRequiredService<IModuleContainer>(); var moduleContainer = service.GetRequiredService<IModuleContainer>();
var db = service.GetRequiredService<ISqlSugarDbContext>().SqlSugarClient; var db = service.GetRequiredService<ISqlSugarDbContext>().SqlSugarClient;
@@ -85,7 +82,7 @@ namespace Yi.Framework.SqlSugarCore
{ {
db.CodeFirst.InitTables(types.ToArray()); db.CodeFirst.InitTables(types.ToArray());
} }
}
} }
private async Task DataSeedAsync(IServiceProvider service) private async Task DataSeedAsync(IServiceProvider service)

View File

@@ -18,6 +18,7 @@ using Yi.Abp.SqlsugarCore;
using Yi.Framework.AspNetCore; using Yi.Framework.AspNetCore;
using Yi.Framework.AspNetCore.Microsoft.AspNetCore.Builder; using Yi.Framework.AspNetCore.Microsoft.AspNetCore.Builder;
using Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection; using Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection;
using Yi.Framework.Bbs.Application;
using Yi.Framework.Rbac.Application; using Yi.Framework.Rbac.Application;
using Yi.Framework.Rbac.Domain.Shared.Options; using Yi.Framework.Rbac.Domain.Shared.Options;
@@ -43,6 +44,7 @@ namespace Yi.Abp.Web
{ {
var configuration = context.Services.GetConfiguration(); var configuration = context.Services.GetConfiguration();
var service = context.Services; var service = context.Services;
//请求日志 //请求日志
Configure<AbpAuditingOptions>(optios => Configure<AbpAuditingOptions>(optios =>
{ {
@@ -55,6 +57,7 @@ namespace Yi.Abp.Web
{ {
options.ConventionalControllers.Create(typeof(YiAbpApplicationModule).Assembly,options=>options.RemoteServiceName="default"); options.ConventionalControllers.Create(typeof(YiAbpApplicationModule).Assembly,options=>options.RemoteServiceName="default");
options.ConventionalControllers.Create(typeof(YiFrameworkRbacApplicationModule).Assembly, options => options.RemoteServiceName = "rbac"); options.ConventionalControllers.Create(typeof(YiFrameworkRbacApplicationModule).Assembly, options => options.RemoteServiceName = "rbac");
options.ConventionalControllers.Create(typeof(YiFrameworkBbsApplicationModule).Assembly, options => options.RemoteServiceName = "bbs");
}); });
//设置api格式 //设置api格式
@@ -67,21 +70,13 @@ namespace Yi.Abp.Web
{ {
options.OutputDateTimeFormat = "yyyy-MM-dd HH:mm:ss"; options.OutputDateTimeFormat = "yyyy-MM-dd HH:mm:ss";
}); });
Configure<AbpAntiForgeryOptions>(options => Configure<AbpAntiForgeryOptions>(options =>
{ {
options.AutoValidate = false; options.AutoValidate = false;
}); });
//Swagger //Swagger
context.Services.AddYiSwaggerGen<YiAbpWebModule>( context.Services.AddYiSwaggerGen<YiAbpWebModule>();
options =>
{
options.DocInclusionPredicate((docName, description) => true);
options.CustomSchemaIds(type => type.FullName);
}
);
//跨域 //跨域
context.Services.AddCors(options => context.Services.AddCors(options =>
@@ -103,9 +98,6 @@ namespace Yi.Abp.Web
}); });
}); });
//jwt鉴权 //jwt鉴权
var section = configuration.GetSection(nameof(JwtOptions)); var section = configuration.GetSection(nameof(JwtOptions));
Configure<JwtOptions>(section); Configure<JwtOptions>(section);
@@ -153,17 +145,29 @@ namespace Yi.Abp.Web
app.UseRouting(); app.UseRouting();
//跨域
app.UseCors(DefaultCorsPolicyName); app.UseCors(DefaultCorsPolicyName);
//鉴权
app.UseAuthentication(); app.UseAuthentication();
//swagger
app.UseYiSwagger(); app.UseYiSwagger();
//app.UseYiApiHandlinge();
//工作单元
app.UseUnitOfWork(); app.UseUnitOfWork();
//授权
app.UseAuthorization(); app.UseAuthorization();
//审计日志
app.UseAuditing(); app.UseAuditing();
//日志记录
app.UseAbpSerilogEnrichers(); app.UseAbpSerilogEnrichers();
//终节点
app.UseConfiguredEndpoints(); app.UseConfiguredEndpoints();
return Task.CompletedTask; return Task.CompletedTask;

View File

@@ -20,7 +20,8 @@
"DbType": "Sqlite", "DbType": "Sqlite",
"EnabledReadWrite": false, "EnabledReadWrite": false,
"EnabledCodeFirst": true, "EnabledCodeFirst": true,
"EnabledSqlLog":true "EnabledSqlLog": true,
"EnabledDbSeed": true
//读写分离地址 //读写分离地址
//"ReadUrl": [ //"ReadUrl": [
// "DataSource=[xxxx]", //Sqlite // "DataSource=[xxxx]", //Sqlite