Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add the component config env #322

Closed
wants to merge 14 commits into from
10 changes: 5 additions & 5 deletions deployments/templates/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ zookeeper:

# Configuration for the chat API service
chatApi:
openImChatApiPort: [ 10008 ] # OPENIM_CHAT_API_PORT, Port for OpenIM Chat API
openImChatApiPort: [ 10008 ] # Port for OpenIM Chat API
listenIP: # CHAT_API_LISTEN_IP, IP address to listen on for Chat API

# Configuration for the admin API service
adminApi:
openImAdminApiPort: [ 10009 ] # OPENIM_ADMIN_API_PORT, Port for OpenIM Admin API
openImAdminApiPort: [ 10009 ] # Port for OpenIM Admin API
listenIP: # ADMIN_API_LISTEN_IP, IP address to listen on for Admin API

# RPC configuration for service communication
Expand All @@ -41,8 +41,8 @@ rpc:

# Ports for RPC services
rpcPort:
openImAdminPort: [ 30200 ] # OPENIM_ADMIN_PORT, Port for OpenIM Admin RPC service
openImChatPort: [ 30300 ] # OPENIM_CHAT_PORT, Port for OpenIM Chat RPC service
openImAdminPort: [ 30200 ] # Port for OpenIM Admin RPC service
openImChatPort: [ 30300 ] # Port for OpenIM Chat RPC service

# Names for RPC services registration
rpcRegisterName:
Expand Down Expand Up @@ -118,7 +118,7 @@ adminList:
imAdmin: openIMAdmin

# URL for OpenIM server
openIMUrl: "http://127.0.0.1:10002" # OPENIM_SERVER_ADDRESS:API_OPENIM_PORT, URL of the OpenIM server
openIMUrl: "http://127.0.0.1:10002" # OPENIM_SERVER_ADDRESS, URL of the OpenIM server

