From 44c041decfc5ad9ad44779bd0230f6632b62281d Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 27 Mar 2022 20:47:57 +0800 Subject: [PATCH] [add] Store Process & Javascript Object --- engine/load.go | 8 ++++++++ store/store.go | 36 ++++++++++++++++++++++++++++++++++++ store/store_test.go | 23 +++++++++++++++++++++++ tests/stores/share.lru.json | 6 ++++++ 4 files changed, 73 insertions(+) create mode 100644 store/store.go create mode 100644 store/store_test.go create mode 100644 tests/stores/share.lru.json diff --git a/engine/load.go b/engine/load.go index 4046165a..bef0cdd1 100644 --- a/engine/load.go +++ b/engine/load.go @@ -21,6 +21,7 @@ import ( "github.com/yaoapp/yao/script" "github.com/yaoapp/yao/server" "github.com/yaoapp/yao/share" + "github.com/yaoapp/yao/store" "github.com/yaoapp/yao/table" "github.com/yaoapp/yao/workflow" ) @@ -56,6 +57,7 @@ func Load(cfg config.Config) (err error) { if err != nil { log.Warn(err.Error()) } + err = script.Load(cfg) // 加载JS处理器 script if err != nil { log.Warn(err.Error()) @@ -71,6 +73,12 @@ func Load(cfg config.Config) (err error) { if err != nil { log.Warn(err.Error()) } + + err = store.Load(cfg) // Load stores + if err != nil { + log.Warn(err.Error()) + } + err = plugin.Load(cfg) // 加载业务插件 plugin if err != nil { log.Warn(err.Error()) diff --git a/store/store.go b/store/store.go new file mode 100644 index 00000000..4b5e3db6 --- /dev/null +++ b/store/store.go @@ -0,0 +1,36 @@ +package store + +import ( + "fmt" + "path/filepath" + + "github.com/yaoapp/gou" + "github.com/yaoapp/kun/log" + "github.com/yaoapp/yao/config" + "github.com/yaoapp/yao/share" +) + +// Load load store +func Load(cfg config.Config) error { + var root = filepath.Join(cfg.Root, "stores") + return LoadFrom(root, "") +} + +// LoadFrom load from dir +func LoadFrom(dir string, prefix string) error { + + if share.DirNotExists(dir) { + return fmt.Errorf("%s does not exists", dir) + } + + err := share.Walk(dir, ".json", func(root, filename string) { + name := prefix + share.SpecName(root, filename) + content := share.ReadFile(filename) + _, err := gou.LoadStore(string(content), name) + if err != nil { + log.With(log.F{"root": root, "file": filename}).Error(err.Error()) + } + }) + + return err +} diff --git a/store/store_test.go b/store/store_test.go new file mode 100644 index 00000000..c569579f --- /dev/null +++ b/store/store_test.go @@ -0,0 +1,23 @@ +package store + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/yaoapp/gou/kv" + "github.com/yaoapp/yao/config" +) + +func TestLoad(t *testing.T) { + Load(config.Conf) + LoadFrom("not a path", "404.") + check(t) +} + +func check(t *testing.T) { + keys := []string{} + for key := range kv.Pools { + keys = append(keys, key) + } + assert.Equal(t, 1, len(keys)) +} diff --git a/tests/stores/share.lru.json b/tests/stores/share.lru.json new file mode 100644 index 00000000..7ecbd3a7 --- /dev/null +++ b/tests/stores/share.lru.json @@ -0,0 +1,6 @@ +{ + "name": "Share LRU Cache", + "description": "The share memory LRU cache", + "type": "lru", + "option": { "size": 1024 } +}