129 lines
4.2 KiB
Go
129 lines
4.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"kra/internal/conf"
|
|
"kra/internal/service/dto"
|
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
func (s *SystemConfigService) PersistConfig(ctx context.Context) error {
|
|
return s.uc.PersistConfig(ctx)
|
|
}
|
|
|
|
func (s *SystemConfigService) PersistAdminConfig(ctx context.Context, value *conf.AdminBackend) error {
|
|
raw, err := protojson.MarshalOptions{UseProtoNames: true}.Marshal(value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return s.uc.PersistAdminConfig(ctx, raw)
|
|
}
|
|
|
|
func (s *SystemConfigService) ReloadConfig(ctx context.Context) error { return s.uc.ReloadConfig(ctx) }
|
|
|
|
func (s *SystemConfigService) DiskMountPoints() []string {
|
|
config := s.runtime.Admin()
|
|
if config == nil {
|
|
return nil
|
|
}
|
|
points := make([]string, 0, len(config.DiskList))
|
|
for _, item := range config.DiskList {
|
|
if item != nil && item.MountPoint != "" {
|
|
points = append(points, item.MountPoint)
|
|
}
|
|
}
|
|
return points
|
|
}
|
|
|
|
func (s *SystemConfigService) SystemConfig() map[string]any {
|
|
admin := map[string]any{"routerPrefix": ""}
|
|
email := map[string]any{}
|
|
config := s.runtime.Admin()
|
|
if config == nil {
|
|
return map[string]any{"config": map[string]any{"admin": admin, "email": email, "data": map[string]any{}}}
|
|
}
|
|
admin["routerPrefix"] = config.RouterPrefix
|
|
if config.System != nil {
|
|
admin["system"] = map[string]any{"useRedis": config.System.UseRedis, "useMultipoint": config.System.UseMultipoint, "useStrictAuth": config.System.UseStrictAuth, "disableAutoMigrate": config.System.DisableAutoMigrate, "useMongo": config.System.UseMongo}
|
|
}
|
|
if config.Jwt != nil {
|
|
admin["jwt"] = map[string]any{"signingKey": "******", "expiresTime": config.Jwt.ExpiresTime.AsDuration().String(), "bufferTime": config.Jwt.BufferTime.AsDuration().String(), "issuer": config.Jwt.Issuer}
|
|
}
|
|
if config.Captcha != nil {
|
|
admin["captcha"] = map[string]any{"keyLong": config.Captcha.KeyLong, "imgWidth": config.Captcha.ImgWidth, "imgHeight": config.Captcha.ImgHeight, "storeExpiration": config.Captcha.StoreExpiration.AsDuration().String()}
|
|
}
|
|
if config.Local != nil {
|
|
admin["local"] = map[string]any{"storePath": config.Local.StorePath, "pathPrefix": config.Local.PathPrefix}
|
|
}
|
|
if config.Media != nil {
|
|
admin["media"] = map[string]any{"sessionTtl": config.Media.SessionTtl, "maxFileSize": config.Media.MaxFileSize}
|
|
}
|
|
if config.Email != nil {
|
|
email = map[string]any{"to": config.Email.To, "from": config.Email.From, "host": config.Email.Host, "secret": "******", "nickname": config.Email.Nickname, "port": config.Email.Port, "is-ssl": config.Email.IsSsl, "is-loginauth": config.Email.IsLoginAuth}
|
|
}
|
|
if config.Storage != nil {
|
|
storage := proto.Clone(config.Storage).(*conf.AdminBackend_Storage)
|
|
maskStorageSecrets(storage)
|
|
admin["storage"] = storage
|
|
}
|
|
if config.Zap != nil {
|
|
admin["zap"] = config.Zap
|
|
}
|
|
if config.Cors != nil {
|
|
admin["cors"] = config.Cors
|
|
}
|
|
if config.App != nil {
|
|
admin["app"] = config.App
|
|
}
|
|
data := s.runtime.Data()
|
|
if data != nil {
|
|
if data.Database != nil {
|
|
data.Database.Password = "******"
|
|
}
|
|
if data.Redis != nil {
|
|
data.Redis.Password = "******"
|
|
}
|
|
if data.Mongo != nil {
|
|
data.Mongo.Password = "******"
|
|
}
|
|
for _, database := range data.DatabaseList {
|
|
if database != nil {
|
|
database.Password = "******"
|
|
}
|
|
}
|
|
for _, redis := range data.RedisList {
|
|
if redis != nil {
|
|
redis.Password = "******"
|
|
}
|
|
}
|
|
}
|
|
dataMap := map[string]any{}
|
|
if raw, err := (protojson.MarshalOptions{UseProtoNames: true}).Marshal(data); err == nil {
|
|
_ = json.Unmarshal(raw, &dataMap)
|
|
}
|
|
return map[string]any{"config": map[string]any{"admin": admin, "email": email, "data": dataMap}}
|
|
}
|
|
|
|
func (s *SystemConfigService) SaveSystemConfig(ctx context.Context, req *dto.SetSystemConfigRequest) error {
|
|
config := s.runtime.Admin()
|
|
if config == nil {
|
|
return nil
|
|
}
|
|
next := proto.Clone(config).(*conf.AdminBackend)
|
|
applyAdminConfig(next, req)
|
|
data := applyDataConfig(s.runtime.Data(), req)
|
|
dataRaw, err := protojson.MarshalOptions{UseProtoNames: true}.Marshal(data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
adminRaw, err := protojson.MarshalOptions{UseProtoNames: true}.Marshal(next)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return s.uc.PersistRuntimeConfig(ctx, dataRaw, adminRaw)
|
|
}
|