feat(ops-api/site-mappings): provision monitoring DB pool and readiness check

Open a separate pgx pool for the monitoring database, wire it into the
SiteDomainMappingService, and surface its health in /readyz. Point compose
at the dedicated monitoring-postgres service and align local/dev configs
with the per-instance ports. Also reformat the site_domain_mapping types
with gofmt while the files are touched.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-29 17:10:48 +08:00
parent 665405763c
commit bef55bd11f
9 changed files with 84 additions and 65 deletions
+2
View File
@@ -240,6 +240,8 @@ services:
depends_on: depends_on:
postgres: postgres:
condition: service_healthy condition: service_healthy
monitoring-postgres:
condition: service_healthy
migrate: migrate:
condition: service_completed_successfully condition: service_completed_successfully
ports: ports:
+1 -1
View File
@@ -13,7 +13,7 @@ database:
max_idle_conns: 2 max_idle_conns: 2
monitoring_database: monitoring_database:
host: postgres host: monitoring-postgres
port: 5432 port: 5432
user: geo user: geo
password: geo_dev password: geo_dev
+10
View File
@@ -44,9 +44,16 @@ func main() {
} }
defer pool.Close() defer pool.Close()
monitoringPool, err := postgres.NewPool(ctx, cfg.MonitoringDatabase)
if err != nil {
logger.Sugar().Fatalf("ops-api init monitoring postgres: %v", err)
}
defer monitoringPool.Close()
accountsRepo := repository.NewAccountRepository(pool) accountsRepo := repository.NewAccountRepository(pool)
adminUsersRepo := repository.NewAdminUserRepository(pool) adminUsersRepo := repository.NewAdminUserRepository(pool)
auditsRepo := repository.NewAuditRepository(pool) auditsRepo := repository.NewAuditRepository(pool)
siteDomainMappingsRepo := repository.NewSiteDomainMappingRepository(monitoringPool)
ipRegionResolver, err := app.NewIPRegionResolver(cfg.IPRegion.V4XDBPath, cfg.IPRegion.V6XDBPath, logger) ipRegionResolver, err := app.NewIPRegionResolver(cfg.IPRegion.V4XDBPath, cfg.IPRegion.V6XDBPath, logger)
if err != nil { if err != nil {
@@ -59,6 +66,7 @@ func main() {
authSvc := app.NewAuthService(accountsRepo, auditSvc, issuer) authSvc := app.NewAuthService(accountsRepo, auditSvc, issuer)
accountSvc := app.NewAccountService(accountsRepo, auditSvc) accountSvc := app.NewAccountService(accountsRepo, auditSvc)
adminUserSvc := app.NewAdminUserService(adminUsersRepo, auditSvc, cfg.AdminUsers.DefaultPlanCode) adminUserSvc := app.NewAdminUserService(adminUsersRepo, auditSvc, cfg.AdminUsers.DefaultPlanCode)
siteDomainMappingSvc := app.NewSiteDomainMappingService(siteDomainMappingsRepo, auditSvc)
if err := app.SeedDefaultAdmin(ctx, accountsRepo, app.DefaultAdminSeed{ if err := app.SeedDefaultAdmin(ctx, accountsRepo, app.DefaultAdminSeed{
Username: cfg.DefaultAdmin.Username, Username: cfg.DefaultAdmin.Username,
@@ -78,11 +86,13 @@ func main() {
Engine: engine, Engine: engine,
Logger: logger, Logger: logger,
DB: pool, DB: pool,
MonitoringDB: monitoringPool,
Issuer: issuer, Issuer: issuer,
Auth: authSvc, Auth: authSvc,
Accounts: accountSvc, Accounts: accountSvc,
AdminUsers: adminUserSvc, AdminUsers: adminUserSvc,
Audits: auditSvc, Audits: auditSvc,
SiteDomains: siteDomainMappingSvc,
}) })
addr := fmt.Sprintf(":%d", cfg.Server.Port) addr := fmt.Sprintf(":%d", cfg.Server.Port)
+1 -1
View File
@@ -14,7 +14,7 @@ database:
monitoring_database: monitoring_database:
host: localhost host: localhost
port: 5432 port: 5433
user: geo user: geo
password: geo_dev password: geo_dev
dbname: geo_monitoring dbname: geo_monitoring
+7
View File
@@ -14,6 +14,7 @@ type Deps struct {
Engine *gin.Engine Engine *gin.Engine
Logger *zap.Logger Logger *zap.Logger
DB *pgxpool.Pool DB *pgxpool.Pool
MonitoringDB *pgxpool.Pool
Issuer *app.TokenIssuer Issuer *app.TokenIssuer
Auth *app.AuthService Auth *app.AuthService
Accounts *app.AccountService Accounts *app.AccountService
@@ -38,6 +39,12 @@ func RegisterRoutes(d Deps) {
response.Error(c, response.ErrServiceUnavailable(50301, "postgres_unavailable", "数据库不可达")) response.Error(c, response.ErrServiceUnavailable(50301, "postgres_unavailable", "数据库不可达"))
return return
} }
if d.MonitoringDB != nil {
if err := d.MonitoringDB.Ping(c.Request.Context()); err != nil {
response.Error(c, response.ErrServiceUnavailable(50303, "monitoring_postgres_unavailable", "监控数据库不可达"))
return
}
}
response.Success(c, gin.H{"status": "ready"}) response.Success(c, gin.H{"status": "ready"})
}) })