1 Star 0 Fork 0

玟兵/gin-partner

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
config.go 1.57 KB
一键复制 编辑 原始数据 按行查看 历史
玟兵 提交于 2024-05-07 19:41 . 初始化
package partner
import (
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
_ "github.com/spf13/viper/remote"
)
type Config struct {
V *viper.Viper
}
func NewConfig(onChange func(e fsnotify.Event)) *Config {
v := viper.New()
v.OnConfigChange(onChange)
return &Config{v}
}
func (c *Config) LoadFromFile(filePath, fileType string) error {
c.V.SetConfigFile(filePath)
c.V.SetConfigType(fileType)
if err := c.V.ReadInConfig(); err != nil {
return err
}
c.V.WatchConfig()
return nil
}
func (c *Config) LoadFromRemote(provider, endpoint, path, cType string) error {
if err := c.V.AddRemoteProvider(provider, endpoint, path); err != nil {
return err
}
c.V.SetConfigType(cType)
if err := c.V.ReadRemoteConfig(); err != nil {
return err
}
if err := c.V.WatchRemoteConfig(); err != nil {
return err
}
return nil
}
func (c *Config) SetDefault(key string, val any) {
c.V.SetDefault(key, val)
}
func (c *Config) Get(key string) any {
return c.V.Get(key)
}
func (c *Config) GetAll() map[string]any {
return c.V.AllSettings()
}
func (c *Config) GetString(key string) string {
return c.V.GetString(key)
}
func (c *Config) GetBool(key string) bool {
return c.V.GetBool(key)
}
func (c *Config) GetInt(key string) int {
return c.V.GetInt(key)
}
func (c *Config) GetInt64(key string) int64 {
return c.V.GetInt64(key)
}
func (c *Config) GetStrSlice(key string) []string {
return c.V.GetStringSlice(key)
}
func (c *Config) GetStrMap(key string) map[string]any {
return c.V.GetStringMap(key)
}
func (c *Config) GetIntSlice(key string) []int {
return c.V.GetIntSlice(key)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/binny_w/gin-partner.git
git@gitee.com:binny_w/gin-partner.git
binny_w
gin-partner
gin-partner
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385