wheat-cache/conf/public_conf.go

57 lines
959 B
Go
Raw Normal View History

2021-09-05 17:07:34 +08:00
package conf
import (
"log"
"github.com/spf13/viper"
)
const (
linuxPath = "/etc/wheat-cache/"
devPath = "./conf"
devPathBin = "../conf"
)
func init() {
setDefaultConfValue()
err := LoadConf("")
switch err.(type) {
case nil:
case viper.ConfigFileNotFoundError:
formatPath := []string{linuxPath, devPath, devPath}
log.Fatalf("The profile could not be read, read path:%v", formatPath)
default:
log.Fatalf("The resolution of the profile failed, err: %v", err)
}
}
func setDefaultConfValue() {
// 设置一些默认值
viper.Set("version", "base-01")
}
func LoadConf(path string) error {
if path != "" {
viper.AddConfigPath(path)
}
viper.SetConfigName("wheat-cache")
// 添加默认读取地址
// linux
viper.AddConfigPath(linuxPath)
// 开发环境
viper.AddConfigPath(devPath)
viper.AddConfigPath(devPathBin)
viper.SetConfigType("yaml")
err := viper.ReadInConfig()
if err != nil {
return err
}
return nil
}