1
0
mirror of https://github.com/gogf/gf.git synced 2025-04-05 03:05:05 +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:
John Guo 2022-04-13 21:08:12 +08:00
parent 878dbe4ab9
commit bf674060c0
7 changed files with 49 additions and 24 deletions

View File

@ -13,6 +13,7 @@ import (
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gcode"
"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/os/gcfg"
"github.com/gogf/gf/v2/text/gregex"
@ -23,7 +24,6 @@ import (
const (
frameCoreComponentNameDatabase = "gf.core.component.database"
configNodeNameDatabase = "database"
)
// 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.
var (
configMap map[string]interface{}
configNodeKey = configNodeNameDatabase
configNodeKey = consts.ConfigNodeNameDatabase
)
// It firstly searches the configuration of the instance name.
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
}
}
@ -89,7 +89,7 @@ func Database(name ...string) gdb.DB {
err = gerror.NewCodef(
gcode.CodeMissingConfiguration,
`database initialization failed: "%s" node not found, is configuration file or configuration node missing?`,
configNodeNameDatabase,
consts.ConfigNodeNameDatabase,
)
panic(err)
}
@ -130,13 +130,16 @@ func Database(name ...string) gdb.DB {
if node.Link != "" || node.Host != "" {
cg = append(cg, *node)
}
if len(cg) > 0 {
if gdb.GetConfig(group) == nil {
intlog.Printf(ctx, "add configuration for group: %s, %#v", gdb.DefaultGroupName, cg)
gdb.SetConfigGroup(gdb.DefaultGroupName, cg)
} 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)
}
}
@ -147,7 +150,7 @@ func Database(name ...string) gdb.DB {
// Initialize logger for ORM.
var (
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() {
loggerConfigMap = v.Map()

View File

@ -10,13 +10,13 @@ import (
"context"
"fmt"
"github.com/gogf/gf/v2/internal/consts"
"github.com/gogf/gf/v2/os/glog"
"github.com/gogf/gf/v2/util/gutil"
)
const (
frameCoreComponentNameLogger = "gf.core.component.logger"
configNodeNameLogger = "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.
var (
configMap map[string]interface{}
loggerNodeName = configNodeNameLogger
loggerNodeName = consts.ConfigNodeNameLogger
)
// Try to find possible `loggerNodeName` in case-insensitive way.
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
}
}

View File

@ -11,6 +11,7 @@ import (
"fmt"
"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/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
@ -18,7 +19,6 @@ import (
const (
frameCoreComponentNameRedis = "gf.core.component.redis"
configNodeNameRedis = "redis"
)
// 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 {
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)
}
if len(configMap) > 0 {

View File

@ -10,6 +10,7 @@ import (
"context"
"fmt"
"github.com/gogf/gf/v2/internal/consts"
"github.com/gogf/gf/v2/internal/intlog"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/util/gconv"
@ -17,9 +18,8 @@ import (
)
const (
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.
frameCoreComponentNameServer = "gf.core.component.server" // Prefix for HTTP server instance.
)
// 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.
if len(configMap) > 0 {
if v, _ := gutil.MapPossibleItemByKey(configMap, configNodeNameServer); v != "" {
if v, _ := gutil.MapPossibleItemByKey(configMap, consts.ConfigNodeNameServer); v != "" {
configNodeName = v
}
if configNodeName == "" {
if v, _ := gutil.MapPossibleItemByKey(configMap, configNodeNameServerSecondary); v != "" {
if v, _ := gutil.MapPossibleItemByKey(configMap, consts.ConfigNodeNameServerSecondary); v != "" {
configNodeName = v
}
}
@ -81,7 +81,7 @@ func Server(name ...interface{}) *ghttp.Server {
// Server logger configuration checks.
serverLoggerConfigMap = Config().MustGet(
ctx,
fmt.Sprintf(`%s.%s.%s`, configNodeName, instanceName, configNodeNameLogger),
fmt.Sprintf(`%s.%s.%s`, configNodeName, instanceName, consts.ConfigNodeNameLogger),
).Map()
if len(serverLoggerConfigMap) > 0 {
if err = server.Logger().SetConfigWithMap(serverLoggerConfigMap); err != nil {

View File

@ -10,6 +10,7 @@ import (
"context"
"fmt"
"github.com/gogf/gf/v2/internal/consts"
"github.com/gogf/gf/v2/internal/intlog"
"github.com/gogf/gf/v2/os/gview"
"github.com/gogf/gf/v2/util/gutil"
@ -17,7 +18,6 @@ import (
const (
frameCoreComponentNameViewer = "gf.core.component.viewer"
configNodeNameViewer = "viewer"
)
// View returns an instance of View with default settings.
@ -47,13 +47,13 @@ func getViewInstance(name ...string) *gview.View {
if Config().Available(ctx) {
var (
configMap map[string]interface{}
configNodeName = configNodeNameViewer
configNodeName = consts.ConfigNodeNameViewer
)
if configMap, err = Config().Data(ctx); err != nil {
intlog.Errorf(ctx, `retrieve config data map failed: %+v`, err)
}
if len(configMap) > 0 {
if v, _ := gutil.MapPossibleItemByKey(configMap, configNodeNameViewer); v != "" {
if v, _ := gutil.MapPossibleItemByKey(configMap, consts.ConfigNodeNameViewer); v != "" {
configNodeName = v
}
}

17
internal/consts/consts.go Normal file
View 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.
)

View File

@ -19,6 +19,7 @@ import (
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gmode"
"github.com/gogf/gf/v2/util/gutil"
)
@ -27,9 +28,13 @@ func (view *View) buildInFuncDump(values ...interface{}) string {
buffer := bytes.NewBuffer(nil)
buffer.WriteString("\n")
buffer.WriteString("<!--\n")
for _, v := range values {
gutil.DumpTo(buffer, v, gutil.DumpOption{})
buffer.WriteString("\n")
if gmode.IsDevelop() {
for _, v := range values {
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")
return buffer.String()