to options mode

This commit is contained in:
yanghao05
2023-04-27 18:30:39 +08:00
parent 8ae3d19aa9
commit 574736a878
20 changed files with 180 additions and 50 deletions

View File

@@ -3,42 +3,29 @@ package http
import (
"log"
"github.com/rogeecn/atom/utils/fs"
"github.com/spf13/viper"
)
const DefaultPrefix = "HTTP"
const DefaultPrefix = "Http"
func AutoLoadConfig() *Config {
return LoadConfig("", DefaultPrefix)
return LoadConfig("")
}
func LoadConfig(file, envPrefix string) *Config {
func LoadConfig(file string) *Config {
if file == "" {
file = "config.toml"
}
if envPrefix == "" {
envPrefix = DefaultPrefix
}
v := viper.NewWithOptions(viper.KeyDelimiter("_"))
v.SetEnvPrefix(envPrefix)
v.AutomaticEnv()
if !fs.FileExist(file) {
return &Config{}
}
// load file
v.SetConfigFile(file)
if err := v.ReadInConfig(); err != nil {
log.Fatal(err)
}
var config Config
if err := v.Unmarshal(&config); err != nil {
if err := v.UnmarshalKey(DefaultPrefix, &config); err != nil {
log.Fatal(err)
}