From 3e2a3cf2f2b8ee5bc8a248c5ec2c4d1bee898bda Mon Sep 17 00:00:00 2001 From: John Date: Thu, 4 Jan 2018 11:08:23 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E7=AE=A1=E7=90=86=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E8=B7=AF=E7=94=B1=E6=8E=A7=E5=88=B6=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/frame/gcfg/gcfg.go | 27 +++++++++++++------ geg/frame/mvc/controller/demo/router.go | 12 +++++++++ geg/frame/mvc/controller/demo/router_patch.go | 12 +++++++++ 3 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 geg/frame/mvc/controller/demo/router.go create mode 100644 geg/frame/mvc/controller/demo/router_patch.go diff --git a/g/frame/gcfg/gcfg.go b/g/frame/gcfg/gcfg.go index b27e0afed..ba8d82671 100644 --- a/g/frame/gcfg/gcfg.go +++ b/g/frame/gcfg/gcfg.go @@ -11,6 +11,7 @@ import ( "gitee.com/johng/gf/g/os/gfile" "gitee.com/johng/gf/g/container/gmap" "gitee.com/johng/gf/g/encoding/gjson" + "sync" ) const ( @@ -19,36 +20,46 @@ const ( // 配置管理对象 type Config struct { + mu sync.RWMutex // 并发互斥锁 path string // 配置文件存放目录,绝对路径 jsons *gmap.StringInterfaceMap // 配置文件对象 } // 生成一个配置管理对象 func New(path string) *Config { - return &Config{ + return &Config { path : path, jsons : gmap.NewStringInterfaceMap(), } } // 判断从哪个配置文件中获取内容 -func (c *Config) file(files []string) string { +func (c *Config) filePath(files []string) string { file := gDEFAULT_CONFIG_FILE if len(files) > 0 { file = files[0] } - return file + ".json" + c.mu.RLock() + fpath := c.path + gfile.Separator + file + c.mu.RUnlock() + return fpath + ".json" +} + +// 设置配置管理器的配置文件存放目录绝对路径 +func (c *Config) SetPath(path string) { + c.mu.Lock() + c.path = path + c.mu.Unlock() } // 添加配置文件到配置管理器中,第二个参数为非必须,如果不输入表示添加进入默认的配置名称中 func (c *Config) getJson(files []string) *gjson.Json { - file := c.file(files) - if r := c.jsons.Get(file); r != nil { + fpath := c.filePath(files) + if r := c.jsons.Get(fpath); r != nil { return r.(*gjson.Json) } - path := c.path + gfile.Separator + file - if j, err := gjson.Load(path); err == nil { - c.jsons.Set(file, j) + if j, err := gjson.Load(fpath); err == nil { + c.jsons.Set(fpath, j) return j } return nil diff --git a/geg/frame/mvc/controller/demo/router.go b/geg/frame/mvc/controller/demo/router.go new file mode 100644 index 000000000..9c1913c11 --- /dev/null +++ b/geg/frame/mvc/controller/demo/router.go @@ -0,0 +1,12 @@ +package demo + +import "gitee.com/johng/gf/g/net/ghttp" + +func init() { + ghttp.GetServer().BindHandler("/list", List) + ghttp.GetServer().Router.SetRule(`\/list\/page\/(\d+)[\/\?]*`, "/list?page=$1&") +} + +func List(r *ghttp.Request) { + r.Response.WriteString("list page:" + r.GetQueryString("page")) +} \ No newline at end of file diff --git a/geg/frame/mvc/controller/demo/router_patch.go b/geg/frame/mvc/controller/demo/router_patch.go new file mode 100644 index 000000000..eb9ac1c21 --- /dev/null +++ b/geg/frame/mvc/controller/demo/router_patch.go @@ -0,0 +1,12 @@ +package demo + +import "gitee.com/johng/gf/g/net/ghttp" + +func init() { + ghttp.GetServer().BindHandler("/router-patch", RouterPatch) + ghttp.GetServer().Router.SetPatchRule(`\/list\?page=(\d+)&*`, "/list/page/$1?") +} + +func RouterPatch(r *ghttp.Request) { + r.Response.WriteString(`page2`) +} \ No newline at end of file