Skip to content

Commit

Permalink
+ page
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Oct 20, 2021
1 parent 2253a98 commit 9d42324
Show file tree
Hide file tree
Showing 19 changed files with 613 additions and 11 deletions.
4 changes: 2 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func LoadFrom(dir string, prefix string) {
}

share.Walk(dir, ".json", func(root, filename string) {
name := share.SpecName(root, filename)
name := prefix + share.SpecName(root, filename)
content := share.ReadFile(filename)
gou.LoadAPI(string(content), prefix+name)
gou.LoadAPI(string(content), name)
})
}
13 changes: 12 additions & 1 deletion chart/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,24 @@ func LoadFrom(dir string, prefix string) {
}

share.Walk(dir, ".json", func(root, filename string) {
name := share.SpecName(root, filename)
name := prefix + share.SpecName(root, filename)
content := share.ReadFile(filename)
_, err := LoadChart(content, name)
if err != nil {
exception.New("%s 图表格式错误", 400, name).Ctx(filename).Throw()
}
})

// Load Script
share.Walk(dir, ".js", func(root, filename string) {
name := prefix + share.SpecName(root, filename)
chart := Select(name)
if chart != nil {
script := share.ScriptName(filename)
content := share.ReadFile(filename)
chart.LoadScript(string(content), script)
}
})
}

// LoadChart 载入数据表格
Expand Down
2 changes: 2 additions & 0 deletions engine/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/yaoapp/xiang/config"
"github.com/yaoapp/xiang/flow"
"github.com/yaoapp/xiang/model"
"github.com/yaoapp/xiang/page"
"github.com/yaoapp/xiang/plugin"
"github.com/yaoapp/xiang/query"
"github.com/yaoapp/xiang/share"
Expand All @@ -33,6 +34,7 @@ func Load(cfg config.Config) {
plugin.Load(cfg) // 加载业务插件 plugin
table.Load(cfg) // 加载数据表格 table
chart.Load(cfg) // 加载分析图表 chart
page.Load(cfg) // 加载页面 page

// 加密密钥函数
gou.LoadCrypt(fmt.Sprintf(`{"key":"%s"}`, cfg.Database.AESKey), "AES")
Expand Down
15 changes: 13 additions & 2 deletions flow/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,19 @@ func LoadFrom(dir string, prefix string) {
}

share.Walk(dir, ".json", func(root, filename string) {
name := share.SpecName(root, filename)
name := prefix + share.SpecName(root, filename)
content := share.ReadFile(filename)
gou.LoadFlow(string(content), prefix+name)
gou.LoadFlow(string(content), name)
})

// Load Script
share.Walk(dir, ".js", func(root, filename string) {
name := prefix + share.SpecName(root, filename)
flow := gou.SelectFlow(name)
if flow != nil {
script := share.ScriptName(filename)
content := share.ReadFile(filename)
flow.LoadScript(string(content), script)
}
})
}
4 changes: 2 additions & 2 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func LoadFrom(dir string, prefix string) {
}

share.Walk(dir, ".json", func(root, filename string) {
name := share.SpecName(root, filename)
name := prefix + share.SpecName(root, filename)
content := share.ReadFile(filename)
gou.LoadModel(string(content), prefix+name)
gou.LoadModel(string(content), name)
})
}
1 change: 1 addition & 0 deletions page/REAME.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 数据页面
60 changes: 60 additions & 0 deletions page/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package page

import (
"github.com/yaoapp/xiang/share"
)

// SetupAPIs 设定API数据
func (page *Page) SetupAPIs() {

defaults := map[string]share.API{
"data": apiDataDefault(),
"setting": apiSettingDefault(),
}

// 开发者填写的规则
for name := range page.APIs {
if _, has := defaults[name]; !has {
delete(page.APIs, name)
continue
}

api := defaults[name]
api.Name = name
if page.APIs[name].Process != "" {
api.Process = page.APIs[name].Process
}

if page.APIs[name].Guard != "" {
api.Guard = page.APIs[name].Guard
}

if page.APIs[name].Default != nil {
api.Default = page.APIs[name].Default
}

defaults[name] = api
}

page.APIs = defaults
}

// apiSearchDefault data 接口默认值
func apiDataDefault() share.API {
param := map[string]interface{}{}
return share.API{
Name: "data",
Guard: "bearer-jwt",
Process: "xiang.page.data",
Default: []interface{}{param},
}
}

// apiSettingDefault setting 接口默认值
func apiSettingDefault() share.API {
return share.API{
Name: "setting",
Guard: "bearer-jwt",
Process: "xiang.page.setting",
}
}
86 changes: 86 additions & 0 deletions page/page.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package page