# Redis configuration - used for caching and session management
redis:
Expand Down
4 changes: 2 additions & 2 deletions pkg/common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ var Config struct {
} `yaml:"rpc"`
Redis struct {
Address *[]string `yaml:"address"`
Username *string `yaml:"username"`
Password *string `yaml:"password"`
Username string `yaml:"username"`
Password string `yaml:"password"`
} `yaml:"redis"`
RpcPort struct {
OpenImAdminPort []int `yaml:"openImAdminPort"`
Expand Down
150 changes: 88 additions & 62 deletions pkg/common/config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ import (
"os"
"path/filepath"
"runtime"
"strconv"
"time"

Constant "github.com/OpenIMSDK/chat/pkg/common/constant"
"github.com/OpenIMSDK/protocol/constant"
openKeeper "github.com/OpenIMSDK/tools/discoveryregistry/zookeeper"

"github.com/OpenIMSDK/tools/utils"
"gopkg.in/yaml.v3"
)
Expand All @@ -49,7 +47,6 @@ func readConfig(configFile string) ([]byte, error) {
// if configFile != "" {
// b, err := os.ReadFile(configFile)
// if err == nil { // File exists and was read successfully
// fmt.Println("这里aaaaaaaa")
// return b, nil
// }
// }
Expand Down Expand Up @@ -78,65 +75,13 @@ func InitConfig(configFile string) error {
if err != nil {
return fmt.Errorf("read loacl config file error: %w", err)
}

if err := yaml.NewDecoder(bytes.NewReader(data)).Decode(&Config); err != nil {
return fmt.Errorf("parse loacl openIMConfig file error: %w", err)
}
if Config.Envs.Discovery != "k8s" {
zk, err := openKeeper.NewClient(Config.Zookeeper.ZkAddr, Config.Zookeeper.Schema,
openKeeper.WithFreq(time.Hour), openKeeper.WithUserNameAndPassword(Config.Zookeeper.Username,
Config.Zookeeper.Password), openKeeper.WithRoundRobin(), openKeeper.WithTimeout(10), openKeeper.WithLogger(&zkLogger{}))
if err != nil {
return utils.Wrap(err, "conn zk error ")
}
defer zk.Close()
var openIMConfigData []byte
for i := 0; i < 100; i++ {
var err error
configData, err := zk.GetConfFromRegistry(constant.OpenIMCommonConfigKey)
if err != nil {
fmt.Printf("get zk config [%d] error: %v\n;envs.descoery=%s", i, err, Config.Envs.Discovery)
time.Sleep(time.Second)
continue
}
if len(configData) == 0 {
fmt.Printf("get zk config [%d] data is empty\n", i)
time.Sleep(time.Second)
continue
}
openIMConfigData = configData
}
if len(openIMConfigData) == 0 {
return errors.New("get zk config data failed")
}
if err := yaml.NewDecoder(bytes.NewReader(openIMConfigData)).Decode(&imConfig); err != nil {
return fmt.Errorf("parse zk openIMConfig: %w", err)
}
// 这里可以优化,可将其优化为结构体层面的赋值
configFieldCopy(&Config.Mysql.Address, imConfig.Mysql.Address)
configFieldCopy(&Config.Mysql.Username, imConfig.Mysql.Username)
configFieldCopy(&Config.Mysql.Password, imConfig.Mysql.Password)
configFieldCopy(&Config.Mysql.Database, imConfig.Mysql.Database)
configFieldCopy(&Config.Mysql.MaxOpenConn, imConfig.Mysql.MaxOpenConn)
configFieldCopy(&Config.Mysql.MaxIdleConn, imConfig.Mysql.MaxIdleConn)
configFieldCopy(&Config.Mysql.MaxLifeTime, imConfig.Mysql.MaxLifeTime)
configFieldCopy(&Config.Mysql.LogLevel, imConfig.Mysql.LogLevel)
configFieldCopy(&Config.Mysql.SlowThreshold, imConfig.Mysql.SlowThreshold)

configFieldCopy(&Config.Log.StorageLocation, imConfig.Log.StorageLocation)
configFieldCopy(&Config.Log.RotationTime, imConfig.Log.RotationTime)
configFieldCopy(&Config.Log.RemainRotationCount, imConfig.Log.RemainRotationCount)
configFieldCopy(&Config.Log.RemainLogLevel, imConfig.Log.RemainLogLevel)
configFieldCopy(&Config.Log.IsStdout, imConfig.Log.IsStdout)
configFieldCopy(&Config.Log.WithStack, imConfig.Log.WithStack)
configFieldCopy(&Config.Log.IsJson, imConfig.Log.IsJson)

configFieldCopy(&Config.Secret, imConfig.Secret)
configFieldCopy(&Config.TokenPolicy.Expire, imConfig.TokenPolicy.Expire)

// Redis
configFieldCopy(&Config.Redis.Address, imConfig.Redis.Address)
configFieldCopy(&Config.Redis.Password, imConfig.Redis.Password)
configFieldCopy(&Config.Redis.Username, imConfig.Redis.Username)

if err := configGetEnv(); err != nil {
return fmt.Errorf("get env error:%w", err)
}

configData, err := yaml.Marshal(&Config)
Expand Down Expand Up @@ -211,8 +156,7 @@ func CreateCatalogPath(path string) []string {
// the parent is project(default)
pa3 := filepath.Join(path3, Constant.ConfigPath)

return []string{pa1, pa2,pa3}

return []string{pa1, pa2, pa3}

}

Expand Down Expand Up @@ -276,3 +220,85 @@ func FlagParse() (string, int, bool, bool, error) {
}
return configFile, ginPort, hide, showVersion, nil
}

func configGetEnv() error {
Config.Envs.Discovery = getEnv("ENVS_DISCOVERY", Config.Envs.Discovery)
Config.Zookeeper.Schema = getEnv("ZOOKEEPER_SCHEMA", Config.Zookeeper.Schema)
Config.Zookeeper.Username = getEnv("ZOOKEEPER_USERNAME", Config.Zookeeper.Username)
Config.Zookeeper.Password = getEnv("ZOOKEEPER_PASSWORD", Config.Zookeeper.Password)
Config.Zookeeper.ZkAddr = getArrEnv("ZOOKEEPER_ADDRESS", "ZOOKEEPER_PORT", Config.Zookeeper.ZkAddr)

Config.ChatApi.ListenIP = getEnv("CHAT_API_LISTEN_IP", Config.ChatApi.ListenIP)
Config.AdminApi.ListenIP = getEnv("ADMIN_API_LISTEN_IP", Config.AdminApi.ListenIP)
Config.Rpc.RegisterIP = getEnv("RPC_REGISTER_IP", Config.Rpc.RegisterIP)
Config.Rpc.ListenIP = getEnv("RPC_LISTEN_IP", Config.Rpc.ListenIP)

Config.Mysql.Username = getEnvStringPoint("MYSQL_USERNAME", Config.Mysql.Username)
Config.Mysql.Password = getEnvStringPoint("MYSQL_PASSWORD", Config.Mysql.Password)
Config.Mysql.Database = getEnvStringPoint("MYSQL_DATABASE", Config.Mysql.Database)
Config.Mysql.Address = getArrPointEnv("MYSQL_ADDRESS", "MYSQL_PORT", Config.Mysql.Address)

Config.Log.StorageLocation = getEnvStringPoint("LOG_STORAGE_LOCATION", Config.Log.StorageLocation)

Config.Secret = getEnvStringPoint("SECRET", Config.Secret)
Config.ProxyHeader = getEnv("PROXY_HEADER", Config.ProxyHeader)
Config.OpenIMUrl = getEnv("OPENIM_SERVER_ADDRESS", Config.OpenIMUrl)

Config.Redis.Username = getEnv("REDIS_USERNAME", Config.Redis.Username)
Config.Redis.Password = getEnv("REDIS_PASSWORD", Config.Redis.Password)
Config.Redis.Address = getArrPointEnv("REDIS_ADDRESS", "REDIS_PORT", Config.Redis.Address)

var err error
Config.TokenPolicy.Expire, err = getEnvIntPoint("TOKEN_EXPIRE", Config.TokenPolicy.Expire)
if err != nil {
return err
}

return nil
}

func getArrEnv(key1, key2 string, fallback []string) []string {
str1 := getEnv(key1, "")
str2 := getEnv(key2, "")
str := fmt.Sprintf("%s:%s", str1, str2)
if len(str) <= 1 {
return fallback
}
return []string{str}
}

func getArrPointEnv(key1, key2 string, fallback *[]string) *[]string {
str1 := getEnv(key1, "")
str2 := getEnv(key2, "")
str := fmt.Sprintf("%s:%s", str1, str2)
if len(str) <= 1 {
return fallback
}
return &[]string{str}
}

func getEnv(key, fallback string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}
return fallback
}

func getEnvStringPoint(key string, fallback *string) *string {
if value, exists := os.LookupEnv(key); exists {
return &value
}
return fallback
}

func getEnvIntPoint(key string, fallback *int64) (*int64, error) {
if value, exists := os.LookupEnv(key); exists {
val, err := strconv.Atoi(value)
temp := int64(val)
if err != nil {
return nil, err
}
return &temp, nil
}
return fallback, nil
}
12 changes: 6 additions & 6 deletions pkg/common/db/cache/init_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ func NewRedis() (redis.UniversalClient, error) {
if len(*config.Config.Redis.Address) > 1 {
rdb = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: *config.Config.Redis.Address,
Username: *config.Config.Redis.Username,
Password: *config.Config.Redis.Password, // no password set
Username: config.Config.Redis.Username,
Password: config.Config.Redis.Password, // no password set
PoolSize: 50,
MaxRetries: maxRetry,
})
} else {
rdb = redis.NewClient(&redis.Options{
Addr: (*config.Config.Redis.Address)[0],
Username: *config.Config.Redis.Username,
Password: *config.Config.Redis.Password, // no password set
DB: 0, // use default DB
PoolSize: 100, // connection pool size
Username: config.Config.Redis.Username,
Password: config.Config.Redis.Password, // no password set
DB: 0, // use default DB
PoolSize: 100, // connection pool size
MaxRetries: maxRetry,
})
}
Expand Down