- 所有空 catch 块补全日志记录,统一使用 Serilog/AppLog - 按场景分级:Error(意外失败)、Warning(次要问题)、Information(预期内) - 端口 HttpPort/HttpsPort 抽离到 appsettings.json Server 配置节 - QrCodeService 通过 IConfiguration 读取端口,消除硬编码 - 前端通过 Vite proxy 转发 /api,http.ts 统一使用 origin 地址 - 移除所有 Debug.WriteLine 和 Serilog.Log.Debug 日志 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
78 lines
2.4 KiB
C#
78 lines
2.4 KiB
C#
using FileShare_API.Configuration;
|
||
using FileShare_API.Extensions;
|
||
using FileShare_Common.Infrastructure;
|
||
using FileShare_EFCore.Database;
|
||
using FileShare_Services.Core;
|
||
using Serilog;
|
||
|
||
// 初始化日志系统
|
||
Log.Logger = LoggingConfiguration.CreateDefaultLogger(logDir: "logs");
|
||
Log.Information("FileShare-API 正在启动...");
|
||
|
||
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}");
|
||
|
||
// 使用 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());
|
||
});
|
||
|
||
// 注册统一端点及业务服务(入口在 FileShare-Services/Endpoints/AppEndpoints.cs)
|
||
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 =>
|
||
{
|
||
options.SwaggerEndpoint("/openapi/v1.json", "FileShare API v1");
|
||
options.RoutePrefix = "swagger";
|
||
});
|
||
}
|
||
|
||
app.UseDefaultFiles();
|
||
app.UseStaticFiles();
|
||
// 局域网文件播放优先使用 HTTP,避免手机浏览器对自签 HTTPS/HTTP2 视频流的兼容问题。
|
||
app.UseCors("LanFileViewer");
|
||
app.UseAuthentication();
|
||
app.UseAuthorization();
|
||
|
||
// 将统一端点映射到 ASP.NET Core 路由
|
||
app.MapUnifiedEndpoints(endpoints, app.Services);
|
||
app.MapFileStreamEndpoints();
|
||
app.MapFallbackToFile("index.html");
|
||
|
||
app.Run();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log.Fatal(ex, "FileShare-API 启动失败");
|
||
}
|
||
finally
|
||
{
|
||
Log.CloseAndFlush();
|
||
}
|