From 8e2b1974d15e6b436efb2a9e3d565acc40cc7e1e Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 14 Sep 2021 20:55:02 +0800 Subject: [PATCH] live load --- app/apis/user.http.json | 11 +++++++ global/load.go | 69 ++++++++++++++++++++++++++++------------- global/watch.go | 9 +++--- global/watch_test.go | 4 +-- 4 files changed, 66 insertions(+), 27 deletions(-) diff --git a/app/apis/user.http.json b/app/apis/user.http.json index c43f1fe9..b63f3fba 100644 --- a/app/apis/user.http.json +++ b/app/apis/user.http.json @@ -16,6 +16,17 @@ "type": "application/json" } }, + { + "path": "/ping", + "method": "GET", + "guard": "-", + "process": "plugins.user.Login", + "in": ["$payload.mobile", "$payload.password", "$payload.captcha"], + "out": { + "status": 200, + "type": "application/json" + } + }, { "path": "/find/:id", "method": "GET", diff --git a/global/load.go b/global/load.go index 984f69f2..da0645c2 100644 --- a/global/load.go +++ b/global/load.go @@ -2,6 +2,7 @@ package global import ( "io/ioutil" + "log" "os" "path" "path/filepath" @@ -71,6 +72,22 @@ func LoadApp(api string, flow string, model string, plugin string) { for _, script := range scripts { gou.LoadAPI(string(script.Content), script.Name) } + + // 监听API修改 + if Conf.Mode == "debug" { + go Watch(root, func(op string, file string) { + if op == "write" || op == "create" || op == "rename" { + script := getAppFile(root, file, ".json") + gou.LoadAPI(string(script.Content), script.Name) // Reload + log.Printf("API %s 已重新加载完毕", script.Name) + } else if op == "remove" { + name := getAppFileName(root, file) + if _, has := gou.APIs[name]; has { + delete(gou.APIs, name) + } + } + }) + } } // 加载Flow @@ -136,33 +153,43 @@ func getAppFilesFS(root string, typ string) []Script { return err } if strings.HasSuffix(filepath, typ) { - filename := strings.TrimPrefix(filepath, root+"/") - - namer := strings.Split(filename, ".") - nametypes := strings.Split(namer[0], "/") - name := strings.Join(nametypes, ".") - - file, err := os.Open(filepath) - if err != nil { - exception.Err(err, 500).Throw() - } - - defer file.Close() - content, err := ioutil.ReadAll(file) - if err != nil { - exception.Err(err, 500).Throw() - } - files = append(files, Script{ - Name: name, - Type: "app", - Content: content, - }) + files = append(files, getAppFile(root, filepath, typ)) } + return nil }) return files } +// getAppFile 读取文件 +func getAppFile(root string, filepath string, typ string) Script { + name := getAppFileName(root, filepath) + file, err := os.Open(filepath) + if err != nil { + exception.Err(err, 500).Throw() + } + + defer file.Close() + content, err := ioutil.ReadAll(file) + if err != nil { + exception.Err(err, 500).Throw() + } + return Script{ + Name: name, + Type: "app", + Content: content, + } +} + +// getAppFile 读取文件 +func getAppFileName(root string, filepath string) string { + filename := strings.TrimPrefix(filepath, root+"/") + namer := strings.Split(filename, ".") + nametypes := strings.Split(namer[0], "/") + name := strings.Join(nametypes, ".") + return name +} + // getFilesFS 遍历目录,读取文件列表 func getFilesFS(root string, typ string) []Script { files := []Script{} diff --git a/global/watch.go b/global/watch.go index e6aa7009..36d86609 100644 --- a/global/watch.go +++ b/global/watch.go @@ -1,6 +1,7 @@ package global import ( + "io/fs" "log" "os" "path/filepath" @@ -67,18 +68,18 @@ func Watch(root string, cb func(op string, file string)) { log.Println("开始监听目录:", root) // 监听子目录 - filepath.Walk(root, func(subfolder string, info os.FileInfo, err error) error { + filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { if err != nil { exception.Err(err, 500).Throw() return err } - if subfolder == root { + if path == root { return nil } - if info.IsDir() { - go Watch(subfolder, cb) + if d.IsDir() { + go Watch(path, cb) } return nil }) diff --git a/global/watch_test.go b/global/watch_test.go index 9d3e3fa8..30f5792e 100644 --- a/global/watch_test.go +++ b/global/watch_test.go @@ -9,8 +9,8 @@ import ( "github.com/stretchr/testify/assert" ) -func TestWatchAddNew(t *testing.T) { - root := path.Join(Conf.Source, "/app") +func TestWatch(t *testing.T) { + root := path.Join(Conf.Source, "/app/flows") assert.NotPanics(t, func() { go Watch(root, func(op string, file string) { log.Println(op, file)