优化结构

This commit is contained in:
Yvan
2026-08-21 19:11:14 +08:00
parent 690a2d71ca
commit a2ce3ae218
22 changed files with 1292 additions and 734 deletions
+15 -11
View File
@@ -15,6 +15,7 @@ import (
datapayment "kra/internal/data/payment"
datasystem "kra/internal/data/repository"
"kra/internal/integration/storage"
"kra/internal/integrationruntime"
"kra/pkg/module"
)
@@ -41,6 +42,7 @@ type Data struct {
redis *reloadableRedis
mongo *reloadableMongo
runtime *conf.Runtime
integrations *integrationruntime.Store
storage *storage.Reloadable
dbListMu sync.RWMutex
dbList map[string]*gorm.DB
@@ -73,6 +75,15 @@ func (d *Data) Runtime() *conf.Runtime {
return d.runtime
}
// IntegrationRuntime exposes database-backed integration configuration to
// long-lived adapters without making config.yaml part of their lifecycle.
func (d *Data) IntegrationRuntime() *integrationruntime.Store {
if d == nil {
return nil
}
return d.integrations
}
// Database resolves the primary or a named database for repositories such as
// the system export module.
func (d *Data) Database(name string) (*gorm.DB, error) {
@@ -156,7 +167,7 @@ func NewData(runtime *conf.Runtime, appLogger *slog.Logger, storageManager *stor
// and /init/initdb remain available.
c.Database = &conf.Data_Database{}
}
d := &Data{runtime: runtime, appLogger: appLogger, storage: storageManager, catalog: catalog}
d := &Data{runtime: runtime, integrations: integrationruntime.NewStore(), appLogger: appLogger, storage: storageManager, catalog: catalog}
usingFallback := !databaseConnectionConfigured(c.Database)
var db *gorm.DB
var err error
@@ -208,17 +219,10 @@ func NewData(runtime *conf.Runtime, appLogger *slog.Logger, storageManager *stor
}
admin.Storage = storageConfig
admin.Email = emailConfig
websocketConfig, websocketErr := resolveWebSocketIntegrationConfig(db, admin.Websocket)
if websocketErr != nil {
return nil, nil, fmt.Errorf("load websocket integration configuration: %w", websocketErr)
}
admin.Websocket = websocketConfig
mqConfig, mqErr := resolveMQIntegrationConfig(db, admin.Mq)
if mqErr != nil {
return nil, nil, fmt.Errorf("load mq integration configuration: %w", mqErr)
}
admin.Mq = mqConfig
runtime.Replace(c, admin)
if err = d.loadIntegrationRuntime(db); err != nil {
return nil, nil, fmt.Errorf("load integration runtime: %w", err)
}
activeStorage, storageErr := storage.New(admin)
if storageErr != nil {
return nil, nil, fmt.Errorf("initialize storage: %w", storageErr)