优化结构

This commit is contained in:
Yvan
2026-08-21 08:54:29 +08:00
parent c968f9f24e
commit 77e192b6c0
167 changed files with 3657 additions and 1528 deletions
@@ -5,7 +5,7 @@ import (
"encoding/json"
"kra/app/system/internal/conf"
"kra/app/system/internal/utils/configutil"
"kra/pkg/protoutil"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
@@ -89,6 +89,16 @@ func (r *Repo) ConfigurationJSON() (json.RawMessage, error) {
if adminConfig.App != nil {
admin["app"] = adminConfig.App
}
if adminConfig.Websocket != nil {
admin["websocket"] = adminConfig.Websocket
}
if adminConfig.Mq != nil {
mqConfig := proto.Clone(adminConfig.Mq).(*conf.AdminBackend_MQ)
if mqConfig.Password != "" {
mqConfig.Password = "******"
}
admin["mq"] = mqConfig
}
}
// Never mask secrets on the live runtime object. ConfigurationJSON is a
// read-only operation; mutating dataConfig here would replace the actual
@@ -105,6 +115,9 @@ func (r *Repo) ConfigurationJSON() (json.RawMessage, error) {
safeAdmin.Email.Secret = "******"
}
maskStorageSecrets(safeAdmin.Storage)
if safeAdmin.Mq != nil && safeAdmin.Mq.Password != "" {
safeAdmin.Mq.Password = "******"
}
}
dataMap := map[string]any{}
if safeData != nil {
@@ -137,12 +150,12 @@ func (r *Repo) SaveConfigurationJSON(ctx context.Context, raw json.RawMessage) e
}
options := protojson.UnmarshalOptions{DiscardUnknown: true}
if len(value.Data) > 0 && string(value.Data) != "null" {
if err := configutil.MergeProtoJSON(nextData, value.Data, options); err != nil {
if err := protoutil.MergeProtoJSON(nextData, value.Data, options); err != nil {
return err
}
}
if len(value.Admin) > 0 && string(value.Admin) != "null" {
if err := configutil.MergeProtoJSON(nextAdmin, value.Admin, options); err != nil {
if err := protoutil.MergeProtoJSON(nextAdmin, value.Admin, options); err != nil {
return err
}
}
@@ -307,6 +320,9 @@ func preserveAdminSecrets(next, current *conf.AdminBackend) {
next.Email.Secret = current.Email.Secret
}
preserveStorageSecrets(next.Storage, current.Storage)
if next.Mq != nil && current.Mq != nil && maskedSecret(next.Mq.Password) {
next.Mq.Password = current.Mq.Password
}
}
func maskedSecret(value string) bool { return value == "" || value == "******" }
@@ -0,0 +1,29 @@
package initialize
import (
"encoding/json"
"testing"
"kra/app/system/internal/conf"
"kra/pkg/protoutil"
"google.golang.org/protobuf/encoding/protojson"
)
func TestMergeProtoJSONPreservesOmittedFieldsAndNormalizesKeys(t *testing.T) {
target := &conf.AdminBackend{
RouterPrefix: "/api",
System: &conf.AdminBackend_System{UseRedis: true, IplimitCount: 3},
}
patch := json.RawMessage(`{"router_prefix":"/admin","system":{"iplimit_count":9}}`)
if err := protoutil.MergeProtoJSON(target, patch, protojson.UnmarshalOptions{DiscardUnknown: true}); err != nil {
t.Fatal(err)
}
if target.RouterPrefix != "/admin" || target.System.IplimitCount != 9 {
t.Fatalf("patch was not applied: %#v", target)
}
if !target.System.UseRedis {
t.Fatal("an omitted nested field was cleared")
}
}