package config import ( "fmt" "github.com/spf13/viper" "os" "time" ) var ( // Conf crocodile conf Conf *conf ) // InitConf Config func InitConf(conf string) { //_, err := toml.DecodeFile(conf, &Conf) //if err != nil { // fmt.Printf("Err %v", err) // os.Exit(1) //} viper.SetConfigFile("common.toml") viper.SetConfigName("common") // 配置文件名称(无扩展名) viper.SetConfigType("toml") // 如果配置文件的名称中没有扩展名,则需要配置此项 viper.AddConfigPath("./") err := viper.ReadInConfig() // 查找并读取配置文件 if err != nil { // 处理读取配置文件的错误 fmt.Println("ReadInConfig err =>", err) return } err = viper.Unmarshal(&Conf) if err != nil { fmt.Println("Init Unmarshal err =>", err) os.Exit(1) return } } type conf struct { DB db Udp udp Mail mail Log log Env env Others map[string]OthersData } type udp struct { LocPort string JyAddr string JyPort int NeAddr string NePort int } type udpNext struct { Addr string Port int Stype string } type mail struct { Send bool To string Api string } // Log Config type log struct { LogPath string MaxSize int Compress bool MaxAge int MaxBackups int LogLevel string Format string } type db struct { MongoB mgo MongoP mgo MongoQ mgo MongoS mgo Es es Oss oss MysqlB mysql } type mysql struct { Addr string Dbname string Username string Password string } //oss oss 阿里云配置 type oss struct { Endpoint string AccessKey string AccessSecret string BucketName string Filesize int } type mgo struct { Addr string Dbname string Coll string Size int User string Password string Direct bool } //env 全局的相关配置 type env struct { Stype int //默认0,正式环境;1是测试环境,不会执行定时任务更新采购单位、中标单位、数据检测 } type es struct { Addr string AddrP string Size int Username string Password string IndexB string TypeB string IndexP string TypeP string IndexWinner string TypeWinner string IndexBuyer string TypeBuyer string DetailFilter []string IndexTmp string //FieldEs map[string]interface{} //FieldPurchasingist map[string]interface{} //FieldProcurementList map[string]interface{} //FieldWinnerOrder map[string]interface{} //Package map[string]interface{} Addr2 string Username2 string Password2 string Indexb2 string } type OthersData struct { MgoAddr string MgoDB string MgoColl string MgoUsername string MgoPassword string EsAddr string EsIndex string EsUsername string EsPassword string NextAddr string NextPort int Data map[string]interface{} } type duration struct { time.Duration } // UnmarshalText parse 10s to time.Time func (d *duration) UnmarshalText(text []byte) error { var err error d.Duration, err = time.ParseDuration(string(text)) return err }