feat: add config loader

This commit is contained in:
lab 2021-12-28 01:07:31 +08:00
parent 217ed6924d
commit 0b0da3b030

View File

@ -0,0 +1,43 @@
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
}