2017-12-29 16:03:30 +08:00
|
|
|
|
// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
|
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
|
// You can obtain one at https://gitee.com/johng/gf.
|
|
|
|
|
|
2018-01-19 16:19:48 +08:00
|
|
|
|
// 配置管理.
|
2018-01-20 11:09:27 +08:00
|
|
|
|
// 配置文件格式支持:json, xml, toml, yaml/yml
|
2017-12-30 23:49:55 +08:00
|
|
|
|
package gcfg
|
2017-12-13 16:45:00 +08:00
|
|
|
|
|
|
|
|
|
import (
|
2018-01-04 14:43:57 +08:00
|
|
|
|
"sync"
|
|
|
|
|
"strings"
|
2017-12-14 17:32:51 +08:00
|
|
|
|
"gitee.com/johng/gf/g/os/gfile"
|
2018-04-18 16:16:09 +08:00
|
|
|
|
"gitee.com/johng/gf/g/os/gfsnotify"
|
2017-12-13 16:45:00 +08:00
|
|
|
|
"gitee.com/johng/gf/g/container/gmap"
|
|
|
|
|
"gitee.com/johng/gf/g/encoding/gjson"
|
2018-04-17 14:47:45 +08:00
|
|
|
|
"gitee.com/johng/gf/g/container/gtype"
|
2017-12-13 16:45:00 +08:00
|
|
|
|
)
|
|
|
|
|
|
2017-12-14 17:32:51 +08:00
|
|
|
|
const (
|
2018-04-18 16:00:47 +08:00
|
|
|
|
gDEFAULT_CONFIG_FILE = "config.yml" // 默认的配置管理文件名称
|
2017-12-14 17:32:51 +08:00
|
|
|
|
)
|
2017-12-13 16:45:00 +08:00
|
|
|
|
|
2017-12-14 17:32:51 +08:00
|
|
|
|
// 配置管理对象
|
|
|
|
|
type Config struct {
|
2018-04-17 17:09:07 +08:00
|
|
|
|
mu sync.RWMutex // 并发互斥锁
|
|
|
|
|
path *gtype.String // 配置文件存放目录,绝对路径
|
|
|
|
|
jsons *gmap.StringInterfaceMap // 配置文件对象
|
|
|
|
|
closed *gtype.Bool // 是否已经被close
|
2017-12-13 16:45:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-14 17:32:51 +08:00
|
|
|
|
// 生成一个配置管理对象
|
|
|
|
|
func New(path string) *Config {
|
2018-04-18 16:00:47 +08:00
|
|
|
|
return &Config {
|
2018-04-17 17:09:07 +08:00
|
|
|
|
path : gtype.NewString(path),
|
|
|
|
|
jsons : gmap.NewStringInterfaceMap(),
|
|
|
|
|
closed : gtype.NewBool(),
|
2017-12-13 16:45:00 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-17 17:09:07 +08:00
|
|
|
|
// 判断从哪个配置文件中获取内容,返回配置文件的绝对路径
|
2018-04-17 15:09:32 +08:00
|
|
|
|
func (c *Config) filePath(file []string) string {
|
|
|
|
|
path := gDEFAULT_CONFIG_FILE
|
|
|
|
|
if len(file) > 0 {
|
|
|
|
|
path = file[0]
|
2017-12-13 16:45:00 +08:00
|
|
|
|
}
|
2018-04-17 15:09:32 +08:00
|
|
|
|
fpath := c.path.Val() + gfile.Separator + path
|
2018-01-19 15:26:28 +08:00
|
|
|
|
return fpath
|
2018-01-04 11:08:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置配置管理器的配置文件存放目录绝对路径
|
|
|
|
|
func (c *Config) SetPath(path string) {
|
2018-04-17 14:47:45 +08:00
|
|
|
|
if strings.Compare(c.path.Val(), path) != 0 {
|
|
|
|
|
c.path.Set(path)
|
|
|
|
|
c.mu.Lock()
|
2018-01-04 11:59:35 +08:00
|
|
|
|
c.jsons = gmap.NewStringInterfaceMap()
|
2018-04-17 14:47:45 +08:00
|
|
|
|
c.mu.Unlock()
|
2018-01-04 11:59:35 +08:00
|
|
|
|
}
|
2018-04-17 14:47:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-21 11:20:31 +08:00
|
|
|
|
// 获取配置管理器的配置文件存放目录绝对路径
|
2018-04-17 14:47:45 +08:00
|
|
|
|
func (c *Config) GetPath() string {
|
|
|
|
|
return c.path.Val()
|
2017-12-13 16:45:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-21 11:20:31 +08:00
|
|
|
|
// 获取指定文件的绝对路径,默认获取默认的配置文件路径
|
|
|
|
|
func (c *Config) GetFilePath(name...string) string {
|
|
|
|
|
path := strings.TrimRight(c.path.Val(), gfile.Separator) + gfile.Separator
|
|
|
|
|
if len(name) > 0 {
|
|
|
|
|
return path + name[0]
|
|
|
|
|
}
|
|
|
|
|
return path + gDEFAULT_CONFIG_FILE
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-14 17:32:51 +08:00
|
|
|
|
// 添加配置文件到配置管理器中,第二个参数为非必须,如果不输入表示添加进入默认的配置名称中
|
2018-04-17 15:09:32 +08:00
|
|
|
|
func (c *Config) getJson(file []string) *gjson.Json {
|
|
|
|
|
fpath := c.filePath(file)
|
2018-01-04 11:08:23 +08:00
|
|
|
|
if r := c.jsons.Get(fpath); r != nil {
|
2017-12-15 13:09:09 +08:00
|
|
|
|
return r.(*gjson.Json)
|
|
|
|
|
}
|
2018-01-04 11:08:23 +08:00
|
|
|
|
if j, err := gjson.Load(fpath); err == nil {
|
2018-04-18 16:16:09 +08:00
|
|
|
|
c.mu.Lock()
|
|
|
|
|
c.addMonitor(fpath)
|
2018-01-04 11:08:23 +08:00
|
|
|
|
c.jsons.Set(fpath, j)
|
2018-04-18 16:16:09 +08:00
|
|
|
|
c.mu.Unlock()
|
2017-12-15 13:09:09 +08:00
|
|
|
|
return j
|
2017-12-13 16:45:00 +08:00
|
|
|
|
}
|
2017-12-15 13:09:09 +08:00
|
|
|
|
return nil
|
2017-12-13 16:45:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-14 17:32:51 +08:00
|
|
|
|
// 获取配置项,当不存在时返回nil
|
2018-04-17 15:09:32 +08:00
|
|
|
|
func (c *Config) Get(pattern string, file...string) interface{} {
|
|
|
|
|
if j := c.getJson(file); j != nil {
|
2017-12-15 13:09:09 +08:00
|
|
|
|
return j.Get(pattern)
|
2017-12-14 17:32:51 +08:00
|
|
|
|
}
|
|
|
|
|
return nil
|
2017-12-13 17:35:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-14 17:32:51 +08:00
|
|
|
|
// 获得一个键值对关联数组/哈希表,方便操作,不需要自己做类型转换
|
|
|
|
|
// 注意,如果获取的值不存在,或者类型与json类型不匹配,那么将会返回nil
|
2018-04-17 15:09:32 +08:00
|
|
|
|
func (c *Config) GetMap(pattern string, file...string) map[string]interface{} {
|
|
|
|
|
if j := c.getJson(file); j != nil {
|
2017-12-15 13:09:09 +08:00
|
|
|
|
return j.GetMap(pattern)
|
2017-12-13 17:35:43 +08:00
|
|
|
|
}
|
2017-12-14 17:32:51 +08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获得一个数组[]interface{},方便操作,不需要自己做类型转换
|
|
|
|
|
// 注意,如果获取的值不存在,或者类型与json类型不匹配,那么将会返回nil
|
2018-04-17 15:09:32 +08:00
|
|
|
|
func (c *Config) GetArray(pattern string, file...string) []interface{} {
|
|
|
|
|
if j := c.getJson(file); j != nil {
|
2017-12-15 13:09:09 +08:00
|
|
|
|
return j.GetArray(pattern)
|
2017-12-13 17:35:43 +08:00
|
|
|
|
}
|
|
|
|
|
return nil
|
2017-12-14 17:32:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回指定json中的string
|
2018-04-17 15:09:32 +08:00
|
|
|
|
func (c *Config) GetString(pattern string, file...string) string {
|
|
|
|
|
if j := c.getJson(file); j != nil {
|
2017-12-15 13:09:09 +08:00
|
|
|
|
return j.GetString(pattern)
|
2017-12-14 17:32:51 +08:00
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回指定json中的bool
|
2018-04-17 15:09:32 +08:00
|
|
|
|
func (c *Config) GetBool(pattern string, file...string) bool {
|
|
|
|
|
if j := c.getJson(file); j != nil {
|
2017-12-15 13:09:09 +08:00
|
|
|
|
return j.GetBool(pattern)
|
2017-12-14 17:32:51 +08:00
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-19 15:26:28 +08:00
|
|
|
|
// 返回指定json中的float32
|
2018-04-17 15:09:32 +08:00
|
|
|
|
func (c *Config) GetFloat32(pattern string, file...string) float32 {
|
|
|
|
|
if j := c.getJson(file); j != nil {
|
2018-01-19 15:26:28 +08:00
|
|
|
|
return j.GetFloat32(pattern)
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-14 17:32:51 +08:00
|
|
|
|
// 返回指定json中的float64
|
2018-04-17 15:09:32 +08:00
|
|
|
|
func (c *Config) GetFloat64(pattern string, file...string) float64 {
|
|
|
|
|
if j := c.getJson(file); j != nil {
|
2017-12-15 13:09:09 +08:00
|
|
|
|
return j.GetFloat64(pattern)
|
2017-12-14 17:32:51 +08:00
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回指定json中的float64->int
|
2018-04-17 15:09:32 +08:00
|
|
|
|
func (c *Config) GetInt(pattern string, file...string) int {
|
|
|
|
|
if j := c.getJson(file); j != nil {
|
2018-01-19 15:26:28 +08:00
|
|
|
|
return j.GetInt(pattern)
|
|
|
|
|
}
|
|
|
|
|
return 0
|
2017-12-14 17:32:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-19 15:26:28 +08:00
|
|
|
|
// 返回指定json中的float64->uint
|
2018-04-17 15:09:32 +08:00
|
|
|
|
func (c *Config) GetUint(pattern string, file...string) uint {
|
|
|
|
|
if j := c.getJson(file); j != nil {
|
2018-01-19 15:26:28 +08:00
|
|
|
|
return j.GetUint(pattern)
|
|
|
|
|
}
|
|
|
|
|
return 0
|
2018-04-17 17:09:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 清空当前配置文件缓存,强制重新从磁盘文件读取配置文件内容
|
|
|
|
|
func (c *Config) Reload() {
|
|
|
|
|
c.jsons.Clear()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 关闭Config对象,自动关闭异步协程
|
|
|
|
|
func (c *Config) Close() {
|
|
|
|
|
c.closed.Set(true)
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-18 16:00:47 +08:00
|
|
|
|
// 添加文件监控
|
2018-04-18 16:16:09 +08:00
|
|
|
|
func (c *Config) addMonitor(path string) {
|
|
|
|
|
if c.jsons.Get(path) == nil {
|
2018-04-18 16:00:47 +08:00
|
|
|
|
gfsnotify.Add(path, func(event *gfsnotify.Event) {
|
|
|
|
|
if event.IsRemove() {
|
|
|
|
|
gfsnotify.Remove(event.Path)
|
2018-04-17 17:09:07 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2018-04-18 16:00:47 +08:00
|
|
|
|
c.jsons.Remove(event.Path)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|