using System; using System.Linq; namespace FileShare_Services.Core { /// /// 端点列表打印工具 —— 在应用启动时输出所有已注册的拦截接口。 /// 类似 Swagger 的接口清单效果。 /// public static class EndpointPrinter { /// /// 打印所有已注册端点到控制台。 /// public static void PrintEndpoints( ServiceEndpointCollection collection, string? title = null, EndpointHostTarget host = EndpointHostTarget.All) { title ??= "API Endpoints"; var endpoints = collection.ForHost(host).ToList(); var maxMethodLen = endpoints.Count > 0 ? endpoints.Max(e => e.HttpMethod.Length) : 4; var maxPathLen = endpoints.Count > 0 ? endpoints.Max(e => e.Pattern.Length) : 8; var totalWidth = maxMethodLen + maxPathLen + 5; var separator = new string('─', Math.Max(totalWidth, 50)); Console.WriteLine(); Console.WriteLine($"╔═ {title} ═{new string('═', Math.Max(0, totalWidth - title.Length - 3))}╗"); Console.WriteLine($"║ {"Method".PadRight(maxMethodLen)} │ {"Path".PadRight(maxPathLen)} │ Auth ║"); Console.WriteLine($"╟{separator}╢"); foreach (var ep in endpoints.OrderBy(e => e.Pattern)) { var auth = ep.RequireAuthorization ? (ep.Roles.Count > 0 ? string.Join(",", ep.Roles) : ep.Policy ?? "✓") : "—"; var methodColor = ep.HttpMethod switch { "GET" => ConsoleColor.Green, "POST" => ConsoleColor.Blue, "PUT" => ConsoleColor.Yellow, "DELETE" => ConsoleColor.Red, _ => ConsoleColor.Gray, }; var savedColor = Console.ForegroundColor; Console.Write("║ "); Console.ForegroundColor = methodColor; Console.Write(ep.HttpMethod.PadRight(maxMethodLen)); Console.ForegroundColor = savedColor; Console.Write(" │ "); Console.Write(ep.Pattern.PadRight(maxPathLen)); Console.Write(" │ "); Console.Write(auth.PadRight(4)); Console.WriteLine(" ║"); } Console.WriteLine($"╚{separator}╝"); Console.WriteLine($" Total: {endpoints.Count} endpoint(s)"); Console.WriteLine(); } } }