Skip to content

Commit

Permalink
add resource feature for gcfg
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn committed Aug 19, 2019
1 parent e151055 commit 10e03ee
Show file tree
Hide file tree
Showing 28 changed files with 156 additions and 73 deletions.
27 changes: 1 addition & 26 deletions .example/frame/mvc/config.toml
Original file line number Diff line number Diff line change
@@ -1,29 +1,4 @@
# 模板引擎目录
viewpath = "/home/www/templates/"
test = "v=1"
# MySQL数据库配置
[database]
[[database.default]]
host = "127.0.0.1"
port = "3306"
user = "root"
pass = ""
name = "test"
type = "mysql"
role = "master"
charset = "utf8"
priority = "1"
[[database.default]]
host = "127.0.0.1"
port = "3306"
user = "root"
pass = "8692651"
name = "test"
type = "mysql"
role = "master"
charset = "utf8"
priority = "1"
# Redis数据库配置
viewpath = "/home/www/templates"
[redis]
disk = "127.0.0.1:6379,0"
cache = "127.0.0.1:6379,1"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import (
"fmt"
"time"

"github.com/gogf/gf/os/gtimer"

"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/gtime"
)

// 配置文件热更新示例
func main() {
c := g.Config()
// 每隔1秒打印当前配置项值,用户可手动在外部修改文件内容,gcfg读取到的配置项值会即时得到更新
gtime.SetInterval(time.Second, func() bool {
gtimer.SetInterval(time.Second, func() {
fmt.Println(c.Get("viewpath"))
return true
})

select {}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions .example/os/gcfg/resource/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"github.com/gogf/gf/frame/g"
_ "github.com/gogf/gf/os/gres/testdata"
)

func main() {
g.Res().Dump()
g.Dump(g.Config().Get("redis"))

g.Config().SetFileName("my.ini")
g.Dump(g.Config().Get("redis"))

g.Config().SetPath("/config-custom")
g.Config().SetFileName("my.ini")
g.Dump(g.Config().Get("redis"))

g.Config().SetFileName("config.toml")
g.Dump(g.Config().Get("redis"))
}
1 change: 0 additions & 1 deletion .example/os/gview/resource/main1.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ func main() {
gres.Dump()

v := g.View()
v.SetResource(gres.Instance())
v.SetPath("/template/layout1")
s, err := v.Parse("layout.html")
fmt.Println(err)
Expand Down
13 changes: 13 additions & 0 deletions frame/g/g_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/gogf/gf/net/gtcp"
"github.com/gogf/gf/net/gudp"
"github.com/gogf/gf/os/gcfg"
"github.com/gogf/gf/os/gres"
"github.com/gogf/gf/os/gview"
)

Expand Down Expand Up @@ -42,6 +43,18 @@ func Config(name ...string) *gcfg.Config {
return gins.Config(name...)
}

// Resource returns an instance of Resource.
// The parameter <name> is the name for the instance.
func Resource(name ...string) *gres.Resource {
return gins.Resource(name...)
}

// Res is alias of Resource.
// See Resource.
func Res(name ...string) *gres.Resource {
return Resource(name...)
}

// Database returns an instance of database ORM object with specified configuration group name.
func Database(name ...string) gdb.DB {
return gins.Database(name...)
Expand Down
7 changes: 7 additions & 0 deletions frame/gins/gins.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/gogf/gf/os/gcfg"
"github.com/gogf/gf/os/gfsnotify"
"github.com/gogf/gf/os/glog"
"github.com/gogf/gf/os/gres"
"github.com/gogf/gf/os/gview"
"github.com/gogf/gf/text/gregex"
"github.com/gogf/gf/text/gstr"
Expand Down Expand Up @@ -73,6 +74,12 @@ func Config(name ...string) *gcfg.Config {
return gcfg.Instance(name...)
}

// Resource returns an instance of Resource.
// The parameter <name> is the name for the instance.
func Resource(name ...string) *gres.Resource {
return gres.Instance(name...)
}

// 数据库操作对象,使用了连接池
func Database(name ...string) gdb.DB {
config := Config()
Expand Down
105 changes: 74 additions & 31 deletions os/gcfg/gcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"errors"
"fmt"

"github.com/gogf/gf/os/gres"

"github.com/gogf/gf/container/garray"
"github.com/gogf/gf/container/gmap"
"github.com/gogf/gf/container/gtype"
Expand Down Expand Up @@ -107,18 +109,28 @@ func (c *Config) filePath(file ...string) (path string) {
// The parameter <path> can be absolute or relative path,
// but absolute path is strongly recommended.
func (c *Config) SetPath(path string) error {
// Absolute path.
realPath := gfile.RealPath(path)
if realPath == "" {
// Relative path.
c.paths.RLockFunc(func(array []string) {
for _, v := range array {
if path, _ := gspath.Search(v, path); path != "" {
realPath = path
break
isDir := false
realPath := ""
if file := gres.Get(path); file != nil {
realPath = path
isDir = file.FileInfo().IsDir()
} else {
// Absolute path.
realPath = gfile.RealPath(path)
if realPath == "" {
// Relative path.
c.paths.RLockFunc(func(array []string) {
for _, v := range array {
if path, _ := gspath.Search(v, path); path != "" {
realPath = path
break
}
}
}
})
})
}
if realPath != "" {
isDir = gfile.IsDir(realPath)
}
}
// Path not exist.
if realPath == "" {
Expand All @@ -140,7 +152,7 @@ func (c *Config) SetPath(path string) error {
return err
}
// Should be a directory.
if !gfile.IsDir(realPath) {
if !isDir {
err := fmt.Errorf(`[gcfg] SetPath failed: path "%s" should be directory type`, path)
if errorPrint() {
glog.Error(err)
Expand Down Expand Up @@ -170,18 +182,28 @@ func (c *Config) SetViolenceCheck(check bool) {

// AddPath adds a absolute or relative path to the search paths.
func (c *Config) AddPath(path string) error {
// Absolute path.
realPath := gfile.RealPath(path)
if realPath == "" {
// Relative path.
c.paths.RLockFunc(func(array []string) {
for _, v := range array {
if path, _ := gspath.Search(v, path); path != "" {
realPath = path
break
isDir := false
realPath := ""
if file := gres.Get(path); file != nil {
realPath = path
isDir = file.FileInfo().IsDir()
} else {
// Absolute path.
realPath = gfile.RealPath(path)
if realPath == "" {
// Relative path.
c.paths.RLockFunc(func(array []string) {
for _, v := range array {
if path, _ := gspath.Search(v, path); path != "" {
realPath = path
break
}
}
}
})
})
}
if realPath != "" {
isDir = gfile.IsDir(realPath)
}
}
if realPath == "" {
buffer := bytes.NewBuffer(nil)
Expand All @@ -201,7 +223,7 @@ func (c *Config) AddPath(path string) error {
}
return err
}
if !gfile.IsDir(realPath) {
if !isDir {
err := fmt.Errorf(`[gcfg] AddPath failed: path "%s" should be directory type`, path)
if errorPrint() {
glog.Error(err)
Expand Down Expand Up @@ -233,15 +255,32 @@ func (c *Config) FilePath(file ...string) (path string) {
name = file[0]
}
c.paths.RLockFunc(func(array []string) {
for _, v := range array {
if path, _ = gspath.Search(v, name); path != "" {
break
for _, prefix := range array {
// Firstly checking the resource manager.
for _, v := range []string{"/", "/config", "/config/"} {
if file := gres.Get(prefix + v + name); file != nil {
path = file.Name()
return
}
}
if path, _ = gspath.Search(v+gfile.Separator+"config", name); path != "" {
break
// Secondly checking the file system.
if path, _ = gspath.Search(prefix, name); path != "" {
return
}
if path, _ = gspath.Search(prefix+gfile.Separator+"config", name); path != "" {
return
}
}
})
// Checking the configuration file in default paths.
if path == "" && !gres.IsEmpty() {
for _, v := range []string{"", "/", "/config", "/config/"} {
if file := gres.Get(v + name); file != nil {
path = file.Name()
return
}
}
}
return
}

Expand Down Expand Up @@ -272,13 +311,17 @@ func (c *Config) getJson(file ...string) *gjson.Json {
if filePath == "" {
return nil
}
content = gfile.GetContents(filePath)
if file := gres.Get(filePath); file != nil {
content = string(file.Content())
} else {
content = gfile.GetContents(filePath)
}
}
if j, err := gjson.LoadContent(content, true); err == nil {
j.SetViolenceCheck(c.vc.Val())
// Add monitor for this configuration file,
// any changes of this file will refresh its cache in Config object.
if filePath != "" {
if !gres.Contains(filePath) {
_, err = gfsnotify.Add(filePath, func(event *gfsnotify.Event) {
c.jsons.Remove(name)
})
Expand Down
4 changes: 2 additions & 2 deletions os/gcfg/gcfg_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

const (
// Default group name for instance usage.
DEFAULT_GROUP_NAME = "default"
DEFAULT_NAME = "default"
)

var (
Expand All @@ -23,7 +23,7 @@ var (
// Instance returns an instance of Config with default settings.
// The parameter <name> is the name for the instance.
func Instance(name ...string) *Config {
key := DEFAULT_GROUP_NAME
key := DEFAULT_NAME
if len(name) > 0 && name[0] != "" {
key = name[0]
}
Expand Down
5 changes: 5 additions & 0 deletions os/gres/gres.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func Contains(path string) bool {
return defaultResource.Contains(path)
}

// IsEmpty checks and returns whether the resource manager is empty.
func IsEmpty() bool {
return defaultResource.tree.IsEmpty()
}

// Scan returns the files under the given path, the parameter <path> should be a folder type.
//
// The pattern parameter <pattern> supports multiple file name patterns,
Expand Down
4 changes: 2 additions & 2 deletions os/gres/gres_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import "github.com/gogf/gf/container/gmap"

const (
// Default group name for instance usage.
DEFAULT_INSTANCE_NAME = "default"
DEFAULT_NAME = "default"
)

var (
Expand All @@ -21,7 +21,7 @@ var (
// Instance returns an instance of Resource.
// The parameter <name> is the name for the instance.
func Instance(name ...string) *Resource {
key := DEFAULT_INSTANCE_NAME
key := DEFAULT_NAME
if len(name) > 0 && name[0] != "" {
key = name[0]
}
Expand Down
5 changes: 5 additions & 0 deletions os/gres/gres_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ func (r *Resource) Contains(path string) bool {
return r.Get(path) != nil
}

// IsEmpty checks and returns whether the resource manager is empty.
func (r *Resource) IsEmpty() bool {
return r.tree.IsEmpty()
}

// Scan returns the files under the given path, the parameter <path> should be a folder type.
//
// The pattern parameter <pattern> supports multiple file name patterns,
Expand Down
4 changes: 4 additions & 0 deletions os/gres/testdata/files/config-custom/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
viewpath = "/home/www/templates"
[redis]
disk = "127.0.0.1:6379,4"
cache = "127.0.0.1:6379,5"
4 changes: 4 additions & 0 deletions os/gres/testdata/files/config-custom/my.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
viewpath = "/home/www/templates"
[redis]
disk = "127.0.0.1:6379,6"
cache = "127.0.0.1:6379,7"
4 changes: 4 additions & 0 deletions os/gres/testdata/files/config/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
viewpath = "/home/www/templates"
[redis]
disk = "127.0.0.1:6379,0"
cache = "127.0.0.1:6379,1"
4 changes: 4 additions & 0 deletions os/gres/testdata/files/config/my.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
viewpath = "/home/www/templates"
[redis]
disk = "127.0.0.1:6379,2"
cache = "127.0.0.1:6379,3"
2 changes: 1 addition & 1 deletion os/gres/testdata/testdata.go

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion os/gview/gview.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ func (view *View) SetPath(path string) error {
func (view *View) AddPath(path string) error {
isDir := false
realPath := ""

if file := gres.Get(path); file != nil {
realPath = path
isDir = file.FileInfo().IsDir()
Expand Down
Loading

0 comments on commit 10e03ee

Please sign in to comment.