Skip to content

Commit

Permalink
update init config (#335)
Browse files Browse the repository at this point in the history
* update init config

* update init
  • Loading branch information
AndrewZuo01 authored Dec 22, 2023
1 parent 1d88058 commit a32cec0
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 41 deletions.
6 changes: 3 additions & 3 deletions cmd/api/admin-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions cmd/api/chat-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/rpc/admin-rpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
8 changes: 4 additions & 4 deletions cmd/rpc/chat-rpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/apicall/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions pkg/common/config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"flag"
"fmt"
"github.com/OpenIMSDK/tools/errs"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/db/cache/init_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion pkg/discovery_register/k8s_discovery_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"context"
"errors"
"fmt"
"github.com/OpenIMSDK/tools/errs"
"strings"
"time"

"github.com/OpenIMSDK/chat/pkg/common/config"
Expand All @@ -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
}
Expand Down
74 changes: 53 additions & 21 deletions tools/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
package component

import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
"os"
"strings"
"time"

"github.com/OpenIMSDK/protocol/constant"
Expand All @@ -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)
Expand All @@ -61,6 +46,7 @@ func ComponentCheck(cfgPath string, hide bool) error {
// return err
// }
}
//_, err := checkRedis()

return nil
}
Expand All @@ -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, " "))
}
}
}
Expand All @@ -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 {
Expand All @@ -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
}

0 comments on commit a32cec0

Please sign in to comment.