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 (
|
2021-09-19 10:01:09 +08:00
|
|
|
"context"
|
2020-02-23 20:25:55 +08:00
|
|
|
"fmt"
|
2021-11-13 23:23:55 +08:00
|
|
|
|
2022-04-13 21:08:12 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/consts"
|
2023-01-06 14:15:30 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/instance"
|
2022-02-14 11:46:20 +08:00
|
|
|
"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.
|
2021-09-16 20:57:59 +08:00
|
|
|
// The parameter `name` is the name for the instance.
|
2021-09-19 10:01:09 +08:00
|
|
|
// 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 {
|
2020-12-14 13:02:08 +08:00
|
|
|
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)
|
2023-01-06 14:15:30 +08:00
|
|
|
return instance.GetOrSetFuncLock(instanceKey, func() interface{} {
|
2020-03-10 20:13:36 +08:00
|
|
|
return getViewInstance(instanceName)
|
|
|
|
}).(*gview.View)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getViewInstance(name ...string) *gview.View {
|
2021-09-19 10:01:09 +08:00
|
|
|
var (
|
2022-02-14 11:46:20 +08:00
|
|
|
err error
|
2021-09-19 10:01:09 +08:00
|
|
|
ctx = context.Background()
|
|
|
|
instanceName = gview.DefaultName
|
|
|
|
)
|
2020-03-10 20:13:36 +08:00
|
|
|
if len(name) > 0 && name[0] != "" {
|
|
|
|
instanceName = name[0]
|
|
|
|
}
|
|
|
|
view := gview.Instance(instanceName)
|
2022-02-14 11:46:20 +08:00
|
|
|
if Config().Available(ctx) {
|
|
|
|
var (
|
|
|
|
configMap map[string]interface{}
|
2022-04-13 21:08:12 +08:00
|
|
|
configNodeName = consts.ConfigNodeNameViewer
|
2022-02-14 11:46:20 +08:00
|
|
|
)
|
|
|
|
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
|
|
|
}
|
2022-02-14 11:46:20 +08:00
|
|
|
if len(configMap) > 0 {
|
2022-04-13 21:08:12 +08:00
|
|
|
if v, _ := gutil.MapPossibleItemByKey(configMap, consts.ConfigNodeNameViewer); v != "" {
|
2022-02-14 11:46:20 +08:00
|
|
|
configNodeName = v
|
|
|
|
}
|
2020-03-10 20:13:36 +08:00
|
|
|
}
|
2022-02-14 11:46:20 +08:00
|
|
|
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
|
|
|
}
|
2020-03-10 20:13:36 +08:00
|
|
|
}
|
|
|
|
return view
|
2020-02-23 20:25:55 +08:00
|
|
|
}
|