package config import ( "github.com/spf13/viper" ) const ( DEFAULT_CONFIG_FILE_NAME = "weapp" DEFAULT_CONFIG_FILE_TYPE = "yaml" ) var defaultPaths = []string{ ".", "./conf", "./config", "../conf", "../config", "/conf", "/config", "${HOME}/config", "/", } func AutoLoad() error { return LoadConfig(DEFAULT_CONFIG_FILE_NAME, DEFAULT_CONFIG_FILE_TYPE) } func LoadConfig(fileName, filetype string, paths ...string) error { for _, path := range defaultPaths { viper.AddConfigPath(path) } for _, path := range paths { viper.AddConfigPath(path) } viper.SetConfigName(fileName) viper.SetConfigType(filetype) if err := viper.ReadInConfig(); err != nil { return err } return nil }