Skip to content

Commit

Permalink
fix: fix the env in config (#318)
Browse files Browse the repository at this point in the history
* feat: add some api about admin

* feat: add some api about admin

* fix: del the unuse method

* fix: Optimized code

* fix: fix the userID

* fix: del the config code getting from zk

* fix: fix the initRedis

* fix: add the get Evn method in Config

* fix: fix the config.yaml.template

* fix: fix the env config
  • Loading branch information
luhaoling authored Dec 18, 2023
1 parent b0349fa commit 5823c24
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 73 deletions.
8 changes: 4 additions & 4 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
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
129 changes: 68 additions & 61 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 Down Expand Up @@ -78,65 +76,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 +157,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 +221,65 @@ 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.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.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)

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

return nil
}

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

0 comments on commit 5823c24

Please sign in to comment.