conf.go 594 Bytes
Newer Older
kzkzzzz's avatar
kzkzzzz committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
package conf

import (
	"fmt"
	"github.com/spf13/viper"
)

// Config 配置
type Config struct {
	Server *Server
	Data   *Data
}

type Server struct {
	Http struct {
		Addr    string
		Timeout int
	}
	Grpc struct {
		Addr    string
		Timeout int
	}
}

type Data struct {
}

func LoadFromYaml(configFile string, conf interface{}) {
	v := viper.New()
	v.SetConfigFile(configFile)
	err := v.ReadInConfig()
	if err != nil {
		panic(fmt.Errorf("读取配置文件失败: %s", err))
	}

	err = v.Unmarshal(&conf)
	if err != nil {
		panic(fmt.Errorf("解析配置文件失败: %s", err))
	}
	return
}