improve template view instance initialization for instance of ghttp.Server

This commit is contained in:
John
2020-03-10 20:13:36 +08:00
parent 22e9965629
commit 645cecdffb
6 changed files with 31 additions and 42 deletions

View File

@ -8,12 +8,7 @@
package gins
import (
"github.com/gogf/gf/internal/intlog"
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/container/gmap"
"github.com/gogf/gf/os/gcfg"
"github.com/gogf/gf/os/gfsnotify"
)
var (
@ -59,15 +54,3 @@ func GetOrSetFuncLock(name string, f func() interface{}) interface{} {
func SetIfNotExist(name string, instance interface{}) bool {
return instances.SetIfNotExist(name, instance)
}
// addConfigMonitor adds fsnotify monitor for configuration file if it exists.
func addConfigMonitor(key string, config *gcfg.Config) {
if path := config.FilePath(); path != "" && gfile.Exists(path) {
_, err := gfsnotify.Add(path, func(event *gfsnotify.Event) {
instances.Remove(key)
})
if err != nil {
intlog.Error(err)
}
}
}

View File

@ -29,7 +29,7 @@ func Database(name ...string) gdb.DB {
group = name[0]
}
instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_DATABASE, group)
db := instances.GetOrSetFunc(instanceKey, func() interface{} {
db := instances.GetOrSetFuncLock(instanceKey, func() interface{} {
// Configuration already exists.
if gdb.GetConfig(group) != nil {
db, err := gdb.Instance(group)
@ -72,7 +72,6 @@ func Database(name ...string) gdb.DB {
gdb.SetConfigGroup(gdb.DEFAULT_GROUP_NAME, cg)
}
}
addConfigMonitor(instanceKey, config)
if db, err := gdb.New(name...); err == nil {
// Initialize logger for ORM.

View File

@ -24,7 +24,7 @@ func Log(name ...string) *glog.Logger {
instanceName = name[0]
}
instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_LOGGER, instanceName)
return instances.GetOrSetFunc(instanceKey, func() interface{} {
return instances.GetOrSetFuncLock(instanceKey, func() interface{} {
logger := glog.Instance(instanceName)
// To avoid file no found error while it's not necessary.
if Config().Available() {

View File

@ -24,7 +24,7 @@ func Redis(name ...string) *gredis.Redis {
group = name[0]
}
instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_REDIS, group)
result := instances.GetOrSetFunc(instanceKey, func() interface{} {
result := instances.GetOrSetFuncLock(instanceKey, func() interface{} {
// If already configured, it returns the redis instance.
if _, ok := gredis.GetConfig(group); ok {
return gredis.Instance(group)
@ -36,7 +36,6 @@ func Redis(name ...string) *gredis.Redis {
if err != nil {
panic(err)
}
addConfigMonitor(instanceKey, config)
return gredis.New(redisConfig)
} else {
panic(fmt.Sprintf(`configuration for redis not found for group "%s"`, group))

View File

@ -18,7 +18,7 @@ const (
// Server returns an instance of http server with specified name.
func Server(name ...interface{}) *ghttp.Server {
instanceKey := fmt.Sprintf("%s.%v", gFRAME_CORE_COMPONENT_NAME_SERVER, name)
return instances.GetOrSetFunc(instanceKey, func() interface{} {
return instances.GetOrSetFuncLock(instanceKey, func() interface{} {
s := ghttp.GetServer(name...)
// To avoid file no found error while it's not necessary.
if Config().Available() {
@ -36,7 +36,7 @@ func Server(name ...interface{}) *ghttp.Server {
}
// As it might use template feature,
// it initialize the view instance as well.
View()
_ = getViewInstance()
}
return s
}).(*ghttp.Server)

View File

@ -24,23 +24,31 @@ func View(name ...string) *gview.View {
instanceName = name[0]
}
instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_VIEWER, instanceName)
return instances.GetOrSetFunc(instanceKey, func() interface{} {
view := gview.Instance(instanceName)
// To avoid file no found error while it's not necessary.
if Config().Available() {
var m map[string]interface{}
// It firstly searches the configuration of the instance name.
if m = Config().GetMap(fmt.Sprintf(`%s.%s`, gVIEWER_NODE_NAME, instanceName)); m == nil {
// If the configuration for the instance does not exist,
// it uses the default view configuration.
m = Config().GetMap(gVIEWER_NODE_NAME)
}
if m != nil {
if err := view.SetConfigWithMap(m); err != nil {
panic(err)
}
}
}
return view
return instances.GetOrSetFuncLock(instanceKey, func() interface{} {
return getViewInstance(instanceName)
}).(*gview.View)
}
func getViewInstance(name ...string) *gview.View {
instanceName := gview.DEFAULT_NAME
if len(name) > 0 && name[0] != "" {
instanceName = name[0]
}
view := gview.Instance(instanceName)
// To avoid file no found error while it's not necessary.
if Config().Available() {
var m map[string]interface{}
// It firstly searches the configuration of the instance name.
if m = Config().GetMap(fmt.Sprintf(`%s.%s`, gVIEWER_NODE_NAME, name)); m == nil {
// If the configuration for the instance does not exist,
// it uses the default view configuration.
m = Config().GetMap(gVIEWER_NODE_NAME)
}
if m != nil {
if err := view.SetConfigWithMap(m); err != nil {
panic(err)
}
}
}
return view
}