minimal api 测试
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
8e1ae18de0
commit
5d51ac562a
|
@ -148,7 +148,7 @@ builder.Services.AddMasaMinimalAPIs(options =>
|
|||
options.DeletePrefixes = new List<string> { "Delete", "Remove" };
|
||||
|
||||
options.RouteHandlerBuilder= t=> {
|
||||
t.RequireAuthorization().AddEndpointFilter<UnifiedApiResultEndpointFilter>();
|
||||
t.RequireAuthorization().AddEndpointFilter<UnifiedApiResultEndpointFilter>().WithGroupName("Institution");
|
||||
};
|
||||
options.DisableTrimMethodPrefix = true; //禁用去除方法前缀
|
||||
options.DisableAutoMapRoute = false;//可通过配置true禁用全局自动路由映射或者删除此配置以启用全局自动路由映射
|
||||
|
|
|
@ -43,70 +43,6 @@ public enum SwaggerVersion
|
|||
|
||||
public static class SwaggerSetup
|
||||
{
|
||||
public static void AddSwaggerSetupOld(this IServiceCollection services)
|
||||
{
|
||||
services.AddEndpointsApiExplorer();
|
||||
|
||||
services.AddSwaggerGen(options =>
|
||||
{
|
||||
typeof(SwaggerVersion).GetFields(BindingFlags.Public | BindingFlags.Static).ToList()
|
||||
.ForEach(field =>
|
||||
{
|
||||
var description = field.GetCustomAttribute<DescriptionAttribute>()?.Description ?? field.Name;
|
||||
options.SwaggerDoc(field.Name, new Microsoft.OpenApi.Models.OpenApiInfo
|
||||
{
|
||||
Version = field.Name,
|
||||
Description = $"{field.Name} API",
|
||||
Title = description // 使用Description作为Title
|
||||
});
|
||||
});
|
||||
|
||||
// 接口排序
|
||||
options.OrderActionsBy(o => o.GroupName);
|
||||
|
||||
options.DocInclusionPredicate((docName, apiDes) =>
|
||||
{
|
||||
if (!apiDes.TryGetMethodInfo(out MethodInfo methodInfo)) return false;
|
||||
var versions = methodInfo.DeclaringType.GetCustomAttributes(true)
|
||||
.OfType<ApiExplorerSettingsAttribute>()
|
||||
.Select(attr => attr.GroupName);
|
||||
|
||||
return versions.Any(v => v.ToString() == docName);
|
||||
});
|
||||
|
||||
|
||||
//添加注释
|
||||
var basePath = AppContext.BaseDirectory;
|
||||
var xmlPath1 = Path.Combine(basePath, "IRaCIS.Core.Application.xml");
|
||||
var xmlPath2 = Path.Combine(basePath, "IRaCIS.Core.API.xml");
|
||||
options.IncludeXmlComments(xmlPath1, true);
|
||||
options.IncludeXmlComments(xmlPath2, true);
|
||||
|
||||
// 在header中添加token,传递到后台
|
||||
options.OperationFilter<SecurityRequirementsOperationFilter>();
|
||||
|
||||
|
||||
// 添加登录按钮
|
||||
options.AddSecurityDefinition("bearerAuth", new OpenApiSecurityScheme()
|
||||
{
|
||||
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
|
||||
Name = "Authorization",
|
||||
|
||||
//In = "header",
|
||||
//Type = "apiKey"
|
||||
});
|
||||
|
||||
//// 添加登录按钮
|
||||
//options.AddSecurityDefinition("bearerAuth", new OpenApiSecurityScheme()
|
||||
//{
|
||||
// Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
|
||||
// Name = "Authorization",
|
||||
|
||||
// //In = "header",
|
||||
// //Type = "apiKey"
|
||||
//});
|
||||
});
|
||||
}
|
||||
public static void AddSwaggerSetup(this IServiceCollection services)
|
||||
{
|
||||
services.AddEndpointsApiExplorer();
|
||||
|
|
|
@ -126,56 +126,5 @@ public class UnifiedApiResultFilter : Attribute, IAsyncResultFilter
|
|||
#endregion
|
||||
|
||||
|
||||
#region minimalapi 流程
|
||||
|
||||
|
||||
public class UnifiedApiResultEndpointFilter : IEndpointFilter
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public UnifiedApiResultEndpointFilter(ILogger<UnifiedApiResultFilter> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
|
||||
{
|
||||
// 调用下一个过滤器或端点处理程序
|
||||
var result = await next(context);
|
||||
|
||||
// 检查返回值类型
|
||||
if (result is null)
|
||||
{
|
||||
return ResponseOutput.NotOk("No data found."); // 处理 null 返回
|
||||
}
|
||||
|
||||
// 如果返回的是元组
|
||||
if (result is ValueTuple<object, object> tuple)
|
||||
{
|
||||
return ResponseOutput.Ok(tuple.Item1, tuple.Item2);
|
||||
}
|
||||
|
||||
if (result is IResponseOutput)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
// 对于其他情况,直接返回结果
|
||||
return ResponseOutput.Ok(result);
|
||||
}
|
||||
|
||||
private IResponseOutput WrapResponse(object? value)
|
||||
{
|
||||
// 处理元组的情况
|
||||
if (value is ValueTuple<object, object> tuple)
|
||||
{
|
||||
return ResponseOutput.Ok(tuple.Item1, tuple.Item2);
|
||||
}
|
||||
|
||||
// 包装单个对象
|
||||
return ResponseOutput.Ok(value);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IRaCIS.Core.Application.Service.BusinessFilter;
|
||||
|
||||
#region minimalapi 流程
|
||||
|
||||
|
||||
public class UnifiedApiResultEndpointFilter : IEndpointFilter
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public UnifiedApiResultEndpointFilter(ILogger<UnifiedApiResultFilter> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
|
||||
{
|
||||
// 调用下一个过滤器或端点处理程序
|
||||
var result = await next(context);
|
||||
|
||||
// 检查返回值类型
|
||||
if (result is null)
|
||||
{
|
||||
return ResponseOutput.NotOk("No data found."); // 处理 null 返回
|
||||
}
|
||||
|
||||
// 如果返回的是元组
|
||||
if (result is ValueTuple<object, object> tuple)
|
||||
{
|
||||
return ResponseOutput.Ok(tuple.Item1, tuple.Item2);
|
||||
}
|
||||
|
||||
if (result is IResponseOutput)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
// 对于其他情况,直接返回结果
|
||||
return ResponseOutput.Ok(result);
|
||||
}
|
||||
|
||||
private IResponseOutput WrapResponse(object? value)
|
||||
{
|
||||
// 处理元组的情况
|
||||
if (value is ValueTuple<object, object> tuple)
|
||||
{
|
||||
return ResponseOutput.Ok(tuple.Item1, tuple.Item2);
|
||||
}
|
||||
|
||||
// 包装单个对象
|
||||
return ResponseOutput.Ok(value);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
|
@ -12681,6 +12681,13 @@
|
|||
组件参考文档:https://docs.masastack.com/framework/building-blocks/minimal-apis#section-69828ff0
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:IRaCIS.Core.Application.Service.TestMinimalApiService.#ctor(IRaCIS.Core.Domain.Share.IUserInfo)">
|
||||
<summary>
|
||||
minimal api 测试
|
||||
学习参考文档:http://fanrk.cn/%E6%8A%80%E6%9C%AF%E6%96%87%E6%A1%A3/MinimalApi/MinimalApi.html
|
||||
组件参考文档:https://docs.masastack.com/framework/building-blocks/minimal-apis#section-69828ff0
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:IRaCIS.Core.Application.Service.TestService.DeleteConsistentDate(System.Guid,IRaCIS.Core.Infra.EFCore.IRepository{IRaCIS.Core.Domain.Models.TaskConsistentRule},IRaCIS.Core.Infra.EFCore.IRepository{IRaCIS.Core.Domain.Models.ReadingConsistentClinicalDataPDF},IRaCIS.Core.Infra.EFCore.IRepository{IRaCIS.Core.Domain.Models.ReadingConsistentClinicalData})">
|
||||
<summary>
|
||||
清理一致性分析任务
|
||||
|
|
|
@ -40,16 +40,10 @@ namespace IRaCIS.Core.Application.Service
|
|||
/// </summary>
|
||||
[ApiExplorerSettings(GroupName = "Institution")]
|
||||
|
||||
public class TestMinimalApiService : ServiceBase
|
||||
public class TestMinimalApiService(IUserInfo _userInfo) : ServiceBase
|
||||
{
|
||||
|
||||
public TestMinimalApiService()
|
||||
{
|
||||
RouteHandlerBuilder = t =>
|
||||
{
|
||||
t.RequireAuthorization().WithGroupName("Institution").AddEndpointFilter<UnifiedApiResultEndpointFilter>();
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public Task<List<string>> GetProjectList1Async()
|
||||
{
|
||||
|
@ -61,6 +55,11 @@ namespace IRaCIS.Core.Application.Service
|
|||
};
|
||||
return Task.FromResult(list);
|
||||
}
|
||||
|
||||
public IResponseOutput GetTest()
|
||||
{
|
||||
return ResponseOutput.Ok(_userInfo.IP);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue