136 lines
4.6 KiB
C#
136 lines
4.6 KiB
C#
using iFileProxy.Config;
|
|
using iFileProxy.Helpers;
|
|
using iFileProxy.Middleware;
|
|
using iFileProxy.Services;
|
|
using Serilog;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
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","http://47.243.56.137:50050", "http://localhost:4173")
|
|
.AllowAnyMethod()
|
|
.AllowAnyHeader()
|
|
.AllowCredentials();
|
|
});
|
|
});
|
|
|
|
|
|
// Add services to the container.
|
|
|
|
builder.Services.AddControllers();
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
builder.Host.UseSerilog(logger: Log.Logger);
|
|
|
|
builder.Services.AddSingleton<Dictionary<string, Dictionary<string, uint>>>(serviceProvider =>
|
|
{
|
|
return new Dictionary<string, Dictionary<string, uint>>();
|
|
});
|
|
|
|
builder.Services.AddSingleton(AppConfig.GetCurrConfig());
|
|
|
|
builder.Services.AddSingleton<DatabaseGateService>();
|
|
|
|
builder.Services.AddSingleton<TaskManager>();
|
|
|
|
// 添加验证服务
|
|
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"]))
|
|
};
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
// 全局错误处理中间件
|
|
app.UseMiddleware<ErrorHandlerMiddleware>();
|
|
|
|
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);
|
|
};
|
|
});
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
// 检查自定义配置
|
|
AppConfig.CheckAppConfig(app.Services);
|
|
|
|
// 初始化缓存管理器
|
|
LocalCacheManager localCacheManager = new(app.Services);
|
|
|
|
|
|
app.UseCors("AllowFrontend");
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
|
|
var defaultFilesOptions = new DefaultFilesOptions();
|
|
defaultFilesOptions.DefaultFileNames.Clear();
|
|
defaultFilesOptions.DefaultFileNames.Add("index.html");
|
|
|
|
app.UseDefaultFiles(defaultFilesOptions);
|
|
|
|
app.UseStaticFiles();
|
|
|
|
app.UseAuthorization();
|
|
|
|
app.UseMiddleware<IPAccessLimitMiddleware>(
|
|
app.Services.GetRequiredService<Dictionary<string, Dictionary<string, uint>>>(),
|
|
AppConfig.GetCurrConfig().SecurityOptions.DailyRequestLimitPerIP);
|
|
|
|
// JWT中间件
|
|
app.UseMiddleware<JwtMiddleware>();
|
|
|
|
app.MapControllers();
|
|
|
|
var dbGateService = app.Services.GetRequiredService<DatabaseGateService>();
|
|
SerilogConfig.CreateLogger(dbGateService);
|
|
|
|
app.Run();
|
|
}
|
|
}
|
|
}
|