|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "reflect" |
| 6 | + "strconv" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/gin-gonic/gin" |
| 10 | + "github.com/openimsdk/open-im-server/v3/pkg/apistruct" |
| 11 | + "github.com/openimsdk/open-im-server/v3/pkg/authverify" |
| 12 | + "github.com/openimsdk/open-im-server/v3/pkg/common/config" |
| 13 | + "github.com/openimsdk/open-im-server/v3/pkg/common/discovery/etcd" |
| 14 | + "github.com/openimsdk/open-im-server/v3/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 | + imAdminUserID []string |
| 24 | + config *config.AllConfig |
| 25 | + client *clientv3.Client |
| 26 | + configPath string |
| 27 | + runtimeEnv string |
| 28 | +} |
| 29 | + |
| 30 | +func NewConfigManager(IMAdminUserID []string, cfg *config.AllConfig, client *clientv3.Client, configPath string, runtimeEnv string) *ConfigManager { |
| 31 | + return &ConfigManager{ |
| 32 | + imAdminUserID: IMAdminUserID, |
| 33 | + config: cfg, |
| 34 | + client: client, |
| 35 | + configPath: configPath, |
| 36 | + runtimeEnv: runtimeEnv, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func (cm *ConfigManager) CheckAdmin(c *gin.Context) { |
| 41 | + if err := authverify.CheckAdmin(c, cm.imAdminUserID); err != nil { |
| 42 | + apiresp.GinError(c, err) |
| 43 | + c.Abort() |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func (cm *ConfigManager) GetConfig(c *gin.Context) { |
| 48 | + var req apistruct.GetConfigReq |
| 49 | + if err := c.BindJSON(&req); err != nil { |
| 50 | + apiresp.GinError(c, errs.ErrArgs.WithDetail(err.Error()).Wrap()) |
| 51 | + return |
| 52 | + } |
| 53 | + conf := cm.config.Name2Config(req.ConfigName) |
| 54 | + if conf == nil { |
| 55 | + apiresp.GinError(c, errs.ErrArgs.WithDetail("config name not found").Wrap()) |
| 56 | + return |
| 57 | + } |
| 58 | + b, err := json.Marshal(conf) |
| 59 | + if err != nil { |
| 60 | + apiresp.GinError(c, err) |
| 61 | + return |
| 62 | + } |
| 63 | + apiresp.GinSuccess(c, string(b)) |
| 64 | +} |
| 65 | + |
| 66 | +func (cm *ConfigManager) GetConfigList(c *gin.Context) { |
| 67 | + var resp apistruct.GetConfigListResp |
| 68 | + resp.ConfigNames = cm.config.GetConfigNames() |
| 69 | + resp.Environment = runtimeenv.PrintRuntimeEnvironment() |
| 70 | + resp.Version = version.Version |
| 71 | + |
| 72 | + apiresp.GinSuccess(c, resp) |
| 73 | +} |
| 74 | + |
| 75 | +func (cm *ConfigManager) SetConfig(c *gin.Context) { |
| 76 | + if cm.config.Discovery.Enable != config.ETCD { |
| 77 | + apiresp.GinError(c, errs.New("only etcd support set config").Wrap()) |
| 78 | + return |
| 79 | + } |
| 80 | + var req apistruct.SetConfigReq |
| 81 | + if err := c.BindJSON(&req); err != nil { |
| 82 | + apiresp.GinError(c, errs.ErrArgs.WithDetail(err.Error()).Wrap()) |
| 83 | + return |
| 84 | + } |
| 85 | + var err error |
| 86 | + switch req.ConfigName { |
| 87 | + case cm.config.Discovery.GetConfigFileName(): |
| 88 | + err = compareAndSave[config.Discovery](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 89 | + case cm.config.Kafka.GetConfigFileName(): |
| 90 | + err = compareAndSave[config.Kafka](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 91 | + case cm.config.LocalCache.GetConfigFileName(): |
| 92 | + err = compareAndSave[config.LocalCache](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 93 | + case cm.config.Log.GetConfigFileName(): |
| 94 | + err = compareAndSave[config.Log](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 95 | + case cm.config.Minio.GetConfigFileName(): |
| 96 | + err = compareAndSave[config.Minio](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 97 | + case cm.config.Mongo.GetConfigFileName(): |
| 98 | + err = compareAndSave[config.Mongo](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 99 | + case cm.config.Notification.GetConfigFileName(): |
| 100 | + err = compareAndSave[config.Notification](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 101 | + case cm.config.API.GetConfigFileName(): |
| 102 | + err = compareAndSave[config.API](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 103 | + case cm.config.CronTask.GetConfigFileName(): |
| 104 | + err = compareAndSave[config.CronTask](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 105 | + case cm.config.MsgGateway.GetConfigFileName(): |
| 106 | + err = compareAndSave[config.MsgGateway](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 107 | + case cm.config.MsgTransfer.GetConfigFileName(): |
| 108 | + err = compareAndSave[config.MsgTransfer](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 109 | + case cm.config.Push.GetConfigFileName(): |
| 110 | + err = compareAndSave[config.Push](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 111 | + case cm.config.Auth.GetConfigFileName(): |
| 112 | + err = compareAndSave[config.Auth](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 113 | + case cm.config.Conversation.GetConfigFileName(): |
| 114 | + err = compareAndSave[config.Conversation](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 115 | + case cm.config.Friend.GetConfigFileName(): |
| 116 | + err = compareAndSave[config.Friend](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 117 | + case cm.config.Group.GetConfigFileName(): |
| 118 | + err = compareAndSave[config.Group](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 119 | + case cm.config.Msg.GetConfigFileName(): |
| 120 | + err = compareAndSave[config.Msg](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 121 | + case cm.config.Third.GetConfigFileName(): |
| 122 | + err = compareAndSave[config.Third](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 123 | + case cm.config.User.GetConfigFileName(): |
| 124 | + err = compareAndSave[config.User](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 125 | + case cm.config.Redis.GetConfigFileName(): |
| 126 | + err = compareAndSave[config.Redis](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 127 | + case cm.config.Share.GetConfigFileName(): |
| 128 | + err = compareAndSave[config.Share](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 129 | + case cm.config.Webhooks.GetConfigFileName(): |
| 130 | + err = compareAndSave[config.Webhooks](c, cm.config.Name2Config(req.ConfigName), &req, cm.client) |
| 131 | + default: |
| 132 | + apiresp.GinError(c, errs.ErrArgs.Wrap()) |
| 133 | + return |
| 134 | + } |
| 135 | + if err != nil { |
| 136 | + apiresp.GinError(c, errs.ErrArgs.WithDetail(err.Error()).Wrap()) |
| 137 | + return |
| 138 | + } |
| 139 | + apiresp.GinSuccess(c, nil) |
| 140 | +} |
| 141 | + |
| 142 | +func compareAndSave[T any](c *gin.Context, old any, req *apistruct.SetConfigReq, client *clientv3.Client) error { |
| 143 | + conf := new(T) |
| 144 | + err := json.Unmarshal([]byte(req.Data), &conf) |
| 145 | + if err != nil { |
| 146 | + return errs.ErrArgs.WithDetail(err.Error()).Wrap() |
| 147 | + } |
| 148 | + eq := reflect.DeepEqual(old, conf) |
| 149 | + if eq { |
| 150 | + return nil |
| 151 | + } |
| 152 | + data, err := json.Marshal(conf) |
| 153 | + if err != nil { |
| 154 | + return errs.ErrArgs.WithDetail(err.Error()).Wrap() |
| 155 | + } |
| 156 | + _, err = client.Put(c, etcd.BuildKey(req.ConfigName), string(data)) |
| 157 | + if err != nil { |
| 158 | + return errs.WrapMsg(err, "save to etcd failed") |
| 159 | + } |
| 160 | + return nil |
| 161 | +} |
| 162 | + |
| 163 | +func (cm *ConfigManager) ResetConfig(c *gin.Context) { |
| 164 | + go cm.resetConfig(c) |
| 165 | + apiresp.GinSuccess(c, nil) |
| 166 | +} |
| 167 | + |
| 168 | +func (cm *ConfigManager) resetConfig(c *gin.Context) { |
| 169 | + txn := cm.client.Txn(c) |
| 170 | + type initConf struct { |
| 171 | + old any |
| 172 | + new any |
| 173 | + isChanged bool |
| 174 | + } |
| 175 | + configMap := map[string]*initConf{ |
| 176 | + cm.config.Discovery.GetConfigFileName(): {old: &cm.config.Discovery, new: new(config.Discovery)}, |
| 177 | + cm.config.Kafka.GetConfigFileName(): {old: &cm.config.Kafka, new: new(config.Kafka)}, |
| 178 | + cm.config.LocalCache.GetConfigFileName(): {old: &cm.config.LocalCache, new: new(config.LocalCache)}, |
| 179 | + cm.config.Log.GetConfigFileName(): {old: &cm.config.Log, new: new(config.Log)}, |
| 180 | + cm.config.Minio.GetConfigFileName(): {old: &cm.config.Minio, new: new(config.Minio)}, |
| 181 | + cm.config.Mongo.GetConfigFileName(): {old: &cm.config.Mongo, new: new(config.Mongo)}, |
| 182 | + cm.config.Notification.GetConfigFileName(): {old: &cm.config.Notification, new: new(config.Notification)}, |
| 183 | + cm.config.API.GetConfigFileName(): {old: &cm.config.API, new: new(config.API)}, |
| 184 | + cm.config.CronTask.GetConfigFileName(): {old: &cm.config.CronTask, new: new(config.CronTask)}, |
| 185 | + cm.config.MsgGateway.GetConfigFileName(): {old: &cm.config.MsgGateway, new: new(config.MsgGateway)}, |
| 186 | + cm.config.MsgTransfer.GetConfigFileName(): {old: &cm.config.MsgTransfer, new: new(config.MsgTransfer)}, |
| 187 | + cm.config.Push.GetConfigFileName(): {old: &cm.config.Push, new: new(config.Push)}, |
| 188 | + cm.config.Auth.GetConfigFileName(): {old: &cm.config.Auth, new: new(config.Auth)}, |
| 189 | + cm.config.Conversation.GetConfigFileName(): {old: &cm.config.Conversation, new: new(config.Conversation)}, |
| 190 | + cm.config.Friend.GetConfigFileName(): {old: &cm.config.Friend, new: new(config.Friend)}, |
| 191 | + cm.config.Group.GetConfigFileName(): {old: &cm.config.Group, new: new(config.Group)}, |
| 192 | + cm.config.Msg.GetConfigFileName(): {old: &cm.config.Msg, new: new(config.Msg)}, |
| 193 | + cm.config.Third.GetConfigFileName(): {old: &cm.config.Third, new: new(config.Third)}, |
| 194 | + cm.config.User.GetConfigFileName(): {old: &cm.config.User, new: new(config.User)}, |
| 195 | + cm.config.Redis.GetConfigFileName(): {old: &cm.config.Redis, new: new(config.Redis)}, |
| 196 | + cm.config.Share.GetConfigFileName(): {old: &cm.config.Share, new: new(config.Share)}, |
| 197 | + cm.config.Webhooks.GetConfigFileName(): {old: &cm.config.Webhooks, new: new(config.Webhooks)}, |
| 198 | + } |
| 199 | + |
| 200 | + changedKeys := make([]string, 0, len(configMap)) |
| 201 | + for k, v := range configMap { |
| 202 | + err := config.Load( |
| 203 | + cm.configPath, |
| 204 | + k, |
| 205 | + config.EnvPrefixMap[k], |
| 206 | + cm.runtimeEnv, |
| 207 | + v.new, |
| 208 | + ) |
| 209 | + if err != nil { |
| 210 | + log.ZError(c, "load config failed", err) |
| 211 | + continue |
| 212 | + } |
| 213 | + v.isChanged = reflect.DeepEqual(v.old, v.new) |
| 214 | + if !v.isChanged { |
| 215 | + changedKeys = append(changedKeys, k) |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + ops := make([]clientv3.Op, 0) |
| 220 | + for _, k := range changedKeys { |
| 221 | + data, err := json.Marshal(configMap[k].new) |
| 222 | + if err != nil { |
| 223 | + log.ZError(c, "marshal config failed", err) |
| 224 | + continue |
| 225 | + } |
| 226 | + ops = append(ops, clientv3.OpPut(etcd.BuildKey(k), string(data))) |
| 227 | + } |
| 228 | + if len(ops) > 0 { |
| 229 | + txn.Then(ops...) |
| 230 | + _, err := txn.Commit() |
| 231 | + if err != nil { |
| 232 | + log.ZError(c, "commit etcd txn failed", err) |
| 233 | + return |
| 234 | + } |
| 235 | + } |
| 236 | +} |
| 237 | + |
| 238 | +func (cm *ConfigManager) Restart(c *gin.Context) { |
| 239 | + go cm.restart(c) |
| 240 | + apiresp.GinSuccess(c, nil) |
| 241 | +} |
| 242 | + |
| 243 | +func (cm *ConfigManager) restart(c *gin.Context) { |
| 244 | + time.Sleep(time.Millisecond * 200) // wait for Restart http call return |
| 245 | + t := time.Now().Unix() |
| 246 | + _, err := cm.client.Put(c, etcd.BuildKey(etcd.RestartKey), strconv.Itoa(int(t))) |
| 247 | + if err != nil { |
| 248 | + log.ZError(c, "restart etcd put key failed", err) |
| 249 | + } |
| 250 | +} |
0 commit comments