Skip to content

Commit

Permalink
监听xiang 核心目录
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Sep 15, 2021
1 parent db42268 commit bf895a2
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 23 deletions.
129 changes: 109 additions & 20 deletions global/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
}
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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
}
9 changes: 7 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion xiang/apis/sys.http.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"guard": "bearer-jwt",
"paths": [
{
"path": "/info/:id",
"path": "/info",
"method": "GET",
"guard": "-",
"process": "models.user.Find",
Expand Down

0 comments on commit bf895a2

Please sign in to comment.