优化载入逻辑: Step 2/5

This commit is contained in:
Max 2021-10-18 23:30:18 +08:00
parent e08df986d3
commit 1d5f1d110f
4 changed files with 81 additions and 11 deletions

View file

@ -12,6 +12,7 @@ import (
"github.com/yaoapp/xiang/api"
"github.com/yaoapp/xiang/app"
"github.com/yaoapp/xiang/config"
"github.com/yaoapp/xiang/flow"
"github.com/yaoapp/xiang/share"
"github.com/yaoapp/xiang/table"
"github.com/yaoapp/xun/capsule"
@ -22,7 +23,8 @@ func Load(cfg config.Config) {
app.Load(cfg) // 加载应用信息
DBConnect(cfg.Database)
LoadEngine(cfg.Path)
api.Load(cfg) // 加载API
api.Load(cfg) // 加载API
flow.Load(cfg) // 加载Flow
LoadApp(share.AppRoot{
APIs: cfg.RootAPI,
Flows: cfg.RootFLow,
@ -144,14 +146,14 @@ func LoadApp(app share.AppRoot) {
// }
// }
// 加载Flow
if strings.HasPrefix(app.Flows, "fs://") || !strings.Contains(app.Flows, "://") {
root := strings.TrimPrefix(app.Flows, "fs://")
scripts := share.GetAppFilesFS(root, ".json")
for _, script := range scripts {
gou.LoadFlow(string(script.Content), script.Name)
}
}
// // 加载Flow
// if strings.HasPrefix(app.Flows, "fs://") || !strings.Contains(app.Flows, "://") {
// root := strings.TrimPrefix(app.Flows, "fs://")
// scripts := share.GetAppFilesFS(root, ".json")
// for _, script := range scripts {
// gou.LoadFlow(string(script.Content), script.Name)
// }
// }
// 加载Model
if strings.HasPrefix(app.Models, "fs://") || !strings.Contains(app.Models, "://") {

View file

@ -1,4 +1,29 @@
package flow
// Load 加载业务逻辑编排
func Load(filepath string) {}
import (
"fmt"
"github.com/yaoapp/gou"
"github.com/yaoapp/xiang/config"
"github.com/yaoapp/xiang/share"
)
// Load 加载API
func Load(cfg config.Config) {
fmt.Println(cfg.RootFLow)
LoadFrom(cfg.RootFLow, "")
}
// LoadFrom 从特定目录加载
func LoadFrom(dir string, prefix string) {
if share.DirNotExists(dir) {
return
}
share.Walk(dir, ".json", func(root, filename string) {
name := share.SpecName(root, filename)
content := share.ReadFile(filename)
gou.LoadFlow(string(content), prefix+name)
})
}

24
flow/flow_test.go Normal file
View file

@ -0,0 +1,24 @@
package flow
import (
"testing"
"github.com/go-playground/assert/v2"
"github.com/yaoapp/gou"
"github.com/yaoapp/xiang/config"
)
func TestLoad(t *testing.T) {
gou.Flows = make(map[string]*gou.Flow)
Load(config.Conf)
LoadFrom("not a path", "404.")
check(t)
}
func check(t *testing.T) {
keys := []string{}
for key := range gou.Flows {
keys = append(keys, key)
}
assert.Equal(t, 2, len(keys))
}

View file

@ -0,0 +1,19 @@
{
"label": "最新信息",
"version": "1.0.0",
"description": "最新信息",
"nodes": [
{
"name": "user",
"engine": "xiang",
"query": {
"select": ["id", "name"],
"from": "$user",
"first": true
}
}
],
"output": {
"params": "{{$in}}"
}
}