forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
613 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# 数据页面 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
Oops, something went wrong.