fengling-console/src/Program.cs
Kimi CLI 2f8c35ef3e
Some checks failed
Build and Push Docker / build (push) Failing after 2m40s
fix: update Platform packages to v1.0.17 and fix Gateway DB schema
- Update Fengling.Platform.Domain/Infrastructure from 1.0.14 to 1.0.17
- Add temporary EnsureDeletedAsync() to recreate Gateway DB tables
- Fix bigint/character varying type mismatch in GwCluster.Id
- Add Npgsql log level config to suppress GSSAPI warnings
- Add DATABASE_SCHEMA_FIX.md documentation

Refs: AGENTS.md Recent Changes 2026-03-08
2026-03-08 15:21:09 +08:00

167 lines
5.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<ConsoleDbContext>(options =>
{
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"));
if (builder.Environment.IsDevelopment())
{
options.EnableSensitiveDataLogging();
}
options.EnableDetailedErrors();
});
// Gateway 数据库上下文(用于管理网关配置表)
builder.Services.AddDbContext<PlatformDbContext>(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<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ConsoleDbContext>()
.AddEntityFrameworkStores<PlatformDbContext>()
.AddDefaultTokenProviders();
builder.Services.AddHttpContextAccessor();
builder.Services.AddHttpClient();
builder.Services.AddScoped<IOAuthClientService, OAuthClientService>();
// Register Platform managers
builder.Services.AddScoped<ITenantStore, TenantStore<ConsoleDbContext>>();
builder.Services.AddScoped<ITenantManager, TenantManager>();
// Register Gateway managers
builder.Services.AddScoped<IRouteStore, RouteStore<ConsoleDbContext>>();
builder.Services.AddScoped<IClusterStore, ClusterStore<PlatformDbContext>>();
builder.Services.AddScoped<IRouteManager, RouteManager>();
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<ITenantService, TenantService>();
builder.Services.AddScoped<IRoleService, RoleService>();
builder.Services.AddScoped<IGatewayService, GatewayService>();
builder.Services.AddScoped<IH5LinkService, H5LinkService>();
builder.Services.AddOpenIddict()
.AddCore(options => { options.UseEntityFrameworkCore().UseDbContext<ConsoleDbContext>(); })
.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<ConsoleDbContext>();
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<PlatformDbContext>();
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();