improve pakage g/gins

This commit is contained in:
John 2020-02-23 20:25:55 +08:00
parent 02bd780a33
commit c70bc7c96a
18 changed files with 374 additions and 271 deletions

View File

@ -49,9 +49,15 @@ func TryCatch(try func(), catch ...func(exception interface{})) {
gutil.TryCatch(try, catch...)
}
// IsEmpty checks given value empty or not.
// It returns false if value is: integer(0), bool(false), slice/map(len=0), nil;
// or else true.
// IsNil checks whether given <value> is nil.
// Note that it might use reflect feature which affects performance a little bit.
func IsNil(value interface{}) bool {
return empty.IsNil(value)
}
// IsEmpty checks whether given <value> empty.
// It returns true if <value> is in: 0, nil, false, "", len(slice/map/chan) == 0.
// Or else it returns true.
func IsEmpty(value interface{}) bool {
return empty.IsEmpty(value)
}

View File

@ -10,6 +10,7 @@ import "github.com/gogf/gf/net/ghttp"
// SetServerGraceful enables/disables graceful reload feature of http Web Server.
// This feature is disabled in default.
// Deprecated, use configuration of ghttp.Server for controlling this feature.
func SetServerGraceful(enabled bool) {
ghttp.SetGraceful(enabled)
}

View File