import (
"fmt"

jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/gou"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/xiang/config"
"github.com/yaoapp/xiang/share"
"github.com/yaoapp/xiang/xlog"
)

// Pages 已载入页面
var Pages = map[string]*Page{}

// Load 加载页面
func Load(cfg config.Config) {
LoadFrom(cfg.RootPage, "")
}

// LoadFrom 从特定目录加载
func LoadFrom(dir string, prefix string) {

if share.DirNotExists(dir) {
return
}

share.Walk(dir, ".json", func(root, filename string) {
name := prefix + share.SpecName(root, filename)
content := share.ReadFile(filename)
_, err := LoadPage(content, name)
if err != nil {
exception.New("%s 页面格式错误", 400, name).Ctx(filename).Throw()
}
})

// Load Script
share.Walk(dir, ".js", func(root, filename string) {
name := prefix + share.SpecName(root, filename)
page := Select(name)
if page != nil {
script := share.ScriptName(filename)
content := share.ReadFile(filename)
page.LoadScript(string(content), script)
}
})

}

// LoadPage 载入页面
func LoadPage(source []byte, name string) (*Page, error) {
page := &Page{
Flow: gou.Flow{
Name: name,
},
}
err := jsoniter.Unmarshal(source, page)
if err != nil {
xlog.Println(name)
xlog.Println(err.Error())
xlog.Println(string(source))
return nil, err
}
page.Prepare()
page.SetupAPIs()
Pages[name] = page
return page, nil
}

// Select 读取已加载页面
func Select(name string) *Page {
page, has := Pages[name]
if !has {
exception.New(
fmt.Sprintf("Page:%s; 尚未加载", name),
400,
).Throw()
}
return page
}

// GetData 运行 flow 返回数值
func (page Page) GetData(params map[string]interface{}) interface{} {
return page.Flow.Exec(params)
}
29 changes: 29 additions & 0 deletions page/page_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package page

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/yaoapp/xiang/config"
"github.com/yaoapp/xiang/model"
"github.com/yaoapp/xiang/query"
"github.com/yaoapp/xiang/share"
)

func TestLoad(t *testing.T) {
share.DBConnect(config.Conf.Database)
model.Load(config.Conf)
query.Load(config.Conf)

Load(config.Conf)
LoadFrom("not a path", "404.")
check(t)
}

func check(t *testing.T) {
keys := []string{}
for key := range Pages {
keys = append(keys, key)
}
assert.Equal(t, 1, len(keys))
}
82 changes: 82 additions & 0 deletions page/process.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package page

import (
"strings"

"github.com/yaoapp/gou"
"github.com/yaoapp/kun/maps"
)

// 注册处理器
func init() {
gou.RegisterProcessHandler("xiang.page.data", ProcessData)
gou.RegisterProcessHandler("xiang.page.setting", ProcessSetting)
}

// ProcessData xiang.page.data
// 查询数据分析页面中定义的数据
func ProcessData(process *gou.Process) interface{} {

process.ValidateArgNums(3)
name := process.ArgsString(0)
params := process.ArgsMap(1)
page := Select(name)
api := page.APIs["data"]
if process.NumOfArgsIs(4) && api.IsAllow(process.Args[3]) {
return nil
}

if len(api.Default) > 0 {
if defaults, ok := api.Default[0].(map[string]interface{}); ok {
for key, value := range defaults {
if !params.Has(key) {
params.Set(key, value)
}
}
}
}

return page.GetData(params)
}

// ProcessSetting xiang.page.setting
// 查询数据分析页面中定义的数据
func ProcessSetting(process *gou.Process) interface{} {

process.ValidateArgNums(2)
name := process.ArgsString(0)
field := process.ArgsString(1)
page := Select(name)
api := page.APIs["setting"]
if process.NumOfArgsIs(2) && api.IsAllow(process.Args[1]) {
return nil
}

fields := strings.Split(field, ",")
setting := maps.Map{
"name": page.Name,
"label": page.Label,
"version": page.Version,
"description": page.Description,
"filters": page.Filters,
"page": page.Page,
}

if len(fields) == 1 && setting.Has(fields[0]) {
field := strings.TrimSpace(fields[0])
return setting.Get(field)
}

if len(fields) > 1 {
res := maps.Map{}
for _, field := range fields {
field = strings.TrimSpace(field)
if setting.Has(field) {
res.Set(field, setting.Get(field))
}
}
return res
}
return setting

}
Loading

0 comments on commit 9d42324

Please sign in to comment.