mirror of
https://github.com/gogf/gf.git
synced 2025-04-05 11:18:50 +08:00
add internal package consts to manage shared constants; improve buildin function dump only available in develop mode for package gview
This commit is contained in:
parent
878dbe4ab9
commit
bf674060c0
@ -13,6 +13,7 @@ import (
|
|||||||
"github.com/gogf/gf/v2/database/gdb"
|
"github.com/gogf/gf/v2/database/gdb"
|
||||||
"github.com/gogf/gf/v2/errors/gcode"
|
"github.com/gogf/gf/v2/errors/gcode"
|
||||||
"github.com/gogf/gf/v2/errors/gerror"
|
"github.com/gogf/gf/v2/errors/gerror"
|
||||||
|
"github.com/gogf/gf/v2/internal/consts"
|
||||||
"github.com/gogf/gf/v2/internal/intlog"
|
"github.com/gogf/gf/v2/internal/intlog"
|
||||||
"github.com/gogf/gf/v2/os/gcfg"
|
"github.com/gogf/gf/v2/os/gcfg"
|
||||||
"github.com/gogf/gf/v2/text/gregex"
|
"github.com/gogf/gf/v2/text/gregex"
|
||||||
@ -23,7 +24,6 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
frameCoreComponentNameDatabase = "gf.core.component.database"
|
frameCoreComponentNameDatabase = "gf.core.component.database"
|
||||||
configNodeNameDatabase = "database"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Database returns an instance of database ORM object with specified configuration group name.
|
// Database returns an instance of database ORM object with specified configuration group name.
|
||||||
@ -42,11 +42,11 @@ func Database(name ...string) gdb.DB {
|
|||||||
// It ignores returned error to avoid file no found error while it's not necessary.
|
// It ignores returned error to avoid file no found error while it's not necessary.
|
||||||
var (
|
var (
|
||||||
configMap map[string]interface{}
|
configMap map[string]interface{}
|
||||||
configNodeKey = configNodeNameDatabase
|
configNodeKey = consts.ConfigNodeNameDatabase
|
||||||
)
|
)
|
||||||
// It firstly searches the configuration of the instance name.
|
// It firstly searches the configuration of the instance name.
|
||||||
if configData, _ := Config().Data(ctx); len(configData) > 0 {
|
if configData, _ := Config().Data(ctx); len(configData) > 0 {
|
||||||
if v, _ := gutil.MapPossibleItemByKey(configData, configNodeNameDatabase); v != "" {
|
if v, _ := gutil.MapPossibleItemByKey(configData, consts.ConfigNodeNameDatabase); v != "" {
|
||||||
configNodeKey = v
|
configNodeKey = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -89,7 +89,7 @@ func Database(name ...string) gdb.DB {
|
|||||||
err = gerror.NewCodef(
|
err = gerror.NewCodef(
|
||||||
gcode.CodeMissingConfiguration,
|
gcode.CodeMissingConfiguration,
|
||||||
`database initialization failed: "%s" node not found, is configuration file or configuration node missing?`,
|
`database initialization failed: "%s" node not found, is configuration file or configuration node missing?`,
|
||||||
configNodeNameDatabase,
|
consts.ConfigNodeNameDatabase,
|
||||||
)
|
)
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -130,13 +130,16 @@ func Database(name ...string) gdb.DB {
|
|||||||
if node.Link != "" || node.Host != "" {
|
if node.Link != "" || node.Host != "" {
|
||||||
cg = append(cg, *node)
|
cg = append(cg, *node)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(cg) > 0 {
|
if len(cg) > 0 {
|
||||||
if gdb.GetConfig(group) == nil {
|
if gdb.GetConfig(group) == nil {
|
||||||
intlog.Printf(ctx, "add configuration for group: %s, %#v", gdb.DefaultGroupName, cg)
|
intlog.Printf(ctx, "add configuration for group: %s, %#v", gdb.DefaultGroupName, cg)
|
||||||
gdb.SetConfigGroup(gdb.DefaultGroupName, cg)
|
gdb.SetConfigGroup(gdb.DefaultGroupName, cg)
|
||||||
} else {
|
} else {
|
||||||
intlog.Printf(ctx, "ignore configuration as it already exists for group: %s, %#v", gdb.DefaultGroupName, cg)
|
intlog.Printf(
|
||||||
|
ctx,
|
||||||
|
"ignore configuration as it already exists for group: %s, %#v",
|
||||||
|
gdb.DefaultGroupName, cg,
|
||||||
|
)
|
||||||
intlog.Printf(ctx, "%s, %#v", gdb.DefaultGroupName, cg)
|
intlog.Printf(ctx, "%s, %#v", gdb.DefaultGroupName, cg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -147,7 +150,7 @@ func Database(name ...string) gdb.DB {
|
|||||||
// Initialize logger for ORM.
|
// Initialize logger for ORM.
|
||||||
var (
|
var (
|
||||||
loggerConfigMap map[string]interface{}
|
loggerConfigMap map[string]interface{}
|
||||||
loggerNodeName = fmt.Sprintf("%s.%s", configNodeKey, configNodeNameLogger)
|
loggerNodeName = fmt.Sprintf("%s.%s", configNodeKey, consts.ConfigNodeNameLogger)
|
||||||
)
|
)
|
||||||
if v, _ := Config().Get(ctx, loggerNodeName); !v.IsEmpty() {
|
if v, _ := Config().Get(ctx, loggerNodeName); !v.IsEmpty() {
|
||||||
loggerConfigMap = v.Map()
|
loggerConfigMap = v.Map()
|
||||||
|
@ -10,13 +10,13 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/gogf/gf/v2/internal/consts"
|
||||||
"github.com/gogf/gf/v2/os/glog"
|
"github.com/gogf/gf/v2/os/glog"
|
||||||
"github.com/gogf/gf/v2/util/gutil"
|
"github.com/gogf/gf/v2/util/gutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
frameCoreComponentNameLogger = "gf.core.component.logger"
|
frameCoreComponentNameLogger = "gf.core.component.logger"
|
||||||
configNodeNameLogger = "logger"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Log returns an instance of glog.Logger.
|
// Log returns an instance of glog.Logger.
|
||||||
@ -36,11 +36,11 @@ func Log(name ...string) *glog.Logger {
|
|||||||
// To avoid file no found error while it's not necessary.
|
// To avoid file no found error while it's not necessary.
|
||||||
var (
|
var (
|
||||||
configMap map[string]interface{}
|
configMap map[string]interface{}
|
||||||
loggerNodeName = configNodeNameLogger
|
loggerNodeName = consts.ConfigNodeNameLogger
|
||||||
)
|
)
|
||||||
// Try to find possible `loggerNodeName` in case-insensitive way.
|
// Try to find possible `loggerNodeName` in case-insensitive way.
|
||||||
if configData, _ := Config().Data(ctx); len(configData) > 0 {
|
if configData, _ := Config().Data(ctx); len(configData) > 0 {
|
||||||
if v, _ := gutil.MapPossibleItemByKey(configData, configNodeNameLogger); v != "" {
|
if v, _ := gutil.MapPossibleItemByKey(configData, consts.ConfigNodeNameLogger); v != "" {
|
||||||
loggerNodeName = v
|
loggerNodeName = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/gogf/gf/v2/database/gredis"
|
"github.com/gogf/gf/v2/database/gredis"
|
||||||
|
"github.com/gogf/gf/v2/internal/consts"
|
||||||
"github.com/gogf/gf/v2/internal/intlog"
|
"github.com/gogf/gf/v2/internal/intlog"
|
||||||
"github.com/gogf/gf/v2/util/gconv"
|
"github.com/gogf/gf/v2/util/gconv"
|
||||||
"github.com/gogf/gf/v2/util/gutil"
|
"github.com/gogf/gf/v2/util/gutil"
|
||||||
@ -18,7 +19,6 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
frameCoreComponentNameRedis = "gf.core.component.redis"
|
frameCoreComponentNameRedis = "gf.core.component.redis"
|
||||||
configNodeNameRedis = "redis"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Redis returns an instance of redis client with specified configuration group name.
|
// Redis returns an instance of redis client with specified configuration group name.
|
||||||
@ -47,7 +47,7 @@ func Redis(name ...string) *gredis.Redis {
|
|||||||
if configMap, err = Config().Data(ctx); err != nil {
|
if configMap, err = Config().Data(ctx); err != nil {
|
||||||
intlog.Errorf(ctx, `retrieve config data map failed: %+v`, err)
|
intlog.Errorf(ctx, `retrieve config data map failed: %+v`, err)
|
||||||
}
|
}
|
||||||
if _, v := gutil.MapPossibleItemByKey(configMap, configNodeNameRedis); v != nil {
|
if _, v := gutil.MapPossibleItemByKey(configMap, consts.ConfigNodeNameRedis); v != nil {
|
||||||
configMap = gconv.Map(v)
|
configMap = gconv.Map(v)
|
||||||
}
|
}
|
||||||
if len(configMap) > 0 {
|
if len(configMap) > 0 {
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/gogf/gf/v2/internal/consts"
|
||||||
"github.com/gogf/gf/v2/internal/intlog"
|
"github.com/gogf/gf/v2/internal/intlog"
|
||||||
"github.com/gogf/gf/v2/net/ghttp"
|
"github.com/gogf/gf/v2/net/ghttp"
|
||||||
"github.com/gogf/gf/v2/util/gconv"
|
"github.com/gogf/gf/v2/util/gconv"
|
||||||
@ -17,9 +18,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
frameCoreComponentNameServer = "gf.core.component.server" // Prefix for HTTP server instance.
|
frameCoreComponentNameServer = "gf.core.component.server" // Prefix for HTTP server instance.
|
||||||
configNodeNameServer = "server" // General version configuration item name.
|
|
||||||
configNodeNameServerSecondary = "httpserver" // New version configuration item name support from v2.
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Server returns an instance of http server with specified name.
|
// Server returns an instance of http server with specified name.
|
||||||
@ -49,11 +49,11 @@ func Server(name ...interface{}) *ghttp.Server {
|
|||||||
}
|
}
|
||||||
// Find possible server configuration item by possible names.
|
// Find possible server configuration item by possible names.
|
||||||
if len(configMap) > 0 {
|
if len(configMap) > 0 {
|
||||||
if v, _ := gutil.MapPossibleItemByKey(configMap, configNodeNameServer); v != "" {
|
if v, _ := gutil.MapPossibleItemByKey(configMap, consts.ConfigNodeNameServer); v != "" {
|
||||||
configNodeName = v
|
configNodeName = v
|
||||||
}
|
}
|
||||||
if configNodeName == "" {
|
if configNodeName == "" {
|
||||||
if v, _ := gutil.MapPossibleItemByKey(configMap, configNodeNameServerSecondary); v != "" {
|
if v, _ := gutil.MapPossibleItemByKey(configMap, consts.ConfigNodeNameServerSecondary); v != "" {
|
||||||
configNodeName = v
|
configNodeName = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -81,7 +81,7 @@ func Server(name ...interface{}) *ghttp.Server {
|
|||||||
// Server logger configuration checks.
|
// Server logger configuration checks.
|
||||||
serverLoggerConfigMap = Config().MustGet(
|
serverLoggerConfigMap = Config().MustGet(
|
||||||
ctx,
|
ctx,
|
||||||
fmt.Sprintf(`%s.%s.%s`, configNodeName, instanceName, configNodeNameLogger),
|
fmt.Sprintf(`%s.%s.%s`, configNodeName, instanceName, consts.ConfigNodeNameLogger),
|
||||||
).Map()
|
).Map()
|
||||||
if len(serverLoggerConfigMap) > 0 {
|
if len(serverLoggerConfigMap) > 0 {
|
||||||
if err = server.Logger().SetConfigWithMap(serverLoggerConfigMap); err != nil {
|
if err = server.Logger().SetConfigWithMap(serverLoggerConfigMap); err != nil {
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/gogf/gf/v2/internal/consts"
|
||||||
"github.com/gogf/gf/v2/internal/intlog"
|
"github.com/gogf/gf/v2/internal/intlog"
|
||||||
"github.com/gogf/gf/v2/os/gview"
|
"github.com/gogf/gf/v2/os/gview"
|
||||||
"github.com/gogf/gf/v2/util/gutil"
|
"github.com/gogf/gf/v2/util/gutil"
|
||||||
@ -17,7 +18,6 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
frameCoreComponentNameViewer = "gf.core.component.viewer"
|
frameCoreComponentNameViewer = "gf.core.component.viewer"
|
||||||
configNodeNameViewer = "viewer"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// View returns an instance of View with default settings.
|
// View returns an instance of View with default settings.
|
||||||
@ -47,13 +47,13 @@ func getViewInstance(name ...string) *gview.View {
|
|||||||
if Config().Available(ctx) {
|
if Config().Available(ctx) {
|
||||||
var (
|
var (
|
||||||
configMap map[string]interface{}
|
configMap map[string]interface{}
|
||||||
configNodeName = configNodeNameViewer
|
configNodeName = consts.ConfigNodeNameViewer
|
||||||
)
|
)
|
||||||
if configMap, err = Config().Data(ctx); err != nil {
|
if configMap, err = Config().Data(ctx); err != nil {
|
||||||
intlog.Errorf(ctx, `retrieve config data map failed: %+v`, err)
|
intlog.Errorf(ctx, `retrieve config data map failed: %+v`, err)
|
||||||
}
|
}
|
||||||
if len(configMap) > 0 {
|
if len(configMap) > 0 {
|
||||||
if v, _ := gutil.MapPossibleItemByKey(configMap, configNodeNameViewer); v != "" {
|
if v, _ := gutil.MapPossibleItemByKey(configMap, consts.ConfigNodeNameViewer); v != "" {
|
||||||
configNodeName = v
|
configNodeName = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
17
internal/consts/consts.go
Normal file
17
internal/consts/consts.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
// Copyright GoFrame gf Author(https://goframe.org). 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 consts defines constants that are shared all among packages of framework.
|
||||||
|
package consts
|
||||||
|
|
||||||
|
const (
|
||||||
|
ConfigNodeNameDatabase = "database"
|
||||||
|
ConfigNodeNameLogger = "logger"
|
||||||
|
ConfigNodeNameRedis = "redis"
|
||||||
|
ConfigNodeNameViewer = "viewer"
|
||||||
|
ConfigNodeNameServer = "server" // General version configuration item name.
|
||||||
|
ConfigNodeNameServerSecondary = "httpserver" // New version configuration item name support from v2.
|
||||||
|
)
|
@ -19,6 +19,7 @@ import (
|
|||||||
"github.com/gogf/gf/v2/os/gtime"
|
"github.com/gogf/gf/v2/os/gtime"
|
||||||
"github.com/gogf/gf/v2/text/gstr"
|
"github.com/gogf/gf/v2/text/gstr"
|
||||||
"github.com/gogf/gf/v2/util/gconv"
|
"github.com/gogf/gf/v2/util/gconv"
|
||||||
|
"github.com/gogf/gf/v2/util/gmode"
|
||||||
"github.com/gogf/gf/v2/util/gutil"
|
"github.com/gogf/gf/v2/util/gutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -27,9 +28,13 @@ func (view *View) buildInFuncDump(values ...interface{}) string {
|
|||||||
buffer := bytes.NewBuffer(nil)
|
buffer := bytes.NewBuffer(nil)
|
||||||
buffer.WriteString("\n")
|
buffer.WriteString("\n")
|
||||||
buffer.WriteString("<!--\n")
|
buffer.WriteString("<!--\n")
|
||||||
for _, v := range values {
|
if gmode.IsDevelop() {
|
||||||
gutil.DumpTo(buffer, v, gutil.DumpOption{})
|
for _, v := range values {
|
||||||
buffer.WriteString("\n")
|
gutil.DumpTo(buffer, v, gutil.DumpOption{})
|
||||||
|
buffer.WriteString("\n")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
buffer.WriteString("dump feature is disabled as server is not running in develop mode\n")
|
||||||
}
|
}
|
||||||
buffer.WriteString("-->\n")
|
buffer.WriteString("-->\n")
|
||||||
return buffer.String()
|
return buffer.String()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user