live load
This commit is contained in:
parent
8f4110646d
commit
8e2b1974d1
4 changed files with 66 additions and 27 deletions
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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{}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
})
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue