using System.Reflection; using Fengling.Console.Data; using Fengling.Console.Services; using Fengling.Platform.Domain.AggregatesModel.UserAggregate; using Fengling.Platform.Domain.AggregatesModel.RoleAggregate; using Fengling.Platform.Infrastructure; using OpenIddict.Abstractions; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Identity; using Microsoft.IdentityModel.Tokens; using System.Text; using NetCorePal.Extensions.DependencyInjection; using OpenIddict.Validation.AspNetCore; using k8s; var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); // Use ConsoleDbContext for all identity builder.Services.AddDbContext(options => { options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")); if (builder.Environment.IsDevelopment()) { options.EnableSensitiveDataLogging(); } options.EnableDetailedErrors(); }); // Gateway 数据库上下文(用于管理网关配置表) builder.Services.AddDbContext(options => { options.UseNpgsql(builder.Configuration.GetConnectionString("GatewayConnection") ?? builder.Configuration.GetConnectionString("DefaultConnection")?.Replace("fengling_console", "fengling_gateway")); if (builder.Environment.IsDevelopment()) { options.EnableSensitiveDataLogging(); } options.EnableDetailedErrors(); }); // Use Platform's identity builder.Services.AddIdentity() .AddEntityFrameworkStores() .AddEntityFrameworkStores() .AddDefaultTokenProviders(); builder.Services.AddHttpContextAccessor(); builder.Services.AddHttpClient(); builder.Services.AddScoped(); // Register Platform managers builder.Services.AddScoped>(); builder.Services.AddScoped(); // Register Gateway managers builder.Services.AddScoped>(); builder.Services.AddScoped>(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddOpenIddict() .AddCore(options => { options.UseEntityFrameworkCore().UseDbContext(); }) .AddValidation(options => { options.SetIssuer("http://localhost:5132/"); options.UseIntrospection() .SetClientId("fengling-api") .SetClientSecret("fengling-api-secret"); options.UseSystemNetHttp(); options.UseAspNetCore(); }); builder.Services.AddAuthentication(options => { options.DefaultScheme = OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme; }); builder.Services.AddAuthorization(); builder.Services.AddCors(options => { options.AddPolicy("AllowAll", policy => { policy.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader(); }); }); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new() { Title = "Fengling.Console API", Version = "v1" }); c.CustomSchemaIds(type => type.FullName); }); // 添加通知服务 builder.Services.AddNotificationService(); // 添加 K8s 服务监视 builder.Services.AddServiceLabelParser(); builder.Services.AddPendingConfigCache(); builder.Services.AddK8sServiceWatch(); var app = builder.Build(); app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Fengling.Console API V1"); }); app.UseCors("AllowAll"); app.UseAuthentication(); app.UseAuthorization(); // 执行数据库初始化(Console 负责数据库管理,测试环境使用 EnsureCreated) using (var scope = app.Services.CreateScope()) { // 1. 初始化 Console 数据库 var consoleDb = scope.ServiceProvider.GetRequiredService(); try { app.Logger.LogInformation("Initializing Console database..."); await consoleDb.Database.EnsureCreatedAsync(); app.Logger.LogInformation("Console database initialized successfully"); } catch (Exception ex) { app.Logger.LogError(ex, "Failed to initialize Console database"); } // 2. 初始化 Gateway 数据库(创建 GwRoutes, GwClusters 等表) var gatewayDb = scope.ServiceProvider.GetRequiredService(); try { app.Logger.LogInformation("Deleting old Gateway database schema..."); await gatewayDb.Database.EnsureDeletedAsync(); // 临时:删除旧表结构 app.Logger.LogInformation("Initializing Gateway database..."); await gatewayDb.Database.EnsureCreatedAsync(); app.Logger.LogInformation("Gateway database initialized successfully"); } catch (Exception ex) { app.Logger.LogError(ex, "Failed to initialize Gateway database"); } } app.MapControllers(); // 健康检查端点 app.MapGet("/health", () => Results.Ok(new { status = "healthy", timestamp = DateTime.UtcNow })); app.Run();