package conf import ( "LAPP_SJA_ME/utils" "github.com/spf13/viper" "gopkg.in/yaml.v2" "os" ) var DbConfig EnvConfig type EnvConfig struct { DbType string `yaml:"dbtype"` Server string `yaml:"server"` Ip string `yaml:"ip"` User string `yaml:"user"` Password string `yaml:"password"` DbName string `yaml:"dbname"` Port int `yaml:"port"` TemplatePath string `yaml:"templatepath"` ReadTaskInterval int `yaml:"readtaskinterval"` PrinterType string `yaml:"printertype"` Printnum int `yaml:"printnum"` Msgtype string `yaml:"msgtype"` Finr int `yaml:"finr"` Inbox string `yaml:"inbox"` Outbox string `yaml:"outbox"` Log string `yaml:"log"` App string `yaml:"app"` AppType string `yaml:"apptype"` } //read yaml config //注:path为yaml或yml文件的路径 func ReadYamlConfig() error { path, err := utils.GetCurrentPath("conf/config.yaml") if err != nil { return err } f, err := os.Open(path) if err != nil { return err } else { yaml.NewDecoder(f).Decode(&DbConfig) } defer f.Close() return nil } var AppInfo Config type Config struct { *App `mapstructure:"app"` *ETCD `mapstructure:"etcd"` } type App struct { Port int `yaml:"port"` TaskNums int `yaml:"tasknums"` ErrNums int `yaml:"errnums"` ShellPath string `yaml:"shellpath"` LocalAddr string `yaml:"localaddr"` Mod string `yaml:"mod"` Name string `yaml:"name"` UseETCD bool `yaml:"useetcd"` } type ETCD struct { Addrs []string `yaml:"addrs"` Timeout int `yaml:"timeout"` LockLease int64 `yaml:"locklease"` ServiceLease int64 `yaml:"servicelease"` } func InitConfig() (err error) { baseDir, err := utils.GetCurrentPath("conf") if err != nil { return } viper.SetConfigName("app_config") viper.SetConfigType("yaml") viper.AddConfigPath(baseDir) if err = viper.ReadInConfig(); err != nil { return } if err = viper.Unmarshal(&AppInfo); err != nil { return } return nil }