@ -5,41 +5,15 @@
// You can obtain one at https://github.com/gogf/gf.
// Package gins provides instances and core components management.
//
// Note that it should not used glog.Panic* functions for panics if you do not want
// to log all the panics.
package gins
import (
"fmt"
"github.com/gogf/gf/internal/intlog"
"github.com/gogf/gf/net/ghttp"
"github.com/gogf/gf/text/gstr"
"github.com/gogf/gf/util/gutil"
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/container/gmap"
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/database/gredis"
"github.com/gogf/gf/i18n/gi18n"
"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/util/gconv"
)
const (
gFRAME_CORE_COMPONENT_NAME_REDIS = "gf.core.component.redis"
gFRAME_CORE_COMPONENT_NAME_LOGGER = "gf.core.component.logger"
gFRAME_CORE_COMPONENT_NAME_SERVER = "gf.core.component.server"
gFRAME_CORE_COMPONENT_NAME_VIEWER = "gf.core.component.viewer"
gFRAME_CORE_COMPONENT_NAME_DATABASE = "gf.core.component.database"
gLOGGER_NODE_NAME = "logger"
gVIEWER_NODE_NAME = "viewer"
)
var (
@ -86,243 +60,7 @@ func SetIfNotExist(name string, instance interface{}) bool {
return instances.SetIfNotExist(name, instance)
}
// View returns an instance of View with default settings.
// The parameter <name> is the name for the instance.
func View(name ...string) *gview.View {
instanceName := gview.DEFAULT_NAME
if len(name) > 0 && name[0] != "" {
instanceName = name[0]
}
instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_VIEWER, instanceName)
return instances.GetOrSetFuncLock(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
}).(*gview.View)
}
// Config returns an instance of View with default settings.
// The parameter <name> is the name for the instance.
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...)
}
// I18n returns an instance of gi18n.Manager.
// The parameter <name> is the name for the instance.
func I18n(name ...string) *gi18n.Manager {
return gi18n.Instance(name...)
}
// Log returns an instance of glog.Logger.
// The parameter <name> is the name for the instance.
func Log(name ...string) *glog.Logger {
instanceName := glog.DEFAULT_NAME
if len(name) > 0 && name[0] != "" {
instanceName = name[0]
}
instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_LOGGER, instanceName)
return instances.GetOrSetFuncLock(instanceKey, func() interface{} {
logger := glog.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`, gLOGGER_NODE_NAME, instanceName)); m == nil {
// If the configuration for the instance does not exist,
// it uses the default logging configuration.
m = Config().GetMap(gLOGGER_NODE_NAME)
}
if m != nil {
if err := logger.SetConfigWithMap(m); err != nil {
panic(err)
}
}
}
return logger
}).(*glog.Logger)
}
// Database returns an instance of database ORM object
// with specified configuration group name.
func Database(name ...string) gdb.DB {
config := Config()
group := gdb.DEFAULT_GROUP_NAME
if len(name) > 0 && name[0] != "" {
group = name[0]
}
instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_DATABASE, group)
db := instances.GetOrSetFuncLock(instanceKey, func() interface{} {
// Configuration already exists.
if gdb.GetConfig(group) != nil {
db, err := gdb.Instance(group)
if err != nil {
panic(err)
}
return db
}
m := config.GetMap("database")
if m == nil {
panic(`database init failed: "database" node not found, is config file or configuration missing?`)
}
// Parse <m> as map-slice and adds it to gdb's global configurations.
for group, groupConfig := range m {
cg := gdb.ConfigGroup{}
switch value := groupConfig.(type) {
case []interface{}:
for _, v := range value {
if node := parseDBConfigNode(v); node != nil {
cg = append(cg, *node)
}
}
case map[string]interface{}:
if node := parseDBConfigNode(value); node != nil {
cg = append(cg, *node)
}
}
if len(cg) > 0 {
gdb.SetConfigGroup(group, cg)
}
}
// Parse <m> as a single node configuration,
// which is the default group configuration.
if node := parseDBConfigNode(m); node != nil {
cg := gdb.ConfigGroup{}
if node.LinkInfo != "" || node.Host != "" {
cg = append(cg, *node)
}
if len(cg) > 0 {
gdb.SetConfigGroup(gdb.DEFAULT_GROUP_NAME, cg)
}
}
addConfigMonitor(instanceKey, config)
if db, err := gdb.New(name...); err == nil {
// Initialize logger for ORM.
m := config.GetMap(fmt.Sprintf("database.%s", gLOGGER_NODE_NAME))
if m == nil {
m = config.GetMap(gLOGGER_NODE_NAME)
}
if m != nil {
if err := db.GetLogger().SetConfigWithMap(m); err != nil {
panic(err)
}
}
return db
} else {
panic(err)
}
return nil
})
if db != nil {
return db.(gdb.DB)
}
return nil
}
func parseDBConfigNode(value interface{}) *gdb.ConfigNode {
nodeMap, ok := value.(map[string]interface{})
if !ok {
return nil
}
node := &gdb.ConfigNode{}
err := gconv.Struct(nodeMap, node)
if err != nil {
panic(err)
}
if _, v := gutil.MapPossibleItemByKey(nodeMap, "link"); v != nil {
node.LinkInfo = gconv.String(v)
}
// Parse link syntax.
if node.LinkInfo != "" && node.Type == "" {
match, _ := gregex.MatchString(`([a-z]+):(.+)`, node.LinkInfo)
if len(match) == 3 {
node.Type = gstr.Trim(match[1])
node.LinkInfo = gstr.Trim(match[2])
}
}
return node
}
// Redis returns an instance of redis client with specified configuration group name.
func Redis(name ...string) *gredis.Redis {
config := Config()
group := gredis.DEFAULT_GROUP_NAME
if len(name) > 0 && name[0] != "" {
group = name[0]
}
instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_REDIS, group)
result := instances.GetOrSetFuncLock(instanceKey, func() interface{} {
// If already configured, it returns the redis instance.
if _, ok := gredis.GetConfig(group); ok {
return gredis.Instance(group)
}
// Or else, it parses the default configuration file and returns a new redis instance.
if m := config.GetMap("redis"); m != nil {
if v, ok := m[group]; ok {
redisConfig, err := gredis.ConfigFromStr(gconv.String(v))
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))
}
} else {
panic(fmt.Sprintf(`incomplete configuration for redis: "redis" node not found in config file "%s"`, config.FilePath()))
}
return nil
})
if result != nil {
return result.(*gredis.Redis)
}
return nil
}
// 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.GetOrSetFuncLock(instanceKey, func() interface{} {
s := ghttp.GetServer(name...)
// 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(`server.%s`, s.GetName())); m == nil {
// If the configuration for the instance does not exist,
// it uses the default server configuration.
m = Config().GetMap("server")
}
if m != nil {
if err := s.SetConfigWithMap(m); err != nil {
panic(err)
}
}
}
return s
}).(*ghttp.Server)
}
// 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) {

17
frame/gins/gins_config.go Normal file
View File

@ -0,0 +1,17 @@
// Copyright 2019 gf Author(https://github.com/gogf/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://github.com/gogf/gf.
package gins
import (
"github.com/gogf/gf/os/gcfg"
)
// Config returns an instance of View with default settings.
// The parameter <name> is the name for the instance.
func Config(name ...string) *gcfg.Config {
return gcfg.Instance(name...)
}

122
frame/gins/gins_database.go Normal file
View File

@ -0,0 +1,122 @@
// Copyright 2019 gf Author(https://github.com/gogf/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://github.com/gogf/gf.
package gins
import (
"fmt"
"github.com/gogf/gf/text/gstr"
"github.com/gogf/gf/util/gutil"
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/text/gregex"
"github.com/gogf/gf/util/gconv"
)
const (
gFRAME_CORE_COMPONENT_NAME_DATABASE = "gf.core.component.database"
)
// Database returns an instance of database ORM object
// with specified configuration group name.
func Database(name ...string) gdb.DB {
config := Config()
group := gdb.DEFAULT_GROUP_NAME
if len(name) > 0 && name[0] != "" {
group = name[0]
}
instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_DATABASE, group)
db := instances.GetOrSetFuncLock(instanceKey, func() interface{} {
// Configuration already exists.
if gdb.GetConfig(group) != nil {
db, err := gdb.Instance(group)
if err != nil {
panic(err)
}
return db
}
m := config.GetMap("database")
if m == nil {
panic(`database init failed: "database" node not found, is config file or configuration missing?`)
}
// Parse <m> as map-slice and adds it to gdb's global configurations.
for group, groupConfig := range m {
cg := gdb.ConfigGroup{}
switch value := groupConfig.(type) {
case []interface{}:
for _, v := range value {
if node := parseDBConfigNode(v); node != nil {
cg = append(cg, *node)
}
}
case map[string]interface{}:
if node := parseDBConfigNode(value); node != nil {
cg = append(cg, *node)
}
}
if len(cg) > 0 {
gdb.SetConfigGroup(group, cg)
}
}
// Parse <m> as a single node configuration,
// which is the default group configuration.
if node := parseDBConfigNode(m); node != nil {
cg := gdb.ConfigGroup{}
if node.LinkInfo != "" || node.Host != "" {
cg = append(cg, *node)
}
if len(cg) > 0 {
gdb.SetConfigGroup(gdb.DEFAULT_GROUP_NAME, cg)
}
}
addConfigMonitor(instanceKey, config)
if db, err := gdb.New(name...); err == nil {
// Initialize logger for ORM.
m := config.GetMap(fmt.Sprintf("database.%s", gLOGGER_NODE_NAME))
if m == nil {
m = config.GetMap(gLOGGER_NODE_NAME)
}
if m != nil {
if err := db.GetLogger().SetConfigWithMap(m); err != nil {
panic(err)
}
}
return db
} else {
panic(err)
}
return nil
})
if db != nil {
return db.(gdb.DB)
}
return nil
}
func parseDBConfigNode(value interface{}) *gdb.ConfigNode {
nodeMap, ok := value.(map[string]interface{})
if !ok {
return nil
}
node := &gdb.ConfigNode{}
err := gconv.Struct(nodeMap, node)
if err != nil {
panic(err)
}
if _, v := gutil.MapPossibleItemByKey(nodeMap, "link"); v != nil {
node.LinkInfo = gconv.String(v)
}
// Parse link syntax.
if node.LinkInfo != "" && node.Type == "" {
match, _ := gregex.MatchString(`([a-z]+):(.+)`, node.LinkInfo)
if len(match) == 3 {
node.Type = gstr.Trim(match[1])
node.LinkInfo = gstr.Trim(match[2])
}
}
return node
}

17
frame/gins/gins_i18n.go Normal file
View File

@ -0,0 +1,17 @@
// Copyright 2019 gf Author(https://github.com/gogf/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://github.com/gogf/gf.
package gins
import (
"github.com/gogf/gf/i18n/gi18n"
)
// I18n returns an instance of gi18n.Manager.
// The parameter <name> is the name for the instance.
func I18n(name ...string) *gi18n.Manager {
return gi18n.Instance(name...)
}

46
frame/gins/gins_log.go Normal file
View File

@ -0,0 +1,46 @@
// Copyright 2019 gf Author(https://github.com/gogf/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://github.com/gogf/gf.
package gins
import (
"fmt"
"github.com/gogf/gf/os/glog"
)
const (
gFRAME_CORE_COMPONENT_NAME_LOGGER = "gf.core.component.logger"
gLOGGER_NODE_NAME = "logger"
)
// Log returns an instance of glog.Logger.
// The parameter <name> is the name for the instance.
func Log(name ...string) *glog.Logger {
instanceName := glog.DEFAULT_NAME
if len(name) > 0 && name[0] != "" {
instanceName = name[0]
}
instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_LOGGER, instanceName)
return instances.GetOrSetFuncLock(instanceKey, func() interface{} {
logger := glog.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`, gLOGGER_NODE_NAME, instanceName)); m == nil {
// If the configuration for the instance does not exist,
// it uses the default logging configuration.
m = Config().GetMap(gLOGGER_NODE_NAME)
}
if m != nil {
if err := logger.SetConfigWithMap(m); err != nil {
panic(err)
}
}
}
return logger
}).(*glog.Logger)
}

