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: 优化加载配置(环境变量和文件) #41

Merged
merged 2 commits into from
Jul 6, 2021
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
41 changes: 28 additions & 13 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,9 @@ type Config struct {
}

func Setup(configPath string) {
viper.SetConfigFile(configPath)
viper.AutomaticEnv()
loadEnvToViper()
log.Println("Load config from env")
if err := viper.ReadInConfig(); err == nil {
log.Printf("Load config from %s success\n", configPath)
}
var conf = getDefaultConfig()
if err := viper.Unmarshal(&conf); err != nil {
log.Fatal(err)
}
loadConfigFromEnv(&conf)
loadConfigFromFile(configPath, &conf)
GlobalConfig = &conf
log.Printf("%+v\n", GlobalConfig)

Expand Down Expand Up @@ -80,7 +72,7 @@ func getDefaultConfig() Config {
LogDirPath: LogDirPath,
DrivePath: driveFolderPath,
AccessKeyFilePath: accessKeyFilePath,
CoreHost: "http://127.0.0.1:8080",
CoreHost: "http://localhost:8080",
BootstrapToken: "",
BindHost: "0.0.0.0",
HTTPPort: "8081",
Expand Down Expand Up @@ -121,13 +113,36 @@ func getPwdDirPath() string {
return ""
}

func loadEnvToViper() {
func loadConfigFromEnv(conf *Config) {
viper.AutomaticEnv() // 全局配置,用于其他 pkg 包可以用 viper 获取环境变量的值
envViper := viper.New()
for _, item := range os.Environ() {
envItem := strings.SplitN(item, "=", 2)
if len(envItem) == 2 {
viper.Set(envItem[0], envItem[1])
envViper.Set(envItem[0], viper.Get(envItem[0]))
}
}
if err := envViper.Unmarshal(conf); err == nil {
log.Println("Load config from env")
}

}

func loadConfigFromFile(path string, conf *Config) {
var err error
if have(path) {
fileViper := viper.New()
fileViper.SetConfigFile(path)
if err = fileViper.ReadInConfig(); err == nil {
if err = fileViper.Unmarshal(conf); err == nil {
log.Printf("Load config from %s success\n", path)
return
}
}
}
if err != nil {
log.Fatalf("Load config from %s failed: %s\n", path, err)
}
}

const prefixName = "[Lion]"
Expand Down
32 changes: 3 additions & 29 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,17 @@
package config

import (
"log"
"os"
"testing"

"github.com/spf13/viper"
)

func setupFromYmlFile() {
f, err := os.Open("config_test.yml")
if err != nil {
log.Fatal(err)
return
}
defer f.Close()
viper.SetConfigType("yml")
viper.AutomaticEnv()
err = viper.ReadConfig(f)
if err != nil {
log.Fatal(err)
}
}

func TestSetupByYml(t *testing.T) {
setupFromYmlFile()
var conf = getDefaultConfig()
if err := viper.Unmarshal(&conf); err != nil {
t.Fatalf("%s \n", err.Error())
}
loadConfigFromFile("config_test.yml", &conf)
t.Log(conf)
}

func TestSetupByEnv(t *testing.T) {
viper.AutomaticEnv()
loadEnvToViper()
var conf = getDefaultConfig()
if err := viper.Unmarshal(&conf); err != nil {
t.Fatalf("%s \n", err.Error())
}
t.Logf("%+v\n",conf)
loadConfigFromEnv(&conf)
t.Log(conf)
}
2 changes: 1 addition & 1 deletion pkg/config/config_test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CORE_HSOT: http://10.0.0.5:8080
CORE_HOST: http://10.0.0.5:8080

BOOTSTRAP_TOKEN: ICAgICAgICBUWCBl

Expand Down