127 lines
4.8 KiB
Go
127 lines
4.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"kra/internal/conf"
|
|
"kra/internal/service/dto"
|
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
func (s *SystemService) PersistConfig(ctx context.Context) error { return s.uc.PersistConfig(ctx) }
|
|
|
|
func (s *SystemService) 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 *SystemService) ReloadConfig(ctx context.Context) error { return s.uc.ReloadConfig(ctx) }
|
|
|
|
func (s *SystemService) SystemConfig() map[string]any {
|
|
admin := map[string]any{"routerPrefix": ""}
|
|
email := map[string]any{}
|
|
if s.config == nil {
|
|
return map[string]any{"config": map[string]any{"admin": admin, "email": email}}
|
|
}
|
|
admin["routerPrefix"] = s.config.RouterPrefix
|
|
if s.config.Jwt != nil {
|
|
admin["jwt"] = map[string]any{"signingKey": "******", "expiresTime": s.config.Jwt.ExpiresTime.AsDuration().String(), "bufferTime": s.config.Jwt.BufferTime.AsDuration().String(), "issuer": s.config.Jwt.Issuer}
|
|
}
|
|
if s.config.Captcha != nil {
|
|
admin["captcha"] = map[string]any{"keyLong": s.config.Captcha.KeyLong, "imgWidth": s.config.Captcha.ImgWidth, "imgHeight": s.config.Captcha.ImgHeight, "storeExpiration": s.config.Captcha.StoreExpiration.AsDuration().String()}
|
|
}
|
|
if s.config.Local != nil {
|
|
admin["local"] = map[string]any{"storePath": s.config.Local.StorePath, "pathPrefix": s.config.Local.PathPrefix}
|
|
}
|
|
if s.config.Media != nil {
|
|
admin["media"] = map[string]any{"sessionTtl": s.config.Media.SessionTtl}
|
|
}
|
|
if s.config.Email != nil {
|
|
email = map[string]any{"to": s.config.Email.To, "from": s.config.Email.From, "host": s.config.Email.Host, "secret": "******", "nickname": s.config.Email.Nickname, "port": s.config.Email.Port, "is-ssl": s.config.Email.IsSsl, "is-loginauth": s.config.Email.IsLoginAuth}
|
|
}
|
|
if s.config.Storage != nil {
|
|
storage := proto.Clone(s.config.Storage).(*conf.AdminBackend_Storage)
|
|
maskStorageSecrets(storage)
|
|
admin["storage"] = storage
|
|
}
|
|
return map[string]any{"config": map[string]any{"admin": admin, "email": email}}
|
|
}
|
|
|
|
func (s *SystemService) SaveSystemConfig(ctx context.Context, req *dto.SetSystemConfigRequest) error {
|
|
if s.config == nil {
|
|
return nil
|
|
}
|
|
next := proto.Clone(s.config).(*conf.AdminBackend)
|
|
next.RouterPrefix = req.Config.Admin.RouterPrefix
|
|
if next.Jwt != nil {
|
|
if req.Config.Admin.JWT.SigningKey != "" && req.Config.Admin.JWT.SigningKey != "******" {
|
|
next.Jwt.SigningKey = req.Config.Admin.JWT.SigningKey
|
|
}
|
|
if req.Config.Admin.JWT.Issuer != "" {
|
|
next.Jwt.Issuer = req.Config.Admin.JWT.Issuer
|
|
}
|
|
}
|
|
if next.Local != nil {
|
|
if req.Config.Admin.Local.StorePath != "" {
|
|
next.Local.StorePath = req.Config.Admin.Local.StorePath
|
|
}
|
|
if req.Config.Admin.Local.PathPrefix != "" {
|
|
next.Local.PathPrefix = req.Config.Admin.Local.PathPrefix
|
|
}
|
|
}
|
|
if next.Media != nil && req.Config.Admin.Media.SessionTTL > 0 {
|
|
next.Media.SessionTtl = req.Config.Admin.Media.SessionTTL
|
|
}
|
|
if req.Config.Admin.Storage != nil {
|
|
preserveStorageSecrets(req.Config.Admin.Storage, next.Storage)
|
|
next.Storage = req.Config.Admin.Storage
|
|
}
|
|
if next.Email != nil && req.Config.Email != nil {
|
|
next.Email.To, next.Email.From, next.Email.Host = req.Config.Email.To, req.Config.Email.From, req.Config.Email.Host
|
|
next.Email.Nickname, next.Email.Port = req.Config.Email.Nickname, req.Config.Email.Port
|
|
next.Email.IsSsl, next.Email.IsLoginAuth = req.Config.Email.IsSSL, req.Config.Email.IsLoginAuth
|
|
if req.Config.Email.Secret != "" && req.Config.Email.Secret != "******" {
|
|
next.Email.Secret = req.Config.Email.Secret
|
|
}
|
|
}
|
|
return s.PersistAdminConfig(ctx, next)
|
|
}
|
|
|
|
func objectStores(storage *conf.AdminBackend_Storage) []*conf.AdminBackend_ObjectStore {
|
|
if storage == nil {
|
|
return nil
|
|
}
|
|
return []*conf.AdminBackend_ObjectStore{storage.AliyunOss, storage.HuaweiObs, storage.TencentCos, storage.AwsS3, storage.CloudflareR2, storage.Minio}
|
|
}
|
|
|
|
func maskStorageSecrets(storage *conf.AdminBackend_Storage) {
|
|
if storage.Qiniu != nil {
|
|
storage.Qiniu.SecretKey = "******"
|
|
}
|
|
for _, item := range objectStores(storage) {
|
|
if item != nil {
|
|
item.SecretKey = "******"
|
|
}
|
|
}
|
|
}
|
|
|
|
func preserveStorageSecrets(next, current *conf.AdminBackend_Storage) {
|
|
if next == nil || current == nil {
|
|
return
|
|
}
|
|
if next.Qiniu != nil && current.Qiniu != nil && (next.Qiniu.SecretKey == "" || next.Qiniu.SecretKey == "******") {
|
|
next.Qiniu.SecretKey = current.Qiniu.SecretKey
|
|
}
|
|
nextItems, currentItems := objectStores(next), objectStores(current)
|
|
for i := range nextItems {
|
|
if nextItems[i] != nil && currentItems[i] != nil && (nextItems[i].SecretKey == "" || nextItems[i].SecretKey == "******") {
|
|
nextItems[i].SecretKey = currentItems[i].SecretKey
|
|
}
|
|
}
|
|
}
|