53
frame/gins/gins_redis.go Normal file
View File

@ -0,0 +1,53 @@
// Copyright 2019 gf Author(https://github.com/gogf/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://github.com/gogf/gf.
package gins
import (
"fmt"
"github.com/gogf/gf/database/gredis"
"github.com/gogf/gf/util/gconv"
)
const (
gFRAME_CORE_COMPONENT_NAME_REDIS = "gf.core.component.redis"
)
// Redis returns an instance of redis client with specified configuration group name.
func Redis(name ...string) *gredis.Redis {
config := Config()
group := gredis.DEFAULT_GROUP_NAME
if len(name) > 0 && name[0] != "" {
group = name[0]
}
instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_REDIS, group)
result := instances.GetOrSetFuncLock(instanceKey, func() interface{} {
// If already configured, it returns the redis instance.
if _, ok := gredis.GetConfig(group); ok {
return gredis.Instance(group)
}
// Or else, it parses the default configuration file and returns a new redis instance.
if m := config.GetMap("redis"); m != nil {
if v, ok := m[group]; ok {
redisConfig, err := gredis.ConfigFromStr(gconv.String(v))
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))
}
} else {
panic(fmt.Sprintf(`incomplete configuration for redis: "redis" node not found in config file "%s"`, config.FilePath()))
}
return nil
})
if result != nil {
return result.(*gredis.Redis)
}
return nil
}

