From a32cec084ffa97b5bb45d87e17ec6e2af86bf3fe Mon Sep 17 00:00:00 2001 From: AndrewZuo01 <59896149+AndrewZuo01@users.noreply.github.com> Date: Fri, 22 Dec 2023 16:10:22 +0800 Subject: [PATCH] update init config (#335) * update init config * update init --- cmd/api/admin-api/main.go | 6 +- cmd/api/chat-api/main.go | 8 +- cmd/rpc/admin-rpc/main.go | 7 +- cmd/rpc/chat-rpc/main.go | 8 +- pkg/common/apicall/call.go | 2 +- pkg/common/config/parse.go | 7 +- pkg/common/db/cache/init_redis.go | 2 +- .../k8s_discovery_register.go | 11 ++- tools/component/component.go | 74 +++++++++++++------ 9 files changed, 84 insertions(+), 41 deletions(-) diff --git a/cmd/api/admin-api/main.go b/cmd/api/admin-api/main.go index acdd39fb5..fe33e758a 100644 --- a/cmd/api/admin-api/main.go +++ b/cmd/api/admin-api/main.go @@ -58,11 +58,11 @@ func main() { return } + if err := config.InitConfig(configFile); err != nil { + panic(err) + } err = component.ComponentCheck(configFile, hide) if err != nil { - return - } - if err := config.InitConfig(configFile); err != nil { panic(err) } if err := log.InitFromConfig("chat.log", "admin-api", *config.Config.Log.RemainLogLevel, *config.Config.Log.IsStdout, *config.Config.Log.IsJson, *config.Config.Log.StorageLocation, *config.Config.Log.RemainRotationCount, *config.Config.Log.RotationTime); err != nil { diff --git a/cmd/api/chat-api/main.go b/cmd/api/chat-api/main.go index 0c0a907fa..33555520e 100644 --- a/cmd/api/chat-api/main.go +++ b/cmd/api/chat-api/main.go @@ -67,15 +67,15 @@ func main() { flag.Parse() - err = component.ComponentCheck(configFile, hide) - if err != nil { - return - } err = config.InitConfig(configFile) if err != nil { fmt.Println("err ", err.Error()) panic(err) } + err = component.ComponentCheck(configFile, hide) + if err != nil { + panic(err) + } if err := log.InitFromConfig("chat.log", "chat-api", *config.Config.Log.RemainLogLevel, *config.Config.Log.IsStdout, *config.Config.Log.IsJson, *config.Config.Log.StorageLocation, *config.Config.Log.RemainRotationCount, *config.Config.Log.RotationTime); err != nil { panic(err) } diff --git a/cmd/rpc/admin-rpc/main.go b/cmd/rpc/admin-rpc/main.go index 872f8c5bb..6266debd3 100644 --- a/cmd/rpc/admin-rpc/main.go +++ b/cmd/rpc/admin-rpc/main.go @@ -50,11 +50,12 @@ func main() { } flag.Parse() + + if err := config.InitConfig(configFile); err != nil { + panic(err) + } err = component.ComponentCheck(configFile, hide) if err != nil { - return - } - if err := config.InitConfig(configFile); err != nil { panic(err) } if config.Config.Envs.Discovery == "k8s" { diff --git a/cmd/rpc/chat-rpc/main.go b/cmd/rpc/chat-rpc/main.go index b02705c43..7f7b9de2e 100644 --- a/cmd/rpc/chat-rpc/main.go +++ b/cmd/rpc/chat-rpc/main.go @@ -47,16 +47,16 @@ func main() { return } - err = component.ComponentCheck(configFile, hide) - if err != nil { - return - } if err := config.InitConfig(configFile); err != nil { panic(err) } if config.Config.Envs.Discovery == "k8s" { rpcPort = 80 } + err = component.ComponentCheck(configFile, hide) + if err != nil { + panic(err) + } if err := log.InitFromConfig("chat.log", "chat-rpc", *config.Config.Log.RemainLogLevel, *config.Config.Log.IsStdout, *config.Config.Log.IsJson, *config.Config.Log.StorageLocation, *config.Config.Log.RemainRotationCount, *config.Config.Log.RotationTime); err != nil { panic(err) } diff --git a/pkg/common/apicall/call.go b/pkg/common/apicall/call.go index 870ef0946..4b8780d43 100644 --- a/pkg/common/apicall/call.go +++ b/pkg/common/apicall/call.go @@ -94,7 +94,7 @@ func (a caller[Req, Resp]) call(ctx context.Context, req *Req) (*Resp, error) { log.ZDebug(ctx, "call caller successfully", "code", response.Status) defer response.Body.Close() if response.StatusCode != http.StatusOK { - return nil, errors.New(response.Status) + return nil, errs.Wrap(errors.New(response.Status)) } data, err := io.ReadAll(response.Body) if err != nil { diff --git a/pkg/common/config/parse.go b/pkg/common/config/parse.go index 143c2f26d..8e7b3f6ba 100644 --- a/pkg/common/config/parse.go +++ b/pkg/common/config/parse.go @@ -19,6 +19,7 @@ import ( "errors" "flag" "fmt" + "github.com/OpenIMSDK/tools/errs" "os" "path/filepath" "runtime" @@ -165,7 +166,7 @@ func findConfigPath(configFile string) (string, error) { // First, check the configFile argument if configFile != "" { if _, err := findConfigFile([]string{configFile}); err != nil { - return "", errors.New("the configFile argument path is error") + return "", errs.Wrap(errors.New("the configFile argument path is error")) } fmt.Println("configfile:", configFile) return configFile, nil @@ -176,7 +177,7 @@ func findConfigPath(configFile string) (string, error) { envConfigPath := os.Getenv(Constant.OpenIMConfig) if envConfigPath != "" { if _, err := findConfigFile([]string{envConfigPath}); err != nil { - return "", errors.New("the environment path config path is error") + return "", errs.Wrap(errors.New("the environment path config path is error")) } return envConfigPath, nil } @@ -194,7 +195,7 @@ func findConfigPath(configFile string) (string, error) { } // Forth, use the Default path. - return "", errors.New("the config.yaml path not found") + return "", errs.Wrap(errors.New("the config.yaml path not found")) } func FlagParse() (string, int, bool, bool, error) { diff --git a/pkg/common/db/cache/init_redis.go b/pkg/common/db/cache/init_redis.go index f42e93a80..e977c2101 100644 --- a/pkg/common/db/cache/init_redis.go +++ b/pkg/common/db/cache/init_redis.go @@ -35,7 +35,7 @@ const ( // NewRedis Initialize redis connection. func NewRedis() (redis.UniversalClient, error) { if len(*config.Config.Redis.Address) == 0 { - return nil, errors.New("redis address is empty") + return nil, errs.Wrap(errors.New("redis address is empty")) } specialerror.AddReplace(redis.Nil, errs.ErrRecordNotFound) var rdb redis.UniversalClient diff --git a/pkg/discovery_register/k8s_discovery_register.go b/pkg/discovery_register/k8s_discovery_register.go index 979b84750..ba21ada30 100644 --- a/pkg/discovery_register/k8s_discovery_register.go +++ b/pkg/discovery_register/k8s_discovery_register.go @@ -18,6 +18,8 @@ import ( "context" "errors" "fmt" + "github.com/OpenIMSDK/tools/errs" + "strings" "time" "github.com/OpenIMSDK/chat/pkg/common/config" @@ -37,11 +39,18 @@ func NewDiscoveryRegister(envType string) (discoveryregistry.SvcDiscoveryRegistr config.Config.Zookeeper.Username, config.Config.Zookeeper.Password, ), openkeeper.WithRoundRobin(), openkeeper.WithTimeout(10), openkeeper.WithLogger(log.NewZkLogger())) + err = errs.Wrap(err, + "Zookeeper ZkAddr: "+strings.Join(config.Config.Zookeeper.ZkAddr, ",")+ + ", Zookeeper Schema: "+config.Config.Zookeeper.Schema+ + ", Zookeeper Username: "+config.Config.Zookeeper.Username+ + ", Zookeeper Password: "+config.Config.Zookeeper.Password) case "k8s": client, err = NewK8sDiscoveryRegister() + err = errs.Wrap(err, + "envType: "+"k8s") default: client = nil - err = errors.New("envType not correct") + err = errs.Wrap(errors.New("envType not correct")) } return client, err } diff --git a/tools/component/component.go b/tools/component/component.go index 3bf40987b..b621c8307 100644 --- a/tools/component/component.go +++ b/tools/component/component.go @@ -15,8 +15,11 @@ package component import ( + "context" "fmt" + "github.com/redis/go-redis/v9" "os" + "strings" "time" "github.com/OpenIMSDK/protocol/constant" @@ -26,31 +29,13 @@ import ( "github.com/OpenIMSDK/tools/log" "github.com/go-zookeeper/zk" "github.com/pkg/errors" - "gopkg.in/yaml.v3" ) var ( MaxConnectTimes = 100 ) -func initCfg(cfgPath string) error { - file, err := os.ReadFile(cfgPath) - if err != nil { - return errs.Wrap(err) - } - err = yaml.Unmarshal(file, &config.Config) - if err != nil { - return errs.Wrap(err) - } - return err -} - func ComponentCheck(cfgPath string, hide bool) error { - err := initCfg(cfgPath) - if err != nil { - errorPrint(errs.Wrap(err).Error(), hide) - return err - } if config.Config.Envs.Discovery != "k8s" { if _, err := checkNewZkClient(hide); err != nil { errorPrint(fmt.Sprintf("%v.Please check if your openIM server has started", err.Error()), hide) @@ -61,6 +46,7 @@ func ComponentCheck(cfgPath string, hide bool) error { // return err // } } + //_, err := checkRedis() return nil } @@ -83,11 +69,13 @@ func newZkClient() (*zk.Conn, error) { fmt.Println("zk addr=", config.Config.Zookeeper.ZkAddr) if err != nil { fmt.Println("zookeeper connect error:", err) - return nil, errs.Wrap(err) + return nil, errs.Wrap(err, "Zookeeper Addr: "+strings.Join(config.Config.Zookeeper.ZkAddr, " ")) } else { if config.Config.Zookeeper.Username != "" && config.Config.Zookeeper.Password != "" { if err := c.AddAuth("digest", []byte(config.Config.Zookeeper.Username+":"+config.Config.Zookeeper.Password)); err != nil { - return nil, errs.Wrap(err) + return nil, errs.Wrap(err, "Zookeeper Username: "+config.Config.Zookeeper.Username+ + ", Zookeeper Password: "+config.Config.Zookeeper.Password+ + ", Zookeeper Addr: "+strings.Join(config.Config.Zookeeper.ZkAddr, " ")) } } } @@ -110,7 +98,7 @@ func checkNewZkClient(hide bool) (*zk.Conn, error) { successPrint(fmt.Sprint("zk starts successfully"), hide) return zkConn, nil } - return nil, errors.New("Connecting to zk fails") + return nil, errs.Wrap(errors.New("Connecting to zk fails")) } func checkGetCfg(conn *zk.Conn, hide bool) error { @@ -134,3 +122,47 @@ func checkGetCfg(conn *zk.Conn, hide bool) error { } return errors.New("Getting config from zk failed") } + +// checkRedis checks the Redis connection +func checkRedis() (string, error) { + // Prioritize environment variables + address := getEnv("REDIS_ADDRESS", strings.Join(*config.Config.Redis.Address, ",")) + username := getEnv("REDIS_USERNAME", config.Config.Redis.Username) + password := getEnv("REDIS_PASSWORD", config.Config.Redis.Password) + + // Split address to handle multiple addresses for cluster setup + redisAddresses := strings.Split(address, ",") + + var redisClient redis.UniversalClient + if len(redisAddresses) > 1 { + // Use cluster client for multiple addresses + redisClient = redis.NewClusterClient(&redis.ClusterOptions{ + Addrs: redisAddresses, + Username: username, + Password: password, + }) + } else { + // Use regular client for single address + redisClient = redis.NewClient(&redis.Options{ + Addr: redisAddresses[0], + Username: username, + Password: password, + }) + } + defer redisClient.Close() + + // Ping Redis to check connectivity + _, err := redisClient.Ping(context.Background()).Result() + str := "the addr is:" + strings.Join(redisAddresses, ",") + if err != nil { + return "", errs.Wrap(err, str) + } + + return str, nil +} +func getEnv(key, fallback string) string { + if value, exists := os.LookupEnv(key); exists { + return value + } + return fallback +}