重构项目结构,引入 Avalonia-Common、Avalonia-EFCore、Avalonia-Services,实现 API 与桌面端统一端点注册、过滤器、鉴权和标准响应格式。支持多数据库自动迁移与配置,集成 Serilog 日志系统。移除旧路由与控制器,提升接口一致性与可维护性。
58 lines
1.5 KiB
C#
58 lines
1.5 KiB
C#
using Avalonia_API.Configuration;
|
||
using Avalonia_API.Extensions;
|
||
using Avalonia_Common.Infrastructure;
|
||
using Avalonia_EFCore.Database;
|
||
using Avalonia_Services.Core;
|
||
using Avalonia_Services.Database;
|
||
using Serilog;
|
||
|
||
// 初始化日志系统
|
||
Log.Logger = LoggingConfiguration.CreateDefaultLogger(logDir: "logs");
|
||
Log.Information("Avalonia-API 正在启动...");
|
||
|
||
try
|
||
{
|
||
var builder = WebApplication.CreateBuilder(args);
|
||
|
||
// 使用 Serilog 作为日志提供程序
|
||
builder.Host.UseSerilog();
|
||
|
||
// Add services to the container.
|
||
builder.Services.AddControllers();
|
||
builder.Services.AddOpenApi();
|
||
|
||
// 注册统一端点及业务服务(入口在 Avalonia-Services/Endpoints/AppEndpoints.cs)
|
||
builder.Services.AddUnifiedApiServices();
|
||
|
||
var app = builder.Build();
|
||
|
||
// 初始化数据库(自动迁移 + 种子数据)
|
||
app.Services.InitializeDatabase<AppDataContext>();
|
||
|
||
// 启动时打印所有接口
|
||
var endpoints = app.Services.GetRequiredService<ServiceEndpointCollection>();
|
||
EndpointPrinter.PrintEndpoints(endpoints, "Avalonia-API 接口列表");
|
||
|
||
// Configure the HTTP request pipeline.
|
||
if (app.Environment.IsDevelopment())
|
||
{
|
||
app.MapOpenApi();
|
||
}
|
||
|
||
app.UseHttpsRedirection();
|
||
app.UseAuthorization();
|
||
|
||
// 将统一端点映射到 ASP.NET Core 路由
|
||
app.MapUnifiedEndpoints(endpoints, app.Services);
|
||
|
||
app.Run();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log.Fatal(ex, "Avalonia-API 启动失败");
|
||
}
|
||
finally
|
||
{
|
||
Log.CloseAndFlush();
|
||
}
|