12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package config
- import (
- "github.com/spf13/viper"
- )
- type any interface{}
- var (
- name = "config"
- configType = "yaml"
- path = "."
- )
- func init() {
- Init()
- }
- func Init() {
- //引入viper配置文件
- viper.SetConfigName(name) //name := lib.AppName()
- viper.SetConfigType(configType)
- viper.AddConfigPath(path)
- }
- func SetConfigInfo(n, c, p string) {
- name = n
- configType = c
- path = p
- Init()
- }
- func Load() error {
- return viper.ReadInConfig()
- }
- func Store() error {
- return viper.SafeWriteConfig()
- }
- func Register(module string, key string, value any) {
- viper.SetDefault(module+"."+key, value)
- }
- func GetBool(module string, key string) bool {
- return viper.GetBool(module + "." + key)
- }
- func GetString(module string, key string) string {
- return viper.GetString(module + "." + key)
- }
- func GetInt(module string, key string) int {
- return viper.GetInt(module + "." + key)
- }
- func GetFloat(module string, key string) float64 {
- return viper.GetFloat64(module + "." + key)
- }
- func GetStringSlice(module string, key string) []string {
- return viper.GetStringSlice(module + "." + key)
- }
|