代码拉取完成,页面将自动刷新
package file
import (
"fmt"
"path/filepath"
"strings"
"gitee.com/marcellos/common/config"
"gitee.com/marcellos/common/base"
"gitee.com/marcellos/common/crypto"
"github.com/fsnotify/fsnotify"
"gitee.com/marcellos/common/config/internal"
)
// New 从文件初始化配置
func New(file string, cb func()) (*config.Config, error) {
var (
dir = filepath.Dir(file)
filename = filepath.Base(file)
err error
ok bool
hash, newHash string
)
if ok, err = base.PathExists(file); err != nil || !ok {
return nil, fmt.Errorf("file not exists or no permission")
}
f := strings.Split(filename, ".")
if len(f) != 2 {
return nil, fmt.Errorf("file must contain path, name and suffix; like: /path/to/conf.yaml")
}
if !internal.IsSupportedExt(f[1]) {
return nil, fmt.Errorf("configuration file are not supported")
}
v := config.New()
{
v.SetConfigName(f[0])
v.AddConfigPath(dir)
v.SetConfigType(f[1])
}
if err = v.ReadInConfig(); err != nil {
return nil, err
}
hash, err = crypto.FileMd5(file)
if err != nil {
return nil, err
}
v.WatchConfig()
v.OnConfigChange(func(in fsnotify.Event) {
if newHash, err = crypto.FileMd5(file); err != nil {
return
}
if hash != newHash {
hash = newHash
cb()
}
})
return v, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。