Skip to content

Commit

Permalink
优化载入逻辑: Step 2/5
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Oct 18, 2021
1 parent e08df98 commit 1d5f1d1
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 11 deletions.
20 changes: 11 additions & 9 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/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"
Expand All @@ -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,
Expand Down Expand Up @@ -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, "://") {
Expand Down
29 changes: 27 additions & 2 deletions flow/flow.go
Original file line number Diff line number Diff line change
@@ -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 changes: 24 additions & 0 deletions flow/flow_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
19 changes: 19 additions & 0 deletions tests/flows/foo/bar.flow.json
Original file line number Diff line number Diff line change
@@ -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}}"
}
}

0 comments on commit 1d5f1d1

Please sign in to comment.