2023-01-15 00:21:08 +08:00
|
|
|
package myconf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2024-06-20 11:00:14 +08:00
|
|
|
var (
|
|
|
|
vp *viper.Viper
|
|
|
|
)
|
|
|
|
|
2023-01-15 00:21:08 +08:00
|
|
|
func Init(confPath string, conf interface{}) {
|
2024-06-20 11:00:14 +08:00
|
|
|
vp = viper.New()
|
|
|
|
vp.SetConfigFile(confPath)
|
|
|
|
err := vp.ReadInConfig()
|
2023-01-15 00:21:08 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("读取配置文件失败 %s", err))
|
|
|
|
}
|
2024-06-20 11:00:14 +08:00
|
|
|
|
|
|
|
err = vp.Unmarshal(conf)
|
2023-01-15 00:21:08 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("解析配置文件失败 %s", err))
|
|
|
|
}
|
|
|
|
}
|
2024-06-20 11:00:14 +08:00
|
|
|
|
|
|
|
func Conf() *viper.Viper {
|
|
|
|
return vp
|
|
|
|
}
|