diff --git a/README.MD b/README.MD index 0c02af84c..101a42b8b 100644 --- a/README.MD +++ b/README.MD @@ -155,7 +155,7 @@ gf框架基于模块化设计,实现了通用的HTTP/TCP/UDP Server,并实 ```json { "viewpath" : "/home/www/templates/", - "routines" : { + "routings" : { "dispatch" : { "\/list\/page\/(\d+)[\/\?]*" : "/list?page=$1&" } diff --git a/g/frame/gcfg/gcfg.go b/g/frame/gcfg/gcfg.go index 01a3891a7..0c85405c8 100644 --- a/g/frame/gcfg/gcfg.go +++ b/g/frame/gcfg/gcfg.go @@ -8,11 +8,11 @@ package gcfg import ( + "sync" + "strings" "gitee.com/johng/gf/g/os/gfile" "gitee.com/johng/gf/g/container/gmap" "gitee.com/johng/gf/g/encoding/gjson" - "sync" - "strings" ) const ( diff --git a/g/frame/gins/gins.go b/g/frame/gins/gins.go index 8fcbfa98a..ef94d27f3 100644 --- a/g/frame/gins/gins.go +++ b/g/frame/gins/gins.go @@ -21,9 +21,9 @@ import ( ) const ( - FRAME_CORE_COMPONENT_NAME_VIEW = "gf.core.component.view" - FRAME_CORE_COMPONENT_NAME_CONFIG = "gf.core.component.config" - FRAME_CORE_COMPONENT_NAME_DATABASE = "gf.core.component.database" + gFRAME_CORE_COMPONENT_NAME_VIEW = "gf.core.component.view" + gFRAME_CORE_COMPONENT_NAME_CONFIG = "gf.core.component.config" + gFRAME_CORE_COMPONENT_NAME_DATABASE = "gf.core.component.database" ) // 单例对象存储器 @@ -39,9 +39,28 @@ func Set(k string, v interface{}) { instances.Set(k, v) } +// 自定义框架核心组件:View +func SetView(v *gview.View) { + instances.Set(gFRAME_CORE_COMPONENT_NAME_VIEW, v) +} + +// 自定义框架核心组件:Config +func SetConfig(v *gcfg.Config) { + instances.Set(gFRAME_CORE_COMPONENT_NAME_CONFIG, v) +} + +// 自定义框架核心组件:Database +func SetDatabase(v gdb.Link, names...string) { + dbCacheKey := gFRAME_CORE_COMPONENT_NAME_DATABASE + if len(names) > 0 { + dbCacheKey += names[0] + } + instances.Set(dbCacheKey, v) +} + // 核心对象:View func View() *gview.View { - result := Get(FRAME_CORE_COMPONENT_NAME_VIEW) + result := Get(gFRAME_CORE_COMPONENT_NAME_VIEW) if result != nil { return result.(*gview.View) } else { @@ -53,7 +72,7 @@ func View() *gview.View { } } view := gview.Get(path) - Set(FRAME_CORE_COMPONENT_NAME_VIEW, view) + Set(gFRAME_CORE_COMPONENT_NAME_VIEW, view) return view } return nil @@ -62,7 +81,7 @@ func View() *gview.View { // 核心对象:Config // 配置文件目录查找依次为:启动参数cfgpath、当前程序运行目录 func Config() *gcfg.Config { - result := Get(FRAME_CORE_COMPONENT_NAME_CONFIG) + result := Get(gFRAME_CORE_COMPONENT_NAME_CONFIG) if result != nil { return result.(*gcfg.Config) } else { @@ -74,7 +93,7 @@ func Config() *gcfg.Config { } } config := gcfg.New(path) - Set(FRAME_CORE_COMPONENT_NAME_CONFIG, config) + Set(gFRAME_CORE_COMPONENT_NAME_CONFIG, config) return config } return nil @@ -82,7 +101,11 @@ func Config() *gcfg.Config { // 核心对象:Database func Database(names...string) gdb.Link { - result := Get(FRAME_CORE_COMPONENT_NAME_DATABASE) + dbCacheKey := gFRAME_CORE_COMPONENT_NAME_DATABASE + if len(names) > 0 { + dbCacheKey += names[0] + } + result := Get(dbCacheKey) if result != nil { return result.(gdb.Link) } else { @@ -142,7 +165,7 @@ func Database(names...string) gdb.Link { } } if db != nil { - Set(FRAME_CORE_COMPONENT_NAME_DATABASE, db) + Set(dbCacheKey, db) return db } } diff --git a/geg/frame/config.json b/geg/frame/config.json index e3dd7c9fa..433dcf4a9 100644 --- a/geg/frame/config.json +++ b/geg/frame/config.json @@ -1,5 +1,5 @@ { - "viewpath" : "/home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/geg/frame/mvc/view", + "viewpath" : "/home/www/templates/", "database" : { "default" : [ { diff --git a/geg/frame/mvc/controller/demo/config.go b/geg/frame/mvc/controller/demo/config.go index 078d49dd8..739a2893f 100644 --- a/geg/frame/mvc/controller/demo/config.go +++ b/geg/frame/mvc/controller/demo/config.go @@ -1,9 +1,12 @@ package demo -import "gitee.com/johng/gf/g/net/ghttp" +import ( + "gitee.com/johng/gf/g/net/ghttp" + "gitee.com/johng/gf/g/frame/gins" +) func init() { ghttp.GetServer().BindHandler("/config", func (r *ghttp.Request) { - r.Response.WriteString("Apple") + r.Response.WriteString(gins.Config().GetString("database.default.0.host")) }) }