using System; using System.Collections.Generic; using System.Threading.Tasks; namespace Avalonia_Services.Core { /// /// 单个端点定义。 /// public class ServiceEndpoint { /// 路由路径,如 "api/wData" public string Pattern { get; init; } = string.Empty; /// HTTP 方法(GET/POST/PUT/DELETE) public string HttpMethod { get; init; } = "GET"; /// 端点名称(用于 OpenAPI / 日志) public string? Name { get; set; } /// 端点处理器 public Func> Handler { get; init; } = _ => Task.FromResult(null); /// 该端点专属的过滤器(按顺序执行) public List Filters { get; init; } = new(); /// 是否需要鉴权 public bool RequireAuthorization { get; set; } /// 鉴权策略名 public string? Policy { get; set; } /// /// 设置端点名称(Fluent API)。 /// public ServiceEndpoint WithName(string name) { Name = name; return this; } } /// /// 端点集合 —— 所有端点的注册中心。在 Avalonia-Services 中统一配置。 /// public class ServiceEndpointCollection { /// 所有已注册的端点 public List Endpoints { get; } = new(); /// 作用于所有端点的全局过滤器 public List GlobalFilters { get; } = new(); /// /// 注册一个端点。 /// public ServiceEndpoint MapGet(string pattern, Func> handler) { return AddEndpoint(pattern, "GET", handler); } /// /// 注册一个 POST 端点。 /// public ServiceEndpoint MapPost(string pattern, Func> handler) { return AddEndpoint(pattern, "POST", handler); } /// /// 注册一个 PUT 端点。 /// public ServiceEndpoint MapPut(string pattern, Func> handler) { return AddEndpoint(pattern, "PUT", handler); } /// /// 注册一个 DELETE 端点。 /// public ServiceEndpoint MapDelete(string pattern, Func> handler) { return AddEndpoint(pattern, "DELETE", handler); } /// /// 添加全局过滤器(作用于所有端点)。 /// public ServiceEndpointCollection AddGlobalFilter(IEndpointFilter filter) { GlobalFilters.Add(filter); return this; } /// /// 通过匿名函数添加全局过滤器。 /// public ServiceEndpointCollection AddGlobalFilter(Func filter) { GlobalFilters.Add(new AnonymousEndpointFilter(filter)); return this; } private ServiceEndpoint AddEndpoint(string pattern, string method, Func> handler) { var endpoint = new ServiceEndpoint { Pattern = pattern, HttpMethod = method, Handler = handler, }; Endpoints.Add(endpoint); return endpoint; } } /// /// 构建器 —— 提供 Fluent API 来配置所有端点。 /// public class ServiceEndpointBuilder { /// /// 端点集合 /// public ServiceEndpointCollection Endpoints { get; } = new(); /// /// 鉴权服务(默认匿名) /// public IAuthService AuthService { get; set; } = new AnonymousAuthService(); /// /// 配置端点(在此方法中调用 endpoints.MapGet 等)。 /// public ServiceEndpointBuilder ConfigureEndpoints(Action configure) { configure(Endpoints); return this; } /// /// 设置鉴权服务。 /// public ServiceEndpointBuilder UseAuthService(IAuthService authService) { AuthService = authService; return this; } /// /// 构建最终的端点集合。 /// public ServiceEndpointCollection Build() { return Endpoints; } } }