优化结构

This commit is contained in:
Yvan
2026-08-24 13:25:16 +08:00
parent 7a9a738c6a
commit 0b6b8ddf1a
72 changed files with 2475 additions and 1443 deletions
+36 -38
View File
@@ -7,11 +7,9 @@ import (
"fmt"
"strings"
"kra/internal/conf"
"kra/internal/config"
dataintegration "kra/internal/data/integration"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"gorm.io/gorm"
)
@@ -39,24 +37,24 @@ func normalizeStorageType(value string) string {
return value
}
func storageProviderMessage(storage *conf.AdminBackend_Storage, provider string) proto.Message {
func storageProviderValue(storage *config.Storage, provider string) any {
if storage == nil {
storage = &conf.AdminBackend_Storage{}
storage = &config.Storage{}
}
switch provider {
case "qiniu":
if storage.Qiniu == nil {
storage.Qiniu = &conf.AdminBackend_Qiniu{}
storage.Qiniu = &config.Qiniu{}
}
return storage.Qiniu
case "aliyun-oss":
return ensureObjectStore(&storage.AliyunOss)
return ensureObjectStore(&storage.AliyunOSS)
case "huawei-obs":
return ensureObjectStore(&storage.HuaweiObs)
return ensureObjectStore(&storage.HuaweiOBS)
case "tencent-cos":
return ensureObjectStore(&storage.TencentCos)
return ensureObjectStore(&storage.TencentCOS)
case "aws-s3":
return ensureObjectStore(&storage.AwsS3)
return ensureObjectStore(&storage.AWSS3)
case "cloudflare-r2":
return ensureObjectStore(&storage.CloudflareR2)
case "minio":
@@ -66,45 +64,45 @@ func storageProviderMessage(storage *conf.AdminBackend_Storage, provider string)
}
}
func ensureObjectStore(value **conf.AdminBackend_ObjectStore) proto.Message {
func ensureObjectStore(value **config.ObjectStore) *config.ObjectStore {
if *value == nil {
*value = &conf.AdminBackend_ObjectStore{}
*value = &config.ObjectStore{}
}
return *value
}
func marshalStorageProvider(storage *conf.AdminBackend_Storage, provider string) (string, error) {
message := storageProviderMessage(storage, provider)
if message == nil {
func marshalStorageProvider(storage *config.Storage, provider string) (string, error) {
value := storageProviderValue(storage, provider)
if value == nil {
return "{}", nil
}
raw, err := protojson.MarshalOptions{UseProtoNames: true, EmitDefaultValues: true}.Marshal(message)
raw, err := json.Marshal(value)
if err != nil {
return "", err
}
return string(raw), nil
}
func unmarshalStorageProvider(storage *conf.AdminBackend_Storage, provider, value string) error {
func unmarshalStorageProvider(storage *config.Storage, provider, value string) error {
if provider == "local" || strings.TrimSpace(value) == "" {
return nil
}
message := storageProviderMessage(storage, provider)
if message == nil {
target := storageProviderValue(storage, provider)
if target == nil {
return nil
}
if !json.Valid([]byte(value)) {
return fmt.Errorf("invalid %s integration configuration", provider)
}
if err := (protojson.UnmarshalOptions{DiscardUnknown: true}).Unmarshal([]byte(value), message); err != nil {
if err := json.Unmarshal([]byte(value), target); err != nil {
return fmt.Errorf("decode %s integration configuration: %w", provider, err)
}
return nil
}
func saveStorageIntegrationConfig(db *gorm.DB, storage *conf.AdminBackend_Storage) error {
func saveStorageIntegrationConfig(db *gorm.DB, storage *config.Storage) error {
if storage == nil {
storage = &conf.AdminBackend_Storage{}
storage = &config.Storage{}
}
active := normalizeStorageType(storage.Type)
known := false
@@ -145,7 +143,7 @@ func saveStorageIntegrationConfig(db *gorm.DB, storage *conf.AdminBackend_Storag
})
}
func loadStorageIntegrationConfig(db *gorm.DB) (*conf.AdminBackend_Storage, bool, error) {
func loadStorageIntegrationConfig(db *gorm.DB) (*config.Storage, bool, error) {
var rows []dataintegration.ConfigPO
err := db.Session(&gorm.Session{NewDB: true}).
Where("kind = ?", integrationKindStorage).
@@ -158,7 +156,7 @@ func loadStorageIntegrationConfig(db *gorm.DB) (*conf.AdminBackend_Storage, bool
return nil, false, nil
}
storage := &conf.AdminBackend_Storage{Type: "local"}
storage := &config.Storage{Type: "local"}
for _, row := range rows {
if err = unmarshalStorageProvider(storage, row.Provider, row.Config); err != nil {
return nil, false, err
@@ -173,13 +171,13 @@ func loadStorageIntegrationConfig(db *gorm.DB) (*conf.AdminBackend_Storage, bool
// resolveStorageIntegrationConfig upgrades a legacy YAML configuration only
// when the database has no storage rows yet. From then on the database is the
// sole source of truth.
func resolveStorageIntegrationConfig(db *gorm.DB, legacy *conf.AdminBackend_Storage) (*conf.AdminBackend_Storage, error) {
func resolveStorageIntegrationConfig(db *gorm.DB, legacy *config.Storage) (*config.Storage, error) {
clean := db.Session(&gorm.Session{NewDB: true})
if !clean.Migrator().HasTable(&dataintegration.ConfigPO{}) {
if legacy == nil {
return &conf.AdminBackend_Storage{Type: "local"}, nil
return &config.Storage{Type: "local"}, nil
}
return proto.Clone(legacy).(*conf.AdminBackend_Storage), nil
return config.CloneStorage(legacy), nil
}
storage, found, err := loadStorageIntegrationConfig(clean)
if err != nil {
@@ -189,7 +187,7 @@ func resolveStorageIntegrationConfig(db *gorm.DB, legacy *conf.AdminBackend_Stor
return storage, nil
}
if legacy == nil {
legacy = &conf.AdminBackend_Storage{Type: "local"}
legacy = &config.Storage{Type: "local"}
}
if err = saveStorageIntegrationConfig(clean, legacy); err != nil {
return nil, err
@@ -198,7 +196,7 @@ func resolveStorageIntegrationConfig(db *gorm.DB, legacy *conf.AdminBackend_Stor
return storage, err
}
func (d *Data) persistStorageIntegrationConfig(ctx context.Context, storage *conf.AdminBackend_Storage) error {
func (d *Data) persistStorageIntegrationConfig(ctx context.Context, storage *config.Storage) error {
if !d.databaseReady.Load() {
return errors.New("database is not initialized")
}
@@ -209,15 +207,15 @@ func (d *Data) persistStorageIntegrationConfig(ctx context.Context, storage *con
return saveStorageIntegrationConfig(db, storage)
}
func defaultEmailIntegrationConfig() *conf.AdminBackend_Email {
return &conf.AdminBackend_Email{Port: 465, IsSsl: true}
func defaultEmailIntegrationConfig() *config.Email {
return &config.Email{Port: 465, IsSSL: true}
}
func saveEmailIntegrationConfig(db *gorm.DB, email *conf.AdminBackend_Email) error {
func saveEmailIntegrationConfig(db *gorm.DB, email *config.Email) error {
if email == nil {
email = defaultEmailIntegrationConfig()
}
raw, err := protojson.MarshalOptions{UseProtoNames: true, EmitDefaultValues: true}.Marshal(email)
raw, err := json.Marshal(email)
if err != nil {
return fmt.Errorf("encode smtp integration configuration: %w", err)
}
@@ -237,7 +235,7 @@ func saveEmailIntegrationConfig(db *gorm.DB, email *conf.AdminBackend_Email) err
}
}
func loadEmailIntegrationConfig(db *gorm.DB) (*conf.AdminBackend_Email, bool, error) {
func loadEmailIntegrationConfig(db *gorm.DB) (*config.Email, bool, error) {
var row dataintegration.ConfigPO
err := db.Session(&gorm.Session{NewDB: true}).
Where("kind = ? AND provider = ?", integrationKindEmail, "smtp").
@@ -252,19 +250,19 @@ func loadEmailIntegrationConfig(db *gorm.DB) (*conf.AdminBackend_Email, bool, er
return nil, false, errors.New("invalid smtp integration configuration")
}
email := defaultEmailIntegrationConfig()
if err = (protojson.UnmarshalOptions{DiscardUnknown: true}).Unmarshal([]byte(row.Config), email); err != nil {
if err = json.Unmarshal([]byte(row.Config), email); err != nil {
return nil, false, fmt.Errorf("decode smtp integration configuration: %w", err)
}
return email, true, nil
}
func resolveEmailIntegrationConfig(db *gorm.DB, legacy *conf.AdminBackend_Email) (*conf.AdminBackend_Email, error) {
func resolveEmailIntegrationConfig(db *gorm.DB, legacy *config.Email) (*config.Email, error) {
clean := db.Session(&gorm.Session{NewDB: true})
if !clean.Migrator().HasTable(&dataintegration.ConfigPO{}) {
if legacy == nil {
return defaultEmailIntegrationConfig(), nil
}
return proto.Clone(legacy).(*conf.AdminBackend_Email), nil
return config.CloneEmail(legacy), nil
}
email, found, err := loadEmailIntegrationConfig(clean)
if err != nil {
@@ -283,7 +281,7 @@ func resolveEmailIntegrationConfig(db *gorm.DB, legacy *conf.AdminBackend_Email)
return email, err
}
func (d *Data) persistEmailIntegrationConfig(ctx context.Context, email *conf.AdminBackend_Email) error {
func (d *Data) persistEmailIntegrationConfig(ctx context.Context, email *config.Email) error {
if !d.databaseReady.Load() {
return errors.New("database is not initialized")
}