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.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<IOptions<AbpAspNetCoreMvcOptions>>().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;

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.Options;
using Microsoft.OpenApi.Models;
@@ -9,7 +10,7 @@ namespace Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection
{
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 mvcOptions = serviceProvider.GetRequiredService<IOptions<AbpAspNetCoreMvcOptions>>();
@@ -20,12 +21,19 @@ namespace Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection
services.AddAbpSwaggerGen(
options =>
{
if (action is not null)
{
action.Invoke(options);
}
// 配置分组,还需要去重
foreach (var setting in mvcSettings)
// 配置分组,还需要去重,支持重写,如果外部传入后,将以外部为准
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 文档
options.DocInclusionPredicate((docName, apiDesc) =>
@@ -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);
}
}
);

View File

@@ -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;
}

View File

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

View File

@@ -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;

View File

@@ -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<IOptions<DbConnOptions>>().Value;
if (options.EnabledCodeFirst)
{
var moduleContainer = service.GetRequiredService<IModuleContainer>();
var db = service.GetRequiredService<ISqlSugarDbContext>().SqlSugarClient;
@@ -85,7 +82,7 @@ namespace Yi.Framework.SqlSugarCore
{
db.CodeFirst.InitTables(types.ToArray());
}
}
}
private async Task DataSeedAsync(IServiceProvider service)

View File

@@ -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<AbpAuditingOptions>(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<AbpAntiForgeryOptions>(options =>
{
options.AutoValidate = false;
});
//Swagger
context.Services.AddYiSwaggerGen<YiAbpWebModule>(
options =>
{
options.DocInclusionPredicate((docName, description) => true);
options.CustomSchemaIds(type => type.FullName);
}
);
context.Services.AddYiSwaggerGen<YiAbpWebModule>();
//跨域
context.Services.AddCors(options =>
@@ -103,9 +98,6 @@ namespace Yi.Abp.Web
});
});
//jwt鉴权
var section = configuration.GetSection(nameof(JwtOptions));
Configure<JwtOptions>(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;

View File

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