All checks were successful
Docker Build & Deploy / Build Docker Image (push) Successful in 26s
Docker Build & Deploy / Deploy to Production (push) Successful in 19s
Docker Build & Deploy / Cleanup Dangling Images (push) Successful in 1s
Docker Build & Deploy / WeChat Notification (push) Successful in 1s
156 lines
4.4 KiB
C#
156 lines
4.4 KiB
C#
using FreeSql;
|
||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.AspNetCore.Mvc.Authorization;
|
||
using Microsoft.IdentityModel.Tokens;
|
||
using Scalar.AspNetCore;
|
||
using Serilog;
|
||
using Service.AppSettingModel;
|
||
using WebApi;
|
||
using Yitter.IdGenerator;
|
||
|
||
// 初始化雪花算法ID生成器
|
||
var options = new IdGeneratorOptions(1); // WorkerId 为 1,可根据实际部署情况调整
|
||
YitIdHelper.SetIdGenerator(options);
|
||
|
||
var builder = WebApplication.CreateBuilder(args);
|
||
|
||
// 配置 Serilog
|
||
builder.Host.UseSerilog((context, loggerConfig) =>
|
||
{
|
||
loggerConfig.ReadFrom.Configuration(context.Configuration);
|
||
});
|
||
|
||
// Add services to the container.
|
||
builder.Services.AddControllers(options =>
|
||
{
|
||
var policy = new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme)
|
||
.RequireAuthenticatedUser()
|
||
.Build();
|
||
|
||
options.Filters.Add(new AuthorizeFilter(policy));
|
||
});
|
||
builder.Services.AddOpenApi();
|
||
builder.Services.AddHttpClient();
|
||
|
||
// 配置 CORS
|
||
builder.Services.AddCors(options =>
|
||
{
|
||
options.AddDefaultPolicy(policy =>
|
||
{
|
||
policy.WithOrigins("http://localhost:5173")
|
||
.AllowAnyHeader()
|
||
.AllowAnyMethod();
|
||
});
|
||
});
|
||
|
||
// 绑定配置
|
||
builder.Services.Configure<EmailSettings>(builder.Configuration.GetSection("EmailSettings"));
|
||
builder.Services.Configure<AISettings>(builder.Configuration.GetSection("OpenAI"));
|
||
builder.Services.Configure<JwtSettings>(builder.Configuration.GetSection("JwtSettings"));
|
||
builder.Services.Configure<AuthSettings>(builder.Configuration.GetSection("AuthSettings"));
|
||
|
||
// 配置JWT认证
|
||
var jwtSettings = builder.Configuration.GetSection("JwtSettings");
|
||
var secretKey = jwtSettings["SecretKey"]!;
|
||
var key = Encoding.UTF8.GetBytes(secretKey);
|
||
|
||
builder.Services.AddAuthentication(options =>
|
||
{
|
||
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||
})
|
||
.AddJwtBearer(options =>
|
||
{
|
||
options.TokenValidationParameters = new TokenValidationParameters
|
||
{
|
||
ValidateIssuer = true,
|
||
ValidateAudience = true,
|
||
ValidateLifetime = true,
|
||
ValidateIssuerSigningKey = true,
|
||
ValidIssuer = jwtSettings["Issuer"],
|
||
ValidAudience = jwtSettings["Audience"],
|
||
IssuerSigningKey = new SymmetricSecurityKey(key),
|
||
ClockSkew = TimeSpan.Zero
|
||
};
|
||
options.Events = new JwtBearerEvents
|
||
{
|
||
OnChallenge = async context =>
|
||
{
|
||
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
|
||
context.Response.ContentType = "application/json";
|
||
await context.Response.WriteAsJsonAsync(BaseResponse.Fail("未登录"));
|
||
},
|
||
OnForbidden = async context =>
|
||
{
|
||
context.Response.StatusCode = StatusCodes.Status403Forbidden;
|
||
context.Response.ContentType = "application/json";
|
||
await context.Response.WriteAsJsonAsync(BaseResponse.Fail("权限不足"));
|
||
}
|
||
};
|
||
});
|
||
|
||
builder.Services.AddAuthorization();
|
||
|
||
// 配置 FreeSql + SQLite
|
||
var dbPath = Path.Combine(AppContext.BaseDirectory, "database");
|
||
if (!Directory.Exists(dbPath))
|
||
{
|
||
Directory.CreateDirectory(dbPath);
|
||
}
|
||
|
||
// 使用绝对路径作为数据库文件路径
|
||
var dbFilePath = Path.Combine(dbPath, "EmailBill.db");
|
||
var connectionString = $"Data Source={dbFilePath}";
|
||
Log.Information("数据库路径: {DbPath}", dbFilePath);
|
||
|
||
var fsql = new FreeSqlBuilder()
|
||
.UseConnectionString(DataType.Sqlite, connectionString)
|
||
.UseAutoSyncStructure(true)
|
||
.UseLazyLoading(true)
|
||
.UseMonitorCommand(
|
||
cmd =>
|
||
{
|
||
Log.Debug("执行SQL: {Sql}", cmd.CommandText);
|
||
}
|
||
)
|
||
.Build();
|
||
|
||
builder.Services.AddSingleton(fsql);
|
||
|
||
// 自动扫描注册服务和仓储
|
||
builder.Services.AddServices();
|
||
|
||
// 注册日志清理后台服务
|
||
builder.Services.AddHostedService<LogCleanupService>();
|
||
|
||
// 配置 Quartz.NET 定时任务
|
||
builder.AddScheduler();
|
||
|
||
var app = builder.Build();
|
||
|
||
// Configure the HTTP request pipeline.
|
||
if (app.Environment.IsDevelopment())
|
||
{
|
||
app.MapOpenApi();
|
||
app.MapScalarApiReference();
|
||
}
|
||
|
||
// 启用静态文件服务
|
||
app.UseDefaultFiles();
|
||
app.UseStaticFiles();
|
||
|
||
// 启用 CORS
|
||
app.UseCors();
|
||
|
||
// 启用认证和授权
|
||
app.UseAuthentication();
|
||
app.UseAuthorization();
|
||
|
||
app.MapControllers();
|
||
|
||
// 添加 SPA 回退路由(用于前端路由)
|
||
app.MapFallbackToFile("index.html");
|
||
|
||
app.Run();
|