diff --git a/cmd/start.go b/cmd/start.go index 8ab181f230..a6667c66af 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -84,7 +84,7 @@ var startCmd = &cobra.Command{ fmt.Println("") // 调试模式 - if config.Conf.Mode == "debug" { + if mode == "development" { service.Watch(config.Conf) } diff --git a/data/bindata.go b/data/bindata.go index 9bec921d94..67b7dcb5fd 100644 --- a/data/bindata.go +++ b/data/bindata.go @@ -206,7 +206,7 @@ func uiIndexHtml() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "ui/index.html", size: 22, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "ui/index.html", size: 22, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -226,7 +226,7 @@ func yaoApisReadmeMd() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/apis/README.md", size: 13, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/apis/README.md", size: 13, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -246,7 +246,7 @@ func yaoApisChartHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/apis/chart.http.json", size: 624, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/apis/chart.http.json", size: 624, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -266,7 +266,7 @@ func yaoApisImportHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/apis/import.http.json", size: 1676, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/apis/import.http.json", size: 1676, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -286,7 +286,7 @@ func yaoApisPageHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/apis/page.http.json", size: 609, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/apis/page.http.json", size: 609, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -306,7 +306,7 @@ func yaoApisStorageHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/apis/storage.http.json", size: 1092, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/apis/storage.http.json", size: 1092, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -326,7 +326,7 @@ func yaoApisTableHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/apis/table.http.json", size: 3196, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/apis/table.http.json", size: 3196, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -346,7 +346,7 @@ func yaoApisUserHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/apis/user.http.json", size: 1453, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/apis/user.http.json", size: 1453, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -366,7 +366,7 @@ func yaoApisXiangHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/apis/xiang.http.json", size: 1239, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/apis/xiang.http.json", size: 1239, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -386,7 +386,7 @@ func yaoDataDs_store() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/data/.DS_Store", size: 6148, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/data/.DS_Store", size: 6148, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -406,7 +406,7 @@ func yaoDataAppJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/data/app.json", size: 277, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/data/app.json", size: 277, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -426,7 +426,7 @@ func yaoDataIconsDs_store() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/data/icons/.DS_Store", size: 6148, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/data/icons/.DS_Store", size: 6148, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -446,7 +446,7 @@ func yaoDataIcons404Png() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/data/icons/404.png", size: 9342, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/data/icons/404.png", size: 9342, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -466,7 +466,7 @@ func yaoDataIconsIconIcns() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/data/icons/icon.icns", size: 67465, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/data/icons/icon.icns", size: 67465, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -486,7 +486,7 @@ func yaoDataIconsIconIco() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/data/icons/icon.ico", size: 54993, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/data/icons/icon.ico", size: 54993, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -506,7 +506,7 @@ func yaoDataIconsIconPng() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/data/icons/icon.png", size: 34558, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/data/icons/icon.png", size: 34558, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -526,7 +526,7 @@ func yaoDataIndexHtml() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/data/index.html", size: 282, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/data/index.html", size: 282, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -546,7 +546,7 @@ func yaoFlowsReadmeMd() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/flows/README.md", size: 12, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/flows/README.md", size: 12, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -566,7 +566,7 @@ func yaoFlowsMenuFlowJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/flows/menu.flow.json", size: 1045, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/flows/menu.flow.json", size: 1045, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -586,7 +586,7 @@ func yaoFlowsUserFindFlowJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/flows/user/find.flow.json", size: 328, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/flows/user/find.flow.json", size: 328, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -606,7 +606,7 @@ func yaoLangsEnUsJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/langs/en-US.json", size: 66, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/langs/en-US.json", size: 66, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -626,7 +626,7 @@ func yaoModelsReadmeMd() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/models/README.md", size: 15, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/models/README.md", size: 15, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -646,7 +646,7 @@ func yaoModelsMenuModJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/models/menu.mod.json", size: 2378, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/models/menu.mod.json", size: 2378, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -666,7 +666,7 @@ func yaoModelsUserModJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/models/user.mod.json", size: 8826, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/models/user.mod.json", size: 8826, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -686,7 +686,7 @@ func yaoModelsWorkflowModJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/models/workflow.mod.json", size: 3333, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/models/workflow.mod.json", size: 3333, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -706,7 +706,7 @@ func yaoTablesReadmeMd() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/tables/README.md", size: 15, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/tables/README.md", size: 15, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -726,7 +726,7 @@ func yaoTablesMenuTabJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/tables/menu.tab.json", size: 10433, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/tables/menu.tab.json", size: 10433, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -746,7 +746,7 @@ func yaoTablesUserTabJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "yao/tables/user.tab.json", size: 2319, mode: os.FileMode(420), modTime: time.Unix(1644846194, 0)} + info := bindataFileInfo{name: "yao/tables/user.tab.json", size: 2319, mode: os.FileMode(420), modTime: time.Unix(1644852770, 0)} a := &asset{bytes: bytes, info: info} return a, nil } diff --git a/service/service.go b/service/service.go index c996a53690..e6a73f2ae2 100644 --- a/service/service.go +++ b/service/service.go @@ -29,6 +29,22 @@ func Start() { Middlewares...) } +// StartWithouttSession 启动服务 +func StartWithouttSession() { + + gou.SetHTTPGuards(Guards) + gou.ServeHTTP( + gou.Server{ + Host: config.Conf.Host, + Port: config.Conf.Port, + Root: "/api", + }, + &shutdown, func(s gou.Server) { + shutdownComplete <- true + }, + Middlewares...) +} + // Stop 关闭服务 func Stop(onComplete func()) { shutdown <- true diff --git a/service/watch.go b/service/watch.go index 7c79791231..ed6afc41e3 100644 --- a/service/watch.go +++ b/service/watch.go @@ -2,13 +2,15 @@ package service import ( "fmt" - "log" + "os" "path/filepath" "strings" + "github.com/fatih/color" "github.com/yaoapp/gou" "github.com/yaoapp/yao/chart" "github.com/yaoapp/yao/config" + "github.com/yaoapp/yao/engine" "github.com/yaoapp/yao/page" "github.com/yaoapp/yao/share" "github.com/yaoapp/yao/table" @@ -17,35 +19,62 @@ import ( // Watch 监听应用目录文件变更 func Watch(cfg config.Config) { - // WatchEngine(cfg.Path) - // WatchModel(cfg.RootModel, "") - // WatchAPI(cfg.RootAPI, "") - // WatchFlow(cfg.RootFLow, "") - // WatchPlugin(cfg.RootPlugin) - // WatchTable(cfg.RootTable, "") - // WatchChart(cfg.RootChart, "") - // WatchPage(cfg.RootPage, "") - // WatchWorkFlow(cfg.RootWorkFlow, "") + if os.Getenv("YAO_DEV") != "" { + WatchEngine(filepath.Join(os.Getenv("YAO_DEV"), "/yao")) + } + WatchModel(filepath.Join(cfg.Root, "models"), "") + WatchAPI(filepath.Join(cfg.Root, "apis"), "") + WatchFlow(filepath.Join(cfg.Root, "flows"), "") + WatchPlugin(filepath.Join(cfg.Root, "plugins")) + WatchTable(filepath.Join(cfg.Root, "tables"), "") + WatchChart(filepath.Join(cfg.Root, "charts"), "") + WatchPage(filepath.Join(cfg.Root, "pages"), "") + WatchWorkFlow(filepath.Join(cfg.Root, "workflows"), "") // 看板大屏 - WatchPage(filepath.Join(cfg.Root, "/kanban"), "") - WatchPage(filepath.Join(cfg.Root, "/screen"), "") + WatchPage(filepath.Join(cfg.Root, "kanban"), "") + WatchPage(filepath.Join(cfg.Root, "screen"), "") + + // 监听脚本 & libs更新 + WatchGlobal(filepath.Join(cfg.Root, "libs")) + WatchGlobal(filepath.Join(cfg.Root, "scripts")) } // WatchEngine 监听监听引擎内建数据变更 func WatchEngine(root string) { - if !strings.HasPrefix(root, "fs://") && strings.Contains(root, "://") { - return - } - root = strings.TrimPrefix(root, "fs://") root = share.DirAbs(root) - WatchModel(filepath.Join(root, "models"), "xiang.") WatchAPI(filepath.Join(root, "apis"), "xiang.") WatchFlow(filepath.Join(root, "flows"), "xiang.") WatchTable(filepath.Join(root, "tables"), "xiang.") } +// WatchGlobal 监听通用程序更新 +func WatchGlobal(root string) { + if share.DirNotExists(root) { + return + } + root = share.DirAbs(root) + go share.Watch(root, func(op string, filename string) { + if !strings.HasSuffix(filename, ".json") && !strings.HasSuffix(filename, ".js") { + return + } + + // 重启服务器 + if op == "write" || op == "create" || op == "remove" || op == "rename" { + err := engine.Load(config.Conf) + if err != nil { + fmt.Println(color.RedString("Fatal: %s", err.Error())) + return + } + Stop(func() { + fmt.Println(color.GreenString("Service Restarted")) + go StartWithouttSession() + }) + } + }) +} + // WatchModel 监听业务接口更新 func WatchModel(root string, prefix string) { if share.DirNotExists(root) { @@ -60,14 +89,17 @@ func WatchModel(root string, prefix string) { if op == "write" || op == "create" { name := prefix + share.SpecName(root, filename) content := share.ReadFile(filename) - gou.LoadModel(string(content), name) // Reload - log.Printf("Model %s 已重新加载完毕", name) - + _, err := gou.LoadModelReturn(string(content), name) // Reload + if err != nil { + fmt.Println(color.RedString("Fatal: %s", err.Error())) + return + } + fmt.Println(color.GreenString("Model %s Reloaded", name)) } else if op == "remove" || op == "rename" { name := prefix + share.SpecName(root, filename) if _, has := gou.Models[name]; has { delete(gou.Models, name) - log.Printf("Model %s 已经移除", name) + fmt.Println(color.RedString("Model %s Removed", name)) } } }) @@ -88,22 +120,26 @@ func WatchAPI(root string, prefix string) { if op == "write" || op == "create" { name := prefix + share.SpecName(root, filename) content := share.ReadFile(filename) - gou.LoadAPI(string(content), name) // Reload - log.Printf("API %s 已重新加载完毕", name) + _, err := gou.LoadAPIReturn(string(content), name) // Reload + if err != nil { + fmt.Println(color.RedString("Fatal: %s", err.Error())) + return + } + fmt.Println(color.GreenString("API %s Reloaded", name)) } else if op == "remove" || op == "rename" { name := prefix + share.SpecName(root, filename) if _, has := gou.APIs[name]; has { delete(gou.APIs, name) - log.Printf("API %s 已经移除", name) + fmt.Println(color.RedString("API %s Removed", name)) } } // 重启服务器 if op == "write" || op == "create" || op == "remove" || op == "rename" { Stop(func() { - log.Printf("服务器重启完毕") - go Start() + fmt.Println(color.GreenString("Service Restarted")) + go StartWithouttSession() }) } }) @@ -129,23 +165,32 @@ func WatchFlow(root string, prefix string) { if op == "write" || op == "create" { name := prefix + share.SpecName(root, filename) content := share.ReadFile(filename) - flow := gou.LoadFlow(string(content), name) // Reload - if flow != nil { // Reload Script + flow, err := gou.LoadFlowReturn(string(content), name) // Reload + if err != nil { + fmt.Println(color.RedString("Fatal: %s", err.Error())) + return + } + + if flow != nil { // Reload Script dir := filepath.Dir(filename) share.Walk(dir, ".js", func(root, filename string) { script := share.ScriptName(filename) content := share.ReadFile(filename) - flow.LoadScript(string(content), script) + _, err := flow.LoadScriptReturn(string(content), script) + if err != nil { + fmt.Println(color.RedString("Fatal: %s", err.Error())) + return + } }) } - log.Printf("Flow %s 已重新加载完毕", name) + fmt.Println(color.GreenString("Flow %s Reloaded", name)) } else if op == "remove" || op == "rename" { name := prefix + share.SpecName(root, filename) if _, has := gou.Flows[name]; has { delete(gou.Flows, name) - log.Printf("Flow %s 已经移除", name) + fmt.Println(color.RedString("Flow %s Removed", name)) } } }) @@ -165,14 +210,18 @@ func WatchPlugin(root string) { if op == "write" || op == "create" { name := share.SpecName(root, filename) - gou.LoadPlugin(filename, name) // Reload - log.Printf("Plugin %s 已重新加载完毕", name) + _, err := gou.LoadPluginReturn(filename, name) // Reload + if err != nil { + fmt.Println(color.RedString("Fatal: %s", err.Error())) + return + } + fmt.Println(color.GreenString("Plugin %s Reloaded", name)) } else if op == "remove" || op == "rename" { name := share.SpecName(root, filename) if _, has := gou.Plugins[name]; has { delete(gou.Plugins, name) - log.Printf("Plugin %s 已经移除", name) + fmt.Println(color.RedString("Plugin %s Removed", name)) } } }) @@ -193,27 +242,35 @@ func WatchTable(root string, prefix string) { if op == "write" || op == "create" { name := prefix + share.SpecName(root, filename) content := share.ReadFile(filename) - table.LoadTable(string(content), name) // Reload Table + _, err := table.LoadTable(string(content), name) // Reload Table + if err != nil { + fmt.Println(color.RedString("Fatal: %s", err.Error())) + return + } api, has := gou.APIs["xiang.table"] if has { - api.Reload() // 重载API + _, err := gou.LoadAPIReturn(api.Source, api.Name) + if err != nil { + fmt.Println(color.RedString("Fatal: %s", err.Error())) + return + } } - log.Printf("数据表格 %s 已重新加载完毕", name) + fmt.Println(color.GreenString("Table %s Reloaded", name)) } else if op == "remove" || op == "rename" { name := prefix + share.SpecName(root, filename) if _, has := table.Tables[name]; has { delete(table.Tables, name) - log.Printf("数据表格 %s 已经移除", name) + fmt.Println(color.RedString("Table %s Removed", name)) } } // 重启服务器 if op == "write" || op == "create" || op == "remove" || op == "rename" { Stop(func() { - log.Printf("服务器重启完毕") - go Start() + fmt.Println(color.GreenString("Service Restarted")) + go StartWithouttSession() }) } }) @@ -239,36 +296,48 @@ func WatchChart(root string, prefix string) { if op == "write" || op == "create" { name := prefix + share.SpecName(root, filename) content := share.ReadFile(filename) - chart, _ := chart.LoadChart(content, name) // Relaod - if chart != nil { // Reload Script + chart, err := chart.LoadChart(content, name) // Relaod + if err != nil { + fmt.Println(color.RedString("Fatal: %s", err.Error())) + return + } + + if chart != nil { // Reload Script dir := filepath.Dir(filename) share.Walk(dir, ".js", func(root, filename string) { script := share.ScriptName(filename) content := share.ReadFile(filename) - chart.LoadScript(string(content), script) + _, err := chart.LoadScriptReturn(string(content), script) + if err != nil { + fmt.Println(color.RedString("Fatal: %s", err.Error())) + return + } }) } api, has := gou.APIs["xiang.chart"] if has { - fmt.Println("Reload API:", "--") - api.Reload() // 重载API + _, err := gou.LoadAPIReturn(api.Source, api.Name) + if err != nil { + fmt.Println(color.RedString("Fatal: %s", err.Error())) + return + } } - log.Printf("Chart %s 已重新加载完毕", name) + fmt.Println(color.GreenString("Chart %s Reloaded", name)) } else if op == "remove" || op == "rename" { name := prefix + share.SpecName(root, filename) if _, has := chart.Charts[name]; has { delete(chart.Charts, name) - log.Printf("Chart %s 已经移除", name) + fmt.Println(color.RedString("Chart %s Removed", name)) } } // 重启服务器 if op == "write" || op == "create" || op == "remove" || op == "rename" { Stop(func() { - log.Printf("服务器重启完毕") - go Start() + fmt.Println(color.GreenString("Service Restarted")) + go StartWithouttSession() }) } }) @@ -294,35 +363,48 @@ func WatchPage(root string, prefix string) { if op == "write" || op == "create" { name := prefix + share.SpecName(root, filename) content := share.ReadFile(filename) - page, _ := page.LoadPage(content, name) // Relaod - if page != nil { // Reload Script + page, err := page.LoadPage(content, name) // Relaod + if err != nil { + fmt.Println(color.RedString("Fatal: %s", err.Error())) + return + } + + if page != nil { // Reload Script dir := filepath.Dir(filename) share.Walk(dir, ".js", func(root, filename string) { script := share.ScriptName(filename) content := share.ReadFile(filename) - page.LoadScript(string(content), script) + _, err := page.LoadScriptReturn(string(content), script) + if err != nil { + fmt.Println(color.RedString("Fatal: %s", err.Error())) + return + } }) } api, has := gou.APIs["xiang.page"] if has { - api.Reload() // 重载API + _, err := gou.LoadAPIReturn(api.Source, api.Name) + if err != nil { + fmt.Println(color.RedString("Fatal: %s", err.Error())) + return + } } - log.Printf("Page %s 已重新加载完毕", name) + fmt.Println(color.GreenString("Page %s Reloaded", name)) } else if op == "remove" || op == "rename" { name := prefix + share.SpecName(root, filename) if _, has := page.Pages[name]; has { delete(page.Pages, name) - log.Printf("Page %s 已经移除", name) + fmt.Println(color.RedString("Page %s Removed", name)) } } // 重启服务器 if op == "write" || op == "create" || op == "remove" || op == "rename" { Stop(func() { - log.Printf("服务器重启完毕") - go Start() + fmt.Println(color.GreenString("Service Restarted")) + go StartWithouttSession() }) } }) @@ -344,28 +426,33 @@ func WatchWorkFlow(root string, prefix string) { content := share.ReadFile(filename) _, err := workflow.LoadWorkFlow(content, name) // Relaod if err != nil { + fmt.Println(color.RedString("Fatal: %s", err.Error())) return } api, has := gou.APIs["xiang.workflow."+name] if has { - api.Reload() // 重载API + _, err := gou.LoadAPIReturn(api.Source, api.Name) + if err != nil { + fmt.Println(color.RedString("Fatal: %s", err.Error())) + return + } } - log.Printf("WorkFlow %s 已重新加载完毕", name) + fmt.Println(color.GreenString("Workflow %s Reloaded", name)) } else if op == "remove" || op == "rename" { name := prefix + share.SpecName(root, filename) if _, has := workflow.WorkFlows[name]; has { delete(workflow.WorkFlows, name) - log.Printf("WorkFlow %s 已经移除", name) + fmt.Println(color.RedString("Workflow %s Removed", name)) } } // 重启服务器 if op == "write" || op == "create" || op == "remove" || op == "rename" { Stop(func() { - log.Printf("服务器重启完毕") - go Start() + fmt.Println(color.GreenString("Service Restarted")) + go StartWithouttSession() }) } }) diff --git a/share/watch.go b/share/watch.go index 42d4167fbc..41a13ac2c7 100644 --- a/share/watch.go +++ b/share/watch.go @@ -1,11 +1,13 @@ package share import ( + "fmt" "io/fs" "log" "os" "path/filepath" + "github.com/fatih/color" "github.com/fsnotify/fsnotify" "github.com/yaoapp/kun/exception" ) @@ -55,7 +57,7 @@ func Watch(root string, cb func(op string, file string)) { if !ok { return } - log.Println("error:", err) + log.Println("Error:", err) } } }() @@ -65,7 +67,7 @@ func Watch(root string, cb func(op string, file string)) { log.Fatal(err) } - log.Println("开始监听目录:", root) + fmt.Println(color.GreenString("Watching: %s", root)) // 监听子目录 filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { @@ -86,7 +88,7 @@ func Watch(root string, cb func(op string, file string)) { select { case v := <-watchDone[last]: - log.Println("停止监听目录:", root) + fmt.Println(color.GreenString("Stop Watching: %s", root)) if v == true { break } @@ -96,7 +98,7 @@ func Watch(root string, cb func(op string, file string)) { // StopWatch 停止监听 func StopWatch() { for i := range watchDone { - log.Println("发送停止信号:", i) + fmt.Println(color.GreenString("Stop: %d", i)) watchDone[i] <- true } watchDone = []chan bool{} diff --git a/table/table.go b/table/table.go index cbb8c21a21..ac3dbd3aa8 100644 --- a/table/table.go +++ b/table/table.go @@ -11,7 +11,6 @@ import ( "github.com/yaoapp/gou/helper" "github.com/yaoapp/kun/exception" "github.com/yaoapp/kun/log" - "github.com/yaoapp/kun/maps" "github.com/yaoapp/yao/config" "github.com/yaoapp/yao/share" ) @@ -73,7 +72,8 @@ func LoadTable(source string, name string) (*Table, error) { } err := helper.UnmarshalFile(input, &table) if err != nil { - exception.Err(err, 400).Ctx(maps.Map{"name": name}).Throw() + // exception.Err(err, 400).Ctx(maps.Map{"name": name}).Throw() + return &table, err } table.loadColumns()