重构项目结构,引入 Avalonia-Common、Avalonia-EFCore、Avalonia-Services,实现 API 与桌面端统一端点注册、过滤器、鉴权和标准响应格式。支持多数据库自动迁移与配置,集成 Serilog 日志系统。移除旧路由与控制器,提升接口一致性与可维护性。
65 lines
2.5 KiB
C#
65 lines
2.5 KiB
C#
using System;
|
|
using System.Linq;
|
|
|
|
namespace Avalonia_Services.Core
|
|
{
|
|
/// <summary>
|
|
/// 端点列表打印工具 —— 在应用启动时输出所有已注册的拦截接口。
|
|
/// 类似 Swagger 的接口清单效果。
|
|
/// </summary>
|
|
public static class EndpointPrinter
|
|
{
|
|
/// <summary>
|
|
/// 打印所有已注册端点到控制台。
|
|
/// </summary>
|
|
public static void PrintEndpoints(ServiceEndpointCollection collection, string? title = null)
|
|
{
|
|
title ??= "API Endpoints";
|
|
|
|
var maxMethodLen = collection.Endpoints.Count > 0
|
|
? collection.Endpoints.Max(e => e.HttpMethod.Length)
|
|
: 4;
|
|
var maxPathLen = collection.Endpoints.Count > 0
|
|
? collection.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 collection.Endpoints.OrderBy(e => e.Pattern))
|
|
{
|
|
var auth = ep.RequireAuthorization ? (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: {collection.Endpoints.Count} endpoint(s)");
|
|
Console.WriteLine();
|
|
}
|
|
}
|
|
}
|