From bf895a2bc43789fa84ca1617f9199b42b23deda1 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 15 Sep 2021 15:37:17 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=91=E5=90=ACxiang=20=E6=A0=B8=E5=BF=83?= =?UTF-8?q?=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- global/load.go | 129 +++++++++++++++++++++++++++++++++------ main_test.go | 9 ++- xiang/apis/sys.http.json | 2 +- 3 files changed, 117 insertions(+), 23 deletions(-) diff --git a/global/load.go b/global/load.go index 4f622220..185071a2 100644 --- a/global/load.go +++ b/global/load.go @@ -33,6 +33,78 @@ func LoadEngine(from string) { if strings.HasPrefix(from, "fs://") || !strings.Contains(from, "://") { root := strings.TrimPrefix(from, "fs://") scripts = getFilesFS(root, ".json") + + // 监听 flows (这里应该重构) + go Watch(filepath.Join(root, "flows"), func(op string, file string) { + + if !strings.HasSuffix(file, ".json") { + return + } + + if strings.HasSuffix(file, ".js") { + basName := getFileBaseName(root, file) + file = basName + ".flow.json" + } + + if op == "write" || op == "create" { + script := getFile(root, file) + gou.LoadFlow(string(script.Content), "xiang."+script.Name) // Reload + log.Printf("Flow %s 已重新加载完毕", "xiang."+script.Name) + } else if op == "remove" || op == "rename" { + name := "xiang." + getFileName(root, file) + if _, has := gou.Flows[name]; has { + delete(gou.Flows, name) + log.Printf("Flow %s 已经移除", name) + } + } + }) + + // 监听 models + go Watch(filepath.Join(root, "models"), func(op string, file string) { + + if !strings.HasSuffix(file, ".json") { + return + } + if op == "write" || op == "create" { + script := getFile(root, file) + gou.LoadModel(string(script.Content), "xiang."+script.Name) // Reload + log.Printf("Model %s 已重新加载完毕", "xiang."+script.Name) + } else if op == "remove" || op == "rename" { + name := "xiang." + getFileName(root, file) + if _, has := gou.Models[name]; has { + delete(gou.Models, name) + log.Printf("Model %s 已经移除", name) + } + } + }) + + // 监听 apis + go Watch(filepath.Join(root, "apis"), func(op string, file string) { + + if !strings.HasSuffix(file, ".json") { + return + } + if op == "write" || op == "create" { + script := getFile(root, file) + gou.LoadAPI(string(script.Content), "xiang."+script.Name) // Reload + log.Printf("API %s 已重新加载完毕", "xiang."+script.Name) + } else if op == "remove" || op == "rename" { + name := "xiang." + getFileName(root, file) + if _, has := gou.APIs[name]; has { + delete(gou.APIs, name) + log.Printf("API %s 已经移除", name) + } + } + + // 重启服务器 + if op == "write" || op == "create" || op == "remove" || op == "rename" { + ServiceStop(func() { + log.Printf("服务器重启完毕") + go ServiceStart() + }) + } + }) + } else if strings.HasPrefix(from, "bin://") { root := strings.TrimPrefix(from, "bin://") scripts = getFilesBin(root, ".json") @@ -55,7 +127,7 @@ func LoadEngine(from string) { case "flows": gou.LoadFlow(string(script.Content), "xiang."+script.Name) break - case "api": + case "apis": gou.LoadAPI(string(script.Content), "xiang."+script.Name) break } @@ -308,30 +380,48 @@ func getFilesFS(root string, typ string) []Script { return err } if strings.HasSuffix(path, typ) { - filename := strings.TrimPrefix(path, root+"/") - name, typ := getTypeName(filename) - - file, err := os.Open(path) - 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: typ, - Content: content, - }) + files = append(files, getFile(root, path)) } return nil }) return files } +// getFile 读取文件 +func getFile(root string, path string) Script { + filename := strings.TrimPrefix(path, root+"/") + name, typ := getTypeName(filename) + file, err := os.Open(path) + 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: typ, + Content: content, + } +} + +// getFileName 读取文件 +func getFileName(root string, file string) string { + filename := strings.TrimPrefix(file, root+"/") + name, _ := getTypeName(filename) + return name +} + +// getFileBaseName 读取文件base +func getFileBaseName(root string, file string) string { + filename := strings.TrimPrefix(file, root+"/") + namer := strings.Split(filename, ".") + return filepath.Join(root, namer[0]) +} + // getFilesBin 从 bindata 中读取文件列表 func getFilesBin(root string, typ string) []Script { files := []Script{} @@ -359,6 +449,5 @@ func getTypeName(path string) (name string, typ string) { nametypes := strings.Split(namer[0], "/") name = strings.Join(nametypes[1:], ".") typ = nametypes[0] - return name, typ } diff --git a/main_test.go b/main_test.go index 9912bec0..98dee2ba 100644 --- a/main_test.go +++ b/main_test.go @@ -38,7 +38,10 @@ func TestCommandMigrate(t *testing.T) { func TestCommandStart(t *testing.T) { oldArgs := os.Args - defer func() { os.Args = oldArgs }() + defer func() { + os.Args = oldArgs + global.ServiceStop(func() {}) + }() go func() { os.Args = append(os.Args, "start") main() @@ -81,7 +84,9 @@ func TestCommandStart(t *testing.T) { func TestCommandStop(t *testing.T) { oldArgs := os.Args - defer func() { os.Args = oldArgs }() + defer func() { + os.Args = oldArgs + }() go func() { os.Args = append(os.Args, "start") main() diff --git a/xiang/apis/sys.http.json b/xiang/apis/sys.http.json index 48074be8..c376fbae 100644 --- a/xiang/apis/sys.http.json +++ b/xiang/apis/sys.http.json @@ -6,7 +6,7 @@ "guard": "bearer-jwt", "paths": [ { - "path": "/info/:id", + "path": "/info", "method": "GET", "guard": "-", "process": "models.user.Find",