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);
|
|
|
|
|
|
|
2026-05-21 16:45:56 +08:00
|
|
|
|
// 配置 Kestrel 监听所有本机 IP
|
|
|
|
|
|
builder.WebHost.UseUrls("http://0.0.0.0:5206", "https://0.0.0.0:7165");
|
|
|
|
|
|
|
2026-05-21 15:52:36 +08:00
|
|
|
|
// 使用 Serilog 作为日志提供程序
|
|
|
|
|
|
builder.Host.UseSerilog();
|
|
|
|
|
|
|
|
|
|
|
|
// Add services to the container.
|
|
|
|
|
|
builder.Services.AddControllers();
|
|
|
|
|
|
builder.Services.AddOpenApi();
|
2026-05-21 16:45:56 +08:00
|
|
|
|
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";
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-21 16:45:56 +08:00
|
|
|
|
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);
|
2026-05-21 16:45:56 +08:00
|
|
|
|
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();
|
|
|
|
|
|
}
|