package config import ( "github.com/spf13/viper" "gopkg.in/yaml.v2" "leit.com/leit_seat_aps/common" "os" "fmt" ) 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"` SalveDbType string `yaml:"salvedbtype"` SalveServer string `yaml:"salveserver"` SalveIp string `yaml:"salveip"` SalveUser string `yaml:"salveuser"` SalvePassword string `yaml:"salvepassword"` SalveDbName string `yaml:"salvedbname"` SalvePort int `yaml:"salveport"` Finr int `yaml:"finr"` Filefolder string `yaml:"filefolder"` Crontime string `yaml:"crontime"` TemplatePath string `yaml:"templatepath"` ReadTaskInterval int `yaml:"readtaskinterval"` PrinterType string `yaml:"printertype"` Inbox string `yaml:"inbox"` Outbox string `yaml:"outbox"` Day int `yaml:"day"` Num int `yaml:"num"` TimeInterval int64 `yaml:"timeinterval"` Printobjtype string `yaml:"printobjtype"` Msgtype string `yaml:"msgtype"` Begtime string `yaml:"begtime"` Endtime string `yaml:"endtime"` Shiptog bool `yaml:"shiptog"` Packtog bool `yaml:"packtog"` } var ConfValue EnvConfig //read yaml config //注:path为yaml或yml文件的路径 func ReadYamlConfig(path string) (*EnvConfig, error) { f, err := os.Open(path) if err != nil { return nil, err } else { yaml.NewDecoder(f).Decode(&ConfValue) } defer f.Close() return &ConfValue, nil } var AppConfig 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 := common.GetCurrentPath("config") fmt.Println("conf:", baseDir) 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(&AppConfig); err != nil { return } return nil }