重构项目结构,引入 Avalonia-Common、Avalonia-EFCore、Avalonia-Services,实现 API 与桌面端统一端点注册、过滤器、鉴权和标准响应格式。支持多数据库自动迁移与配置,集成 Serilog 日志系统。移除旧路由与控制器,提升接口一致性与可维护性。
159 lines
5.0 KiB
C#
159 lines
5.0 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Avalonia_Services.Core
|
||
{
|
||
/// <summary>
|
||
/// 单个端点定义。
|
||
/// </summary>
|
||
public class ServiceEndpoint
|
||
{
|
||
/// <summary>路由路径,如 "api/wData"</summary>
|
||
public string Pattern { get; init; } = string.Empty;
|
||
|
||
/// <summary>HTTP 方法(GET/POST/PUT/DELETE)</summary>
|
||
public string HttpMethod { get; init; } = "GET";
|
||
|
||
/// <summary>端点名称(用于 OpenAPI / 日志)</summary>
|
||
public string? Name { get; set; }
|
||
|
||
/// <summary>端点处理器</summary>
|
||
public Func<ServiceEndpointContext, Task<object?>> Handler { get; init; } = _ => Task.FromResult<object?>(null);
|
||
|
||
/// <summary>该端点专属的过滤器(按顺序执行)</summary>
|
||
public List<IEndpointFilter> Filters { get; init; } = new();
|
||
|
||
/// <summary>是否需要鉴权</summary>
|
||
public bool RequireAuthorization { get; set; }
|
||
|
||
/// <summary>鉴权策略名</summary>
|
||
public string? Policy { get; set; }
|
||
|
||
/// <summary>
|
||
/// 设置端点名称(Fluent API)。
|
||
/// </summary>
|
||
public ServiceEndpoint WithName(string name)
|
||
{
|
||
Name = name;
|
||
return this;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 端点集合 —— 所有端点的注册中心。在 Avalonia-Services 中统一配置。
|
||
/// </summary>
|
||
public class ServiceEndpointCollection
|
||
{
|
||
/// <summary>所有已注册的端点</summary>
|
||
public List<ServiceEndpoint> Endpoints { get; } = new();
|
||
|
||
/// <summary>作用于所有端点的全局过滤器</summary>
|
||
public List<IEndpointFilter> GlobalFilters { get; } = new();
|
||
|
||
/// <summary>
|
||
/// 注册一个端点。
|
||
/// </summary>
|
||
public ServiceEndpoint MapGet(string pattern, Func<ServiceEndpointContext, Task<object?>> handler)
|
||
{
|
||
return AddEndpoint(pattern, "GET", handler);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 注册一个 POST 端点。
|
||
/// </summary>
|
||
public ServiceEndpoint MapPost(string pattern, Func<ServiceEndpointContext, Task<object?>> handler)
|
||
{
|
||
return AddEndpoint(pattern, "POST", handler);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 注册一个 PUT 端点。
|
||
/// </summary>
|
||
public ServiceEndpoint MapPut(string pattern, Func<ServiceEndpointContext, Task<object?>> handler)
|
||
{
|
||
return AddEndpoint(pattern, "PUT", handler);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 注册一个 DELETE 端点。
|
||
/// </summary>
|
||
public ServiceEndpoint MapDelete(string pattern, Func<ServiceEndpointContext, Task<object?>> handler)
|
||
{
|
||
return AddEndpoint(pattern, "DELETE", handler);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 添加全局过滤器(作用于所有端点)。
|
||
/// </summary>
|
||
public ServiceEndpointCollection AddGlobalFilter(IEndpointFilter filter)
|
||
{
|
||
GlobalFilters.Add(filter);
|
||
return this;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 通过匿名函数添加全局过滤器。
|
||
/// </summary>
|
||
public ServiceEndpointCollection AddGlobalFilter(Func<ServiceEndpointContext, EndpointFilterDelegate, Task> filter)
|
||
{
|
||
GlobalFilters.Add(new AnonymousEndpointFilter(filter));
|
||
return this;
|
||
}
|
||
|
||
private ServiceEndpoint AddEndpoint(string pattern, string method, Func<ServiceEndpointContext, Task<object?>> handler)
|
||
{
|
||
var endpoint = new ServiceEndpoint
|
||
{
|
||
Pattern = pattern,
|
||
HttpMethod = method,
|
||
Handler = handler,
|
||
};
|
||
Endpoints.Add(endpoint);
|
||
return endpoint;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 构建器 —— 提供 Fluent API 来配置所有端点。
|
||
/// </summary>
|
||
public class ServiceEndpointBuilder
|
||
{
|
||
/// <summary>
|
||
/// 端点集合
|
||
/// </summary>
|
||
public ServiceEndpointCollection Endpoints { get; } = new();
|
||
|
||
/// <summary>
|
||
/// 鉴权服务(默认匿名)
|
||
/// </summary>
|
||
public IAuthService AuthService { get; set; } = new AnonymousAuthService();
|
||
|
||
/// <summary>
|
||
/// 配置端点(在此方法中调用 endpoints.MapGet 等)。
|
||
/// </summary>
|
||
public ServiceEndpointBuilder ConfigureEndpoints(Action<ServiceEndpointCollection> configure)
|
||
{
|
||
configure(Endpoints);
|
||
return this;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置鉴权服务。
|
||
/// </summary>
|
||
public ServiceEndpointBuilder UseAuthService(IAuthService authService)
|
||
{
|
||
AuthService = authService;
|
||
return this;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 构建最终的端点集合。
|
||
/// </summary>
|
||
public ServiceEndpointCollection Build()
|
||
{
|
||
return Endpoints;
|
||
}
|
||
}
|
||
}
|