mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 03:07:45 +08:00
ae4f14c2e2
* improve logging feature, add LevelPrint configuration for glog.Logger; add package internal/instance * improve command build * add default logger for panic message printing if no logger set * up * fix scheduler when timer triggers in less than one second for package gcron * up
69 lines
2.0 KiB
Go
69 lines
2.0 KiB
Go
// Copyright GoFrame 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 gins
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/gogf/gf/v2/internal/consts"
|
|
"github.com/gogf/gf/v2/internal/instance"
|
|
"github.com/gogf/gf/v2/internal/intlog"
|
|
"github.com/gogf/gf/v2/os/gview"
|
|
"github.com/gogf/gf/v2/util/gutil"
|
|
)
|
|
|
|
// View returns an instance of View with default settings.
|
|
// The parameter `name` is the name for the instance.
|
|
// Note that it panics if any error occurs duration instance creating.
|
|
func View(name ...string) *gview.View {
|
|
instanceName := gview.DefaultName
|
|
if len(name) > 0 && name[0] != "" {
|
|
instanceName = name[0]
|
|
}
|
|
instanceKey := fmt.Sprintf("%s.%s", frameCoreComponentNameViewer, instanceName)
|
|
return instance.GetOrSetFuncLock(instanceKey, func() interface{} {
|
|
return getViewInstance(instanceName)
|
|
}).(*gview.View)
|
|
}
|
|
|
|
func getViewInstance(name ...string) *gview.View {
|
|
var (
|
|
err error
|
|
ctx = context.Background()
|
|
instanceName = gview.DefaultName
|
|
)
|
|
if len(name) > 0 && name[0] != "" {
|
|
instanceName = name[0]
|
|
}
|
|
view := gview.Instance(instanceName)
|
|
if Config().Available(ctx) {
|
|
var (
|
|
configMap map[string]interface{}
|
|
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, consts.ConfigNodeNameViewer); v != "" {
|
|
configNodeName = v
|
|
}
|
|
}
|
|
configMap = Config().MustGet(ctx, fmt.Sprintf(`%s.%s`, configNodeName, instanceName)).Map()
|
|
if len(configMap) == 0 {
|
|
configMap = Config().MustGet(ctx, configNodeName).Map()
|
|
}
|
|
if len(configMap) > 0 {
|
|
if err = view.SetConfigWithMap(configMap); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
}
|
|
return view
|
|
}
|