gf/frame/gins/gins_view.go

69 lines
2.0 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2020-02-23 20:25:55 +08:00
//
// 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"
2020-02-23 20:25:55 +08:00
"fmt"
2021-11-13 23:23:55 +08:00
"github.com/gogf/gf/v2/internal/consts"
"github.com/gogf/gf/v2/internal/instance"
"github.com/gogf/gf/v2/internal/intlog"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/os/gview"
"github.com/gogf/gf/v2/util/gutil"
2020-02-23 20:25:55 +08:00
)
// 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.
2020-02-23 20:25:55 +08:00
func View(name ...string) *gview.View {
instanceName := gview.DefaultName
2020-02-23 20:25:55 +08:00
if len(name) > 0 && name[0] != "" {
instanceName = name[0]
}
2020-12-14 13:26:48 +08:00
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)
2020-07-10 00:20:46 +08:00
}
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)
}
2020-02-23 20:25:55 +08:00
}
}
return view
2020-02-23 20:25:55 +08:00
}