diff --git a/cmd/pack.go b/cmd/pack.go index 078c31cb..819b66ff 100644 --- a/cmd/pack.go +++ b/cmd/pack.go @@ -23,6 +23,7 @@ var packCmd = &cobra.Command{ Long: L("Package the application into a single file"), Run: func(cmd *cobra.Command, args []string) { + config.Init() cfg := config.Conf output, err := filepath.Abs(filepath.Join(cfg.Root, "dist")) if err != nil { diff --git a/cmd/root.go b/cmd/root.go index d8f6f02c..045c29f2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -144,8 +144,9 @@ func Execute() { // Boot 设定配置 func Boot() { - root := config.Conf.Root + config.Init() + root := config.Conf.Root if appPath != "" { r, err := filepath.Abs(appPath) if err != nil { @@ -156,7 +157,13 @@ func Boot() { config.Conf = config.LoadFrom(filepath.Join(root, ".env")) + if share.BUILDIN { + os.Setenv("YAO_APP_SOURCE", "::binary") + config.Conf.AppSource = "::binary" + } + if yazFile != "" { + os.Setenv("YAO_APP_SOURCE", yazFile) config.Conf.AppSource = yazFile } diff --git a/cmd/run.go b/cmd/run.go index 9b888a7f..8c25e2ef 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -37,6 +37,7 @@ var runCmd = &cobra.Command{ }() Boot() + cfg := config.Conf cfg.Session.IsCLI = true if len(args) < 1 { diff --git a/cmd/start.go b/cmd/start.go index 4735c1db..dac0d7b8 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -42,12 +42,19 @@ var startCmd = &cobra.Command{ defer share.SessionStop() defer plugin.KillAll() + // recive interrupt signal + interrupt := make(chan os.Signal, 1) + signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT) + + Boot() + // Setup if setup.Check() { go setup.Start() select { case <-setup.Done: setup.Stop() + Boot() break case <-setup.Canceled: os.Exit(1) @@ -55,13 +62,6 @@ var startCmd = &cobra.Command{ } } - // recive interrupt signal - interrupt := make(chan os.Signal, 1) - signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT) - - // defer service.Stop(func() { fmt.Println(L("Service stopped")) }) - Boot() - // force debug if startDebug { config.Development() diff --git a/config/config.go b/config/config.go index 103b1cf0..87ac8d65 100644 --- a/config/config.go +++ b/config/config.go @@ -25,10 +25,17 @@ var LogOutput *os.File // 日志文件 // DSLExtensions the dsl file Extensions var DSLExtensions = []string{"*.yao", "*.json", "*.jsonc"} -func init() { +// Init setting +func Init() { + filename, _ := filepath.Abs(filepath.Join(".", ".env")) if _, err := os.Stat(filename); errors.Is(err, os.ErrNotExist) { Conf = Load() + if Conf.Mode == "production" { + Production() + } else if Conf.Mode == "development" { + Development() + } return } @@ -88,12 +95,6 @@ func Load() Config { if !filepath.IsAbs(cfg.DataRoot) { cfg.DataRoot, _ = filepath.Abs(cfg.DataRoot) } - - if _, err := os.Stat(cfg.DataRoot); errors.Is(err, os.ErrNotExist) { - if err := os.MkdirAll(cfg.DataRoot, os.ModePerm); err != nil { - exception.New("Can't create data root %s", 500, err.Error()).Throw() - } - } } return cfg @@ -101,6 +102,7 @@ func Load() Config { // Production 设定为生产环境 func Production() { + os.Setenv("YAO_MODE", "production") Conf.Mode = "production" log.SetLevel(log.InfoLevel) log.SetFormatter(log.TEXT) @@ -113,6 +115,7 @@ func Production() { // Development 设定为开发环境 func Development() { + os.Setenv("YAO_MODE", "development") Conf.Mode = "development" log.SetLevel(log.TraceLevel) log.SetFormatter(log.TEXT) diff --git a/engine/load.go b/engine/load.go index 54b856ba..518fc2c8 100644 --- a/engine/load.go +++ b/engine/load.go @@ -252,6 +252,7 @@ func loadApp(root string) error { } application.Load(app) + config.Init() // Reset Config data.RemoveApp() } else if strings.HasSuffix(root, ".yaz") { @@ -260,6 +261,7 @@ func loadApp(root string) error { return err } application.Load(app) + config.Init() // Reset Config } else { app, err = application.OpenFromDisk(root) // Load app from Disk diff --git a/setup/check.go b/setup/check.go index fd51af51..e6bfa131 100644 --- a/setup/check.go +++ b/setup/check.go @@ -25,10 +25,7 @@ func Check() bool { root := appRoot() envfile := filepath.Join(root, ".env") if _, err := os.Stat(envfile); err != nil && os.IsNotExist(err) { - cfg, err := getConfig() - if err != nil || !hasInstalled(cfg) { - return true - } + return true } return false @@ -37,8 +34,7 @@ func Check() bool { func appSourceExists() bool { appsource := appSource() - - if strings.HasSuffix(appsource, ".pkg") || strings.HasPrefix(appsource, "bin:") { + if strings.HasSuffix(appsource, ".yaz") || strings.HasPrefix(appsource, "::binary") { return true } diff --git a/setup/install.go b/setup/install.go index a487b0d1..090682fd 100644 --- a/setup/install.go +++ b/setup/install.go @@ -219,12 +219,14 @@ func makeLog(root string) error { func makeDirs(root string) error { - err := os.MkdirAll(filepath.Join(root, "public"), os.ModePerm) - if err != nil && !os.IsExist(err) { - return err + if !appSourceExists() { + err := os.MkdirAll(filepath.Join(root, "public"), os.ModePerm) + if err != nil && !os.IsExist(err) { + return err + } } - err = os.MkdirAll(filepath.Join(root, "logs"), os.ModePerm) + err := os.MkdirAll(filepath.Join(root, "logs"), os.ModePerm) if err != nil && !os.IsExist(err) { return err }