2026-05-11 14:35:34 +08:00
|
|
|
using Avalonia_Services.Core;
|
2026-05-15 17:35:07 +08:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2026-05-11 14:35:34 +08:00
|
|
|
using AspNetCoreFilterContext = Microsoft.AspNetCore.Http.EndpointFilterInvocationContext;
|
|
|
|
|
using AspNetCoreFilterDelegate = Microsoft.AspNetCore.Http.EndpointFilterDelegate;
|
|
|
|
|
// 解决与 ASP.NET Core 同名类型的冲突
|
|
|
|
|
using UnifiedFilter = Avalonia_Services.Core.IEndpointFilter;
|
|
|
|
|
|
|
|
|
|
namespace Avalonia_API.Extensions
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 将 Avalonia-Services 的统一端点映射到 ASP.NET Core Minimal API。
|
|
|
|
|
/// 支持鉴权、过滤器、中间件的完整 ASP.NET Core 管道。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class UnifiedEndpointExtensions
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 将 ServiceEndpointCollection 中的所有端点注册到 ASP.NET Core 路由。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static IEndpointRouteBuilder MapUnifiedEndpoints(
|
|
|
|
|
this IEndpointRouteBuilder routeBuilder,
|
|
|
|
|
ServiceEndpointCollection endpoints,
|
|
|
|
|
IServiceProvider serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
var apiGroup = routeBuilder.MapGroup("/");
|
|
|
|
|
|
2026-05-15 17:35:07 +08:00
|
|
|
foreach (var endpoint in endpoints.ForHost(EndpointHostTarget.Api))
|
2026-05-11 14:35:34 +08:00
|
|
|
{
|
|
|
|
|
var routeHandlerBuilder = MapEndpoint(apiGroup, endpoint, serviceProvider);
|
|
|
|
|
|
|
|
|
|
// 全局过滤器 → ASP.NET Core Endpoint Filters
|
|
|
|
|
foreach (var globalFilter in endpoints.GlobalFilters)
|
|
|
|
|
{
|
|
|
|
|
routeHandlerBuilder.AddEndpointFilter(
|
|
|
|
|
async (context, next) => await ConvertFilterAsync(globalFilter, context, next));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 端点专属过滤器
|
|
|
|
|
foreach (var filter in endpoint.Filters)
|
|
|
|
|
{
|
|
|
|
|
routeHandlerBuilder.AddEndpointFilter(
|
|
|
|
|
async (context, next) => await ConvertFilterAsync(filter, context, next));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 鉴权(使用 ASP.NET Core 原生鉴权机制)
|
|
|
|
|
if (endpoint.RequireAuthorization)
|
|
|
|
|
{
|
2026-05-15 17:35:07 +08:00
|
|
|
if (endpoint.Roles.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
routeHandlerBuilder.RequireAuthorization(new AuthorizeAttribute
|
|
|
|
|
{
|
|
|
|
|
Roles = string.Join(',', endpoint.Roles),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else if (!string.IsNullOrEmpty(endpoint.Policy))
|
2026-05-11 14:35:34 +08:00
|
|
|
{
|
|
|
|
|
routeHandlerBuilder.RequireAuthorization(endpoint.Policy);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
routeHandlerBuilder.RequireAuthorization();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(endpoint.Name))
|
|
|
|
|
{
|
|
|
|
|
routeHandlerBuilder.WithName(endpoint.Name);
|
|
|
|
|
}
|
2026-05-15 17:35:07 +08:00
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(endpoint.OpenApiTag))
|
|
|
|
|
{
|
|
|
|
|
routeHandlerBuilder.WithTags(endpoint.OpenApiTag);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(endpoint.OpenApiDescription))
|
|
|
|
|
{
|
|
|
|
|
routeHandlerBuilder.WithDescription(endpoint.OpenApiDescription);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(endpoint.OpenApiSummary))
|
|
|
|
|
{
|
|
|
|
|
routeHandlerBuilder.WithSummary(endpoint.OpenApiSummary);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (endpoint.OpenApiRequestType is not null)
|
|
|
|
|
{
|
|
|
|
|
routeHandlerBuilder.Accepts(endpoint.OpenApiRequestType, "application/json");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (endpoint.OpenApiResponseType is not null)
|
|
|
|
|
{
|
|
|
|
|
routeHandlerBuilder.Produces(200, endpoint.OpenApiResponseType, "application/json");
|
|
|
|
|
}
|
2026-05-11 14:35:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return routeBuilder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static RouteHandlerBuilder MapEndpoint(
|
|
|
|
|
IEndpointRouteBuilder group,
|
|
|
|
|
ServiceEndpoint endpoint,
|
|
|
|
|
IServiceProvider serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
var handler = CreateAspNetCoreHandler(endpoint.Handler, serviceProvider);
|
|
|
|
|
|
|
|
|
|
return endpoint.HttpMethod.ToUpperInvariant() switch
|
|
|
|
|
{
|
|
|
|
|
"GET" => group.MapGet(endpoint.Pattern, handler),
|
|
|
|
|
"POST" => group.MapPost(endpoint.Pattern, handler),
|
|
|
|
|
"PUT" => group.MapPut(endpoint.Pattern, handler),
|
|
|
|
|
"DELETE" => group.MapDelete(endpoint.Pattern, handler),
|
|
|
|
|
_ => group.MapGet(endpoint.Pattern, handler),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Delegate CreateAspNetCoreHandler(
|
|
|
|
|
Func<ServiceEndpointContext, Task<object?>> unifiedHandler,
|
|
|
|
|
IServiceProvider serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
return async (HttpContext httpContext) =>
|
|
|
|
|
{
|
|
|
|
|
var ctx = await BuildContextFromHttpContext(httpContext);
|
|
|
|
|
ctx.Items["ServiceProvider"] = serviceProvider;
|
2026-05-15 17:35:07 +08:00
|
|
|
ctx.Items["User"] = httpContext.User;
|
2026-05-11 14:35:34 +08:00
|
|
|
|
|
|
|
|
var result = await unifiedHandler(ctx);
|
|
|
|
|
|
|
|
|
|
// 同步响应状态
|
|
|
|
|
httpContext.Response.StatusCode = ctx.StatusCode;
|
|
|
|
|
foreach (var kvp in ctx.ResponseHeaders)
|
|
|
|
|
{
|
|
|
|
|
httpContext.Response.Headers[kvp.Key] = kvp.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result is not null ? Results.Json(result) : Results.Ok();
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task<ServiceEndpointContext> BuildContextFromHttpContext(HttpContext httpContext)
|
|
|
|
|
{
|
|
|
|
|
var ctx = new ServiceEndpointContext
|
|
|
|
|
{
|
|
|
|
|
Path = httpContext.Request.Path.Value ?? "/",
|
|
|
|
|
Method = httpContext.Request.Method,
|
|
|
|
|
StatusCode = 200,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
foreach (var header in httpContext.Request.Headers)
|
|
|
|
|
{
|
|
|
|
|
ctx.Headers[header.Key] = header.Value.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var query in httpContext.Request.Query)
|
|
|
|
|
{
|
|
|
|
|
ctx.Query[query.Key] = query.Value.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (httpContext.Request.ContentLength > 0)
|
|
|
|
|
{
|
|
|
|
|
using var reader = new StreamReader(httpContext.Request.Body);
|
|
|
|
|
ctx.Body = await reader.ReadToEndAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.Items["HttpContext"] = httpContext;
|
2026-05-15 17:35:07 +08:00
|
|
|
ctx.Items["User"] = httpContext.User;
|
2026-05-11 14:35:34 +08:00
|
|
|
|
|
|
|
|
return ctx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async ValueTask<object?> ConvertFilterAsync(
|
|
|
|
|
UnifiedFilter unifiedFilter,
|
|
|
|
|
AspNetCoreFilterContext aspContext,
|
|
|
|
|
AspNetCoreFilterDelegate aspNext)
|
|
|
|
|
{
|
|
|
|
|
var httpContext = aspContext.HttpContext;
|
|
|
|
|
var ctx = httpContext.Items["UnifiedContext"] as ServiceEndpointContext
|
|
|
|
|
?? await BuildContextFromHttpContext(httpContext);
|
|
|
|
|
|
|
|
|
|
httpContext.Items["UnifiedContext"] = ctx;
|
|
|
|
|
|
|
|
|
|
await unifiedFilter.InvokeAsync(ctx, async (c) =>
|
|
|
|
|
{
|
|
|
|
|
httpContext.Response.StatusCode = c.StatusCode;
|
|
|
|
|
foreach (var kvp in c.ResponseHeaders)
|
|
|
|
|
{
|
|
|
|
|
httpContext.Response.Headers[kvp.Key] = kvp.Value;
|
|
|
|
|
}
|
|
|
|
|
await aspNext(aspContext);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (ctx.ResponseBody is not null)
|
|
|
|
|
{
|
|
|
|
|
return Results.Json(ctx.ResponseBody, statusCode: ctx.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null!;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|