|
| 1 | +package admin |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "reflect" |
| 6 | + "strconv" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/gin-gonic/gin" |
| 10 | + "github.com/openimsdk/chat/pkg/common/apistruct" |
| 11 | + "github.com/openimsdk/chat/pkg/common/config" |
| 12 | + "github.com/openimsdk/chat/pkg/common/kdisc" |
| 13 | + "github.com/openimsdk/chat/pkg/common/kdisc/etcd" |
| 14 | + "github.com/openimsdk/chat/version" |
| 15 | + "github.com/openimsdk/tools/apiresp" |
| 16 | + "github.com/openimsdk/tools/errs" |
| 17 | + "github.com/openimsdk/tools/log" |
| 18 | + "github.com/openimsdk/tools/utils/runtimeenv" |
| 19 | + clientv3 "go.etcd.io/etcd/client/v3" |
| 20 | +) |
| 21 | + |
| 22 | +type ConfigManager struct { |
| 23 | + config *config.AllConfig |
| 24 | + client *clientv3.Client |
| 25 | + configPath string |
| 26 | + runtimeEnv string |
| 27 | +} |
| 28 | + |
| 29 | +func NewConfigManager(cfg *config.AllConfig, client *clientv3.Client, configPath string, runtimeEnv string) *ConfigManager { |
| 30 | + return &ConfigManager{ |
| 31 | + config: cfg, |
| 32 | + client: client, |
| 33 | + configPath: configPath, |
| 34 | + runtimeEnv: runtimeEnv, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func (cm *ConfigManager) GetConfig(c *gin.Context) { |
| 39 | + var req apistruct.GetConfigReq |
| 40 | + if err := c.BindJSON(&req); err != nil { |
| 41 | + apiresp.GinError(c, errs.ErrArgs.WithDetail(err.Error()).Wrap()) |
| 42 | + return |
| 43 | + } |
| 44 | + conf := cm.config.Name2Config(req.ConfigName) |
| 45 | + if conf == nil { |
| 46 | + apiresp.GinError(c, errs.ErrArgs.WithDetail("config name not found").Wrap()) |
| 47 | + return |
| 48 | + } |
| 49 | + b, err := json.Marshal(conf) |
| 50 | + if err != nil { |
| 51 | + apiresp.GinError(c, err) |
| 52 | + return |
| 53 | + } |
| 54 | + apiresp.GinSuccess(c, string(b)) |
| 55 | +} |
| 56 | + |
| 57 | +func (cm *ConfigManager) GetConfigList(c *gin.Context) { |
| 58 | + var resp apistruct.GetConfigListResp |
| 59 | + resp.ConfigNames = cm.config.GetConfigNames() |
| 60 | + resp.Environment = runtimeenv.PrintRuntimeEnvironment() |
| 61 | + resp.Version = version.Version |
| 62 | + |
| 63 | + apiresp.GinSuccess(c, resp) |
| 64 | +} |
| 65 | + |
| 66 | +func (cm *ConfigManager) SetConfig(c *gin.Context) { |
| 67 | + if cm.config.Discovery.Enable != kdisc.ETCDCONST { |
| 68 | + apiresp.GinError(c, errs.New("only etcd support set config").Wrap()) |
| 69 | + return |
| 70 | + } |
| 71 | + var req apistruct.SetConfigReq |
| 72 | + if err := c.BindJSON(&req); err != nil { |
| 73 | + apiresp.GinError(c, errs.ErrArgs.WithDetail(err.Error()).Wrap()) |
| 74 | + return |
| 75 | + } |
| 76 | + var err error |
| 77 | + switch req.ConfigName { |
| 78 | + case config.DiscoveryConfigFileName: |
| 79 | + err = compareAndSave[config.Discovery](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 80 | + case config.LogConfigFileName: |
| 81 | + err = compareAndSave[config.Log](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 82 | + case config.MongodbConfigFileName: |
| 83 | + err = compareAndSave[config.Mongo](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 84 | + case config.ChatAPIAdminCfgFileName: |
| 85 | + err = compareAndSave[config.API](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 86 | + case config.ChatAPIChatCfgFileName: |
| 87 | + err = compareAndSave[config.API](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 88 | + case config.ChatRPCAdminCfgFileName: |
| 89 | + err = compareAndSave[config.Admin](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 90 | + case config.ChatRPCChatCfgFileName: |
| 91 | + err = compareAndSave[config.Chat](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 92 | + case config.ShareFileName: |
| 93 | + err = compareAndSave[config.Share](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 94 | + case config.RedisConfigFileName: |
| 95 | + err = compareAndSave[config.Redis](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 96 | + default: |
| 97 | + apiresp.GinError(c, errs.ErrArgs.Wrap()) |
| 98 | + return |
| 99 | + } |
| 100 | + if err != nil { |
| 101 | + apiresp.GinError(c, errs.ErrArgs.WithDetail(err.Error()).Wrap()) |
| 102 | + return |
| 103 | + } |
| 104 | + apiresp.GinSuccess(c, nil) |
| 105 | +} |
| 106 | + |
| 107 | +func compareAndSave[T any](c *gin.Context, old any, req *apistruct.SetConfigReq, client *clientv3.Client) error { |
| 108 | + conf := new(T) |
| 109 | + err := json.Unmarshal([]byte(req.Data), &conf) |
| 110 | + if err != nil { |
| 111 | + return errs.ErrArgs.WithDetail(err.Error()).Wrap() |
| 112 | + } |
| 113 | + eq := reflect.DeepEqual(old, conf) |
| 114 | + if eq { |
| 115 | + return nil |
| 116 | + } |
| 117 | + data, err := json.Marshal(conf) |
| 118 | + if err != nil { |
| 119 | + return errs.ErrArgs.WithDetail(err.Error()).Wrap() |
| 120 | + } |
| 121 | + _, err = client.Put(c, etcd.BuildKey(req.ConfigName), string(data)) |
| 122 | + if err != nil { |
| 123 | + return errs.WrapMsg(err, "save to etcd failed") |
| 124 | + } |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +func (cm *ConfigManager) ResetConfig(c *gin.Context) { |
| 129 | + go cm.resetConfig(c) |
| 130 | + apiresp.GinSuccess(c, nil) |
| 131 | +} |
| 132 | + |
| 133 | +func (cm *ConfigManager) resetConfig(c *gin.Context) { |
| 134 | + txn := cm.client.Txn(c) |
| 135 | + type initConf struct { |
| 136 | + old any |
| 137 | + new any |
| 138 | + isChanged bool |
| 139 | + } |
| 140 | + configMap := map[string]*initConf{ |
| 141 | + config.DiscoveryConfigFileName: {old: &cm.config.Discovery, new: new(config.Discovery)}, |
| 142 | + config.LogConfigFileName: {old: &cm.config.Log, new: new(config.Log)}, |
| 143 | + config.MongodbConfigFileName: {old: &cm.config.Mongo, new: new(config.Mongo)}, |
| 144 | + config.ChatAPIAdminCfgFileName: {old: &cm.config.AdminAPI, new: new(config.API)}, |
| 145 | + config.ChatAPIChatCfgFileName: {old: &cm.config.ChatAPI, new: new(config.API)}, |
| 146 | + config.ChatRPCAdminCfgFileName: {old: &cm.config.Admin, new: new(config.Admin)}, |
| 147 | + config.ChatRPCChatCfgFileName: {old: &cm.config.Chat, new: new(config.Chat)}, |
| 148 | + config.RedisConfigFileName: {old: &cm.config.Redis, new: new(config.Redis)}, |
| 149 | + config.ShareFileName: {old: &cm.config.Share, new: new(config.Share)}, |
| 150 | + } |
| 151 | + |
| 152 | + changedKeys := make([]string, 0, len(configMap)) |
| 153 | + for k, v := range configMap { |
| 154 | + err := config.Load( |
| 155 | + cm.configPath, |
| 156 | + k, |
| 157 | + config.EnvPrefixMap[k], |
| 158 | + cm.runtimeEnv, |
| 159 | + v.new, |
| 160 | + ) |
| 161 | + if err != nil { |
| 162 | + log.ZError(c, "load config failed", err) |
| 163 | + continue |
| 164 | + } |
| 165 | + v.isChanged = reflect.DeepEqual(v.old, v.new) |
| 166 | + if !v.isChanged { |
| 167 | + changedKeys = append(changedKeys, k) |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + ops := make([]clientv3.Op, 0) |
| 172 | + for _, k := range changedKeys { |
| 173 | + data, err := json.Marshal(configMap[k].new) |
| 174 | + if err != nil { |
| 175 | + log.ZError(c, "marshal config failed", err) |
| 176 | + continue |
| 177 | + } |
| 178 | + ops = append(ops, clientv3.OpPut(etcd.BuildKey(k), string(data))) |
| 179 | + } |
| 180 | + if len(ops) > 0 { |
| 181 | + txn.Then(ops...) |
| 182 | + _, err := txn.Commit() |
| 183 | + if err != nil { |
| 184 | + log.ZError(c, "commit etcd txn failed", err) |
| 185 | + return |
| 186 | + } |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +func (cm *ConfigManager) Restart(c *gin.Context) { |
| 191 | + go cm.restart(c) |
| 192 | + apiresp.GinSuccess(c, nil) |
| 193 | +} |
| 194 | + |
| 195 | +func (cm *ConfigManager) restart(c *gin.Context) { |
| 196 | + time.Sleep(time.Millisecond * 200) // wait for Restart http call return |
| 197 | + t := time.Now().Unix() |
| 198 | + _, err := cm.client.Put(c, etcd.BuildKey(etcd.RestartKey), strconv.Itoa(int(t))) |
| 199 | + if err != nil { |
| 200 | + log.ZError(c, "restart etcd put key failed", err) |
| 201 | + } |
| 202 | +} |
0 commit comments