78 lines
2.4 KiB
C#
Raw Normal View History

2026-05-22 14:29:22 +08:00
using FileShare_API.Configuration;
using FileShare_API.Extensions;
using FileShare_Common.Infrastructure;
using FileShare_EFCore.Database;
using FileShare_Services.Core;
2026-05-21 15:52:36 +08:00
using Serilog;
// 初始化日志系统
Log.Logger = LoggingConfiguration.CreateDefaultLogger(logDir: "logs");
2026-05-22 14:29:22 +08:00
Log.Information("FileShare-API 正在启动...");
2026-05-21 15:52:36 +08:00
try
{
var builder = WebApplication.CreateBuilder(args);
// 配置 Kestrel 监听所有本机 IP端口从 Server 配置节读取)
var httpPort = builder.Configuration.GetValue<int>("Server:HttpPort", 5206);
var httpsPort = builder.Configuration.GetValue<int>("Server:HttpsPort", 7165);
builder.WebHost.UseUrls($"http://0.0.0.0:{httpPort}", $"https://0.0.0.0:{httpsPort}");
2026-05-21 15:52:36 +08:00
// 使用 Serilog 作为日志提供程序
builder.Host.UseSerilog();
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddOpenApi();
builder.Services.AddCors(options =>
{
options.AddPolicy("LanFileViewer", policy =>
policy.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod());
});
2026-05-21 15:52:36 +08:00
2026-05-22 14:29:22 +08:00
// 注册统一端点及业务服务(入口在 FileShare-Services/Endpoints/AppEndpoints.cs
2026-05-21 15:52:36 +08:00
builder.Services.AddUnifiedApiServices(builder.Configuration);
var app = builder.Build();
// 初始化数据库(自动迁移 + 种子数据)
app.Services.InitializeDatabase<AppDataContext>();
var endpoints = app.Services.GetRequiredService<ServiceEndpointCollection>();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.UseSwaggerUI(options =>
{
2026-05-22 14:29:22 +08:00
options.SwaggerEndpoint("/openapi/v1.json", "FileShare API v1");
2026-05-21 15:52:36 +08:00
options.RoutePrefix = "swagger";
});
}
app.UseDefaultFiles();
app.UseStaticFiles();
// 局域网文件播放优先使用 HTTP避免手机浏览器对自签 HTTPS/HTTP2 视频流的兼容问题。
app.UseCors("LanFileViewer");
2026-05-21 15:52:36 +08:00
app.UseAuthentication();
app.UseAuthorization();
// 将统一端点映射到 ASP.NET Core 路由
app.MapUnifiedEndpoints(endpoints, app.Services);
app.MapFileStreamEndpoints();
app.MapFallbackToFile("index.html");
2026-05-21 15:52:36 +08:00
app.Run();
}
catch (Exception ex)
{
2026-05-22 14:29:22 +08:00
Log.Fatal(ex, "FileShare-API 启动失败");
2026-05-21 15:52:36 +08:00
}
finally
{
Log.CloseAndFlush();
}