View File

@ -0,0 +1,17 @@
// Copyright 2019 gf Author(https://github.com/gogf/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://github.com/gogf/gf.
package gins
import (
"github.com/gogf/gf/os/gres"
)
// 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...)
}

40
frame/gins/gins_server.go Normal file
View File

@ -0,0 +1,40 @@
// Copyright 2019 gf Author(https://github.com/gogf/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://github.com/gogf/gf.
package gins
import (
"fmt"
"github.com/gogf/gf/net/ghttp"
)
const (
gFRAME_CORE_COMPONENT_NAME_SERVER = "gf.core.component.server"
)
// 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.GetOrSetFuncLock(instanceKey, func() interface{} {
s := ghttp.GetServer(name...)
// 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(`server.%s`, s.GetName())); m == nil {
// If the configuration for the instance does not exist,
// it uses the default server configuration.
m = Config().GetMap("server")
}
if m != nil {
if err := s.SetConfigWithMap(m); err != nil {
panic(err)
}
}
}
return s
}).(*ghttp.Server)
}

46
frame/gins/gins_view.go Normal file
View File

@ -0,0 +1,46 @@
// Copyright 2019 gf Author(https://github.com/gogf/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://github.com/gogf/gf.
package gins
import (
"fmt"
"github.com/gogf/gf/os/gview"
)
const (
gFRAME_CORE_COMPONENT_NAME_VIEWER = "gf.core.component.viewer"
gVIEWER_NODE_NAME = "viewer"
)
// View returns an instance of View with default settings.
// The parameter <name> is the name for the instance.
func View(name ...string) *gview.View {
instanceName := gview.DEFAULT_NAME
if len(name) > 0 && name[0] != "" {
instanceName = name[0]
}
instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_VIEWER, instanceName)
return instances.GetOrSetFuncLock(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
}).(*gview.View)
}

View File

@ -72,7 +72,7 @@ func IsEmpty(value interface{}) bool {
}
// IsNil checks whether given <value> is nil.
// Note that it's using reflect feature which affects performance a little bit.
// Note that it might use reflect feature which affects performance a little bit.
func IsNil(value interface{}) bool {
if value == nil {
return true

View File

@ -42,7 +42,7 @@ func Print(v ...interface{}) {
if !isGFDebug {
return
}
fmt.Println(append([]interface{}{now(), "[INTE]", file()}, v...)...)
fmt.Println(append([]interface{}{now(), "[GF]", file()}, v...)...)
}
// Printf prints <v> with format <format> using fmt.Printf.
@ -51,7 +51,7 @@ func Printf(format string, v ...interface{}) {
if !isGFDebug {
return
}
fmt.Printf(now()+" [INTE] "+file()+" "+format+"\n", v...)
fmt.Printf(now()+" [GF] "+file()+" "+format+"\n", v...)
}
// Error prints <v> with newline using fmt.Println.
@ -60,7 +60,7 @@ func Error(v ...interface{}) {
if !isGFDebug {
return
}
array := append([]interface{}{now(), "[INTE]", file()}, v...)
array := append([]interface{}{now(), "[GF]", file()}, v...)
array = append(array, "\n"+gdebug.StackWithFilter(gFILTER_KEY))
fmt.Println(array...)
}
@ -71,7 +71,7 @@ func Errorf(format string, v ...interface{}) {
return
}
fmt.Printf(
now()+" [INTE] "+file()+" "+format+"\n%s\n",
now()+" [GF] "+file()+" "+format+"\n%s\n",
append(v, gdebug.StackWithFilter(gFILTER_KEY))...,
)
}