28 lines
503 B
Go
28 lines
503 B
Go
package config
|
|
|
|
import (
|
|
"github.com/spf13/viper"
|
|
"log"
|
|
)
|
|
|
|
var Config MainConfig
|
|
|
|
func LoadConfig() error {
|
|
viper.SetConfigName("config")
|
|
viper.SetConfigType("yaml")
|
|
viper.AddConfigPath(".")
|
|
|
|
// 读取配置文件
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
log.Fatalf("Error reading config file, %s", err)
|
|
return err
|
|
}
|
|
|
|
// 反序列化配置到结构体
|
|
if err := viper.Unmarshal(&Config); err != nil {
|
|
log.Fatalf("Unable to decode into struct, %v", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|