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 component config env #323

Merged
merged 18 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion deployments/templates/config.yaml
Original file line number Diff line number Diff line change
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: "127.0.0.1:10002" # OPENIM_SERVER_ADDRESS:API_OPENIM_PORT, URL of the OpenIM server

# Redis configuration - used for caching and session management
redis:
Expand Down
41 changes: 35 additions & 6 deletions pkg/common/config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,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 @@ -227,6 +226,7 @@ func configGetEnv() error {
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)
Expand All @@ -236,16 +236,17 @@ func configGetEnv() error {
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.OpenIMUrl = getStringEnv("OPENIM_SERVER_ADDRESS", "API_OPENIM_PORT", 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)
Expand All @@ -256,12 +257,40 @@ func configGetEnv() error {
return nil
}

func getEnv(key, fallback string) string {
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 getStringEnv(key1, key2 string, fallback string) string {
str1 := getEnv(key1, "")
str2 := getEnv(key2, "")
str := fmt.Sprintf("%s:%s", str1, str2)
if len(str) <= 2 {
return fallback
}
return str
}

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

return fallback
}

Expand All @@ -282,4 +311,4 @@ func getEnvIntPoint(key string, fallback *int64) (*int64, error) {
return &temp, nil
}
return fallback, nil
}
}
Loading