diff --git a/cmd/start.go b/cmd/start.go index eff2e1a0..321aac56 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -49,13 +49,27 @@ var startCmd = &cobra.Command{ Boot() // Setup - isNewApp := false - if !setup.SourceExists() { + isnew := false + if setup.IsEmptyDir(config.Conf.Root) { + + // In Yao App + if setup.InYaoApp(config.Conf.Root) { + fmt.Println(color.RedString(L("Please run the command in the root directory of project"))) + os.Exit(1) + } + + // Install the init app if err := install(); err != nil { fmt.Println(color.RedString(L("Install: %s"), err.Error())) os.Exit(1) } - isNewApp = true + isnew = true + } + + // Is Yao App + if !setup.IsYaoApp(config.Conf.Root) { + fmt.Println(color.RedString(L("yao.app not found"))) + os.Exit(1) } // force debug @@ -105,17 +119,6 @@ var startCmd = &cobra.Command{ urls, _ = setup.URLs(config.Conf) } - fmt.Println(color.WhiteString(L("Runtime")), color.GreenString(" %s", runtimeMode)) - fmt.Println(color.WhiteString(L("Data")), color.GreenString(" %s", dataRoot)) - fmt.Println(color.WhiteString(L("Listening")), color.GreenString(" %s:%d", config.Conf.Host, config.Conf.Port)) - for _, url := range urls { - fmt.Println(color.CyanString("\n%s", url)) - fmt.Println(color.WhiteString("--------------------------")) - fmt.Println(color.WhiteString(L("Frontend")), color.GreenString(" %s", url)) - fmt.Println(color.WhiteString(L("Dashboard")), color.GreenString(" %s/%s/login/admin", url, strings.Trim(root, "/"))) - fmt.Println(color.WhiteString(L("API")), color.GreenString(" %s/api", url)) - } - // print the messages under the development mode if mode == "development" { @@ -144,10 +147,22 @@ var startCmd = &cobra.Command{ printStores(false) printStudio(false, host) - // Print welcome message for the new application - if isNewApp { - printWelcome() - } + } + + fmt.Println(color.WhiteString(L("Runtime")), color.GreenString(" %s", runtimeMode)) + fmt.Println(color.WhiteString(L("Data")), color.GreenString(" %s", dataRoot)) + fmt.Println(color.WhiteString(L("Listening")), color.GreenString(" %s:%d", config.Conf.Host, config.Conf.Port)) + for _, url := range urls { + fmt.Println(color.CyanString("\n%s", url)) + fmt.Println(color.WhiteString("--------------------------")) + fmt.Println(color.WhiteString(L("Frontend")), color.GreenString(" %s", url)) + fmt.Println(color.WhiteString(L("Dashboard")), color.GreenString(" %s/%s/login/admin", url, strings.Trim(root, "/"))) + fmt.Println(color.WhiteString(L("API")), color.GreenString(" %s/api", url)) + } + + // Print welcome message for the new application + if isnew { + printWelcome() } // Start Tasks @@ -195,7 +210,7 @@ var startCmd = &cobra.Command{ switch v { case http.READY: fmt.Println(color.GreenString(L("โœจServer is up and running..."))) - fmt.Println(color.BlackString("โœจCtrl+C to stop")) + fmt.Println(color.GreenString("โœจCtrl+C to stop")) break case http.CLOSED: @@ -256,11 +271,11 @@ func adminRoot() (string, int) { } func printWelcome() { - fmt.Println(color.BlueString("\n---------------------------------")) - fmt.Println(color.BlueString(L("๐ŸŽ‰ Welcome to Yao ๐ŸŽ‰ "))) - fmt.Println(color.BlueString("---------------------------------")) - fmt.Println(color.WhiteString("๐Ÿ“š Documentation:"), color.BlueString("https://yaoapps.com/docs")) - fmt.Println(color.WhiteString("๐Ÿ’ฌ Build App via Chat:"), color.BlueString("https://moapi.ai")) + fmt.Println(color.CyanString("\n---------------------------------")) + fmt.Println(color.CyanString(L("๐ŸŽ‰ Welcome to Yao ๐ŸŽ‰ "))) + fmt.Println(color.CyanString("---------------------------------")) + fmt.Println(color.WhiteString("๐Ÿ“š Documentation:"), color.CyanString("https://yaoapps.com/docs")) + fmt.Println(color.WhiteString("๐Ÿ’ฌ Build App via Chat:"), color.CyanString("https://moapi.ai")) fmt.Println("") } diff --git a/config/config.go b/config/config.go index 7f61a7b9..105784b8 100644 --- a/config/config.go +++ b/config/config.go @@ -150,17 +150,19 @@ func OpenLog() { logfile, err := filepath.Abs(Conf.Log) if err != nil { - log.With(log.F{"file": logfile}).Error(err.Error()) return } logpath := filepath.Dir(logfile) - if _, err := os.Stat(logpath); os.IsNotExist(err) { - if err := os.MkdirAll(logpath, os.ModePerm); err != nil { - log.With(log.F{"file": logfile}).Error(err.Error()) - return - } + + // Check if the log path exists + if _, err := os.Stat(logpath); errors.Is(err, os.ErrNotExist) { + LogOutput, _ := os.OpenFile(os.DevNull, os.O_WRONLY, 0666) + log.SetOutput(LogOutput) + gin.DefaultWriter = io.MultiWriter(LogOutput) + return } + LogOutput = &lumberjack.Logger{ Filename: logfile, MaxSize: Conf.LogMaxSize, // megabytes diff --git a/setup/check.go b/setup/check.go index 4b421212..fe59160e 100644 --- a/setup/check.go +++ b/setup/check.go @@ -1,41 +1,44 @@ package setup import ( + "fmt" "os" "path/filepath" "github.com/yaoapp/yao/config" ) -// SourceExists check if the app source exists -func SourceExists() bool { - return appSourceExists() +// InYaoApp Check if the current directory is a yao app +func InYaoApp(root string) bool { + // Check current directory and parent directories + for root != "/" { + if IsYaoApp(root) { + return true + } + root = filepath.Dir(root) + } + return false } -func appSourceExists() bool { - - root := appRoot() - if isEmptyDir(root) { - return false - } - - // check app.yao/app.json/app.jsonc +// IsYaoApp Check if the directory is a yao app +func IsYaoApp(root string) bool { appfiles := []string{"app.yao", "app.json", "app.jsonc"} - exist := false + yaoapp := false for _, appfile := range appfiles { appfile = filepath.Join(root, appfile) if _, err := os.Stat(appfile); err == nil { - exist = true + yaoapp = true break } } - - return exist + return yaoapp } -func isEmptyDir(dir string) bool { +// IsEmptyDir Check if the directory is empty +func IsEmptyDir(dir string) bool { f, err := os.Open(dir) if err != nil { + fmt.Println("Can't open the directory: ", err) return true } defer f.Close() diff --git a/setup/install.go b/setup/install.go index c6e547c9..c45a0fe3 100644 --- a/setup/install.go +++ b/setup/install.go @@ -1,15 +1,12 @@ package setup import ( - "fmt" "os" "path/filepath" "strings" - "github.com/google/uuid" "github.com/yaoapp/gou/model" "github.com/yaoapp/gou/process" - v8 "github.com/yaoapp/gou/runtime/v8" "github.com/yaoapp/kun/log" "github.com/yaoapp/yao/config" "github.com/yaoapp/yao/data" @@ -48,7 +45,7 @@ func Initialize(root string, cfg config.Config) error { func makeInit(root string) error { - if appSourceExists() { + if !IsEmptyDir(root) { return nil } @@ -106,31 +103,6 @@ func makeSetup(cfg config.Config) error { if app.Setting != nil && app.Setting.Setup != "" { - if strings.HasPrefix(app.Setting.Setup, "studio.") { - names := strings.Split(app.Setting.Setup, ".") - if len(names) < 3 { - return fmt.Errorf("setup studio script %s error", app.Setting.Setup) - } - - service := strings.Join(names[1:len(names)-1], ".") - method := names[len(names)-1] - - script, err := v8.SelectRoot(service) - if err != nil { - return err - } - - sid := uuid.NewString() - ctx, err := script.NewContext(fmt.Sprintf("%v", sid), nil) - if err != nil { - return err - } - defer ctx.Close() - - _, err = ctx.Call(method) - return err - } - p, err := process.Of(app.Setting.Setup, cfg) if err != nil { return err