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"
|
"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",
|
"path": "/find/:id",
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package global
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
@ -71,6 +72,22 @@ func LoadApp(api string, flow string, model string, plugin string) {
|
||||||
for _, script := range scripts {
|
for _, script := range scripts {
|
||||||
gou.LoadAPI(string(script.Content), script.Name)
|
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
|
// 加载Flow
|
||||||
|
|
@ -136,33 +153,43 @@ func getAppFilesFS(root string, typ string) []Script {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if strings.HasSuffix(filepath, typ) {
|
if strings.HasSuffix(filepath, typ) {
|
||||||
filename := strings.TrimPrefix(filepath, root+"/")
|
files = append(files, getAppFile(root, filepath, typ))
|
||||||
|
|
||||||
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,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return files
|
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 遍历目录,读取文件列表
|
// getFilesFS 遍历目录,读取文件列表
|
||||||
func getFilesFS(root string, typ string) []Script {
|
func getFilesFS(root string, typ string) []Script {
|
||||||
files := []Script{}
|
files := []Script{}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package global
|
package global
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
@ -67,18 +68,18 @@ func Watch(root string, cb func(op string, file string)) {
|
||||||
log.Println("开始监听目录:", root)
|
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 {
|
if err != nil {
|
||||||
exception.Err(err, 500).Throw()
|
exception.Err(err, 500).Throw()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if subfolder == root {
|
if path == root {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if info.IsDir() {
|
if d.IsDir() {
|
||||||
go Watch(subfolder, cb)
|
go Watch(path, cb)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,8 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestWatchAddNew(t *testing.T) {
|
func TestWatch(t *testing.T) {
|
||||||
root := path.Join(Conf.Source, "/app")
|
root := path.Join(Conf.Source, "/app/flows")
|
||||||
assert.NotPanics(t, func() {
|
assert.NotPanics(t, func() {
|
||||||
go Watch(root, func(op string, file string) {
|
go Watch(root, func(op string, file string) {
|
||||||
log.Println(op, file)
|
log.Println(op, file)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue