125 lines
4.6 KiB
C#
125 lines
4.6 KiB
C#
using iFileProxy.Config;
|
||
using iFileProxy.Middleware;
|
||
using iFileProxy.Helpers;
|
||
using iFileProxy.Services;
|
||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||
using Microsoft.IdentityModel.Tokens;
|
||
using Serilog;
|
||
using System.Text;
|
||
|
||
namespace iFileProxy
|
||
{
|
||
public class Program
|
||
{
|
||
public static void Main(string[] args)
|
||
{
|
||
SerilogConfig.CreateLogger();
|
||
Serilog.ILogger logger = Log.Logger.ForContext<Program>();
|
||
|
||
Console.Write(" "); // 补全日志第一行开头的空白
|
||
|
||
var builder = WebApplication.CreateBuilder(args);
|
||
|
||
// CORS配置
|
||
builder.Services.AddCors(options =>
|
||
{
|
||
options.AddPolicy("AllowFrontend",
|
||
builder =>
|
||
{
|
||
builder
|
||
.WithOrigins("http://localhost:3000", "http://admin.gitdl.cn", "https://admin.gitdl.cn", "https://github.linxi.info", "http://github.linxi.info/", "http://localhost:4173")
|
||
.AllowAnyMethod()
|
||
.AllowAnyHeader()
|
||
.AllowCredentials();
|
||
});
|
||
});
|
||
|
||
// Add services to the container
|
||
builder.Services.AddControllers();
|
||
builder.Services.AddEndpointsApiExplorer();
|
||
builder.Services.AddSwaggerGen();
|
||
|
||
builder.Host.UseSerilog(logger: Log.Logger);
|
||
|
||
// 注入依赖
|
||
builder.Services.AddSingleton(AppConfig.GetCurrConfig());
|
||
builder.Services.AddSingleton<DatabaseGateService>();
|
||
builder.Services.AddSingleton<TaskManager>();
|
||
builder.Services.AddSingleton<Dictionary<string, Dictionary<string, uint>>>();
|
||
|
||
// 添加验证服务
|
||
builder.Services.AddScoped<AuthService>();
|
||
|
||
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||
.AddJwtBearer(options =>
|
||
{
|
||
options.TokenValidationParameters = new TokenValidationParameters
|
||
{
|
||
ValidateIssuer = true,
|
||
ValidateAudience = true,
|
||
ValidateLifetime = true,
|
||
ValidateIssuerSigningKey = true,
|
||
ValidIssuer = builder.Configuration["Jwt:Issuer"],
|
||
ValidAudience = builder.Configuration["Jwt:Audience"],
|
||
IssuerSigningKey = new SymmetricSecurityKey(
|
||
Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]))
|
||
};
|
||
});
|
||
|
||
builder.Services.AddHttpClient();
|
||
|
||
var app = builder.Build();
|
||
// 初始化缓存管理服务
|
||
LocalCacheManager localCacheManager = new(app.Services);
|
||
|
||
// 1. 错误处理(放在请求管道的最前面)
|
||
app.UseMiddleware<ErrorHandlerMiddleware>();
|
||
|
||
// 2. 配置CORS(要在Routing之前)
|
||
app.UseCors("AllowFrontend");
|
||
|
||
// 3. 配置静态文件(要在Routing之前)
|
||
var defaultFilesOptions = new DefaultFilesOptions();
|
||
defaultFilesOptions.DefaultFileNames.Clear();
|
||
defaultFilesOptions.DefaultFileNames.Add("index.html");
|
||
app.UseDefaultFiles(defaultFilesOptions);
|
||
app.UseStaticFiles();
|
||
|
||
// 4. 强制使用HTTPS(要在Routing之前)
|
||
app.UseHttpsRedirection();
|
||
|
||
// 5. 请求日志记录(在Routing之前)
|
||
app.UseSerilogRequestLogging(options =>
|
||
{
|
||
options.EnrichDiagnosticContext = (diagCtx, httpCtx) =>
|
||
{
|
||
diagCtx.Set("ClientIp", MasterHelper.GetClientIPAddr(httpCtx));
|
||
diagCtx.Set("contentType", httpCtx.Request.ContentType);
|
||
diagCtx.Set("queryString", httpCtx.Request.QueryString);
|
||
};
|
||
});
|
||
|
||
// 6. 路由配置
|
||
app.UseRouting();
|
||
|
||
// 7. 限制访问(IP访问限制,通常在认证之前)
|
||
app.UseMiddleware<IPAccessLimitMiddleware>();
|
||
|
||
// 8. 身份验证与授权
|
||
app.UseAuthentication();
|
||
app.UseAuthorization();
|
||
|
||
// 9. 配置JWT验证
|
||
app.UseMiddleware<JwtMiddleware>();
|
||
|
||
// 10. 映射控制器
|
||
app.MapControllers();
|
||
|
||
// 11. 启动应用
|
||
var dbGateService = app.Services.GetRequiredService<DatabaseGateService>();
|
||
SerilogConfig.CreateLogger(dbGateService);
|
||
|
||
app.Run();
|
||
}
|
||
}
|
||
}
|