[fix] setup & config

This commit is contained in:
Max 2023-04-24 17:38:29 +08:00
parent 0c438d088a
commit ce4aaf667f
8 changed files with 37 additions and 25 deletions

View file

@ -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 {

View file

@ -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
}

View file

@ -37,6 +37,7 @@ var runCmd = &cobra.Command{
}()
Boot()
cfg := config.Conf
cfg.Session.IsCLI = true
if len(args) < 1 {

View file

@ -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()

View file

@ -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)

View file

@ -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

View file

@ -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
}

View file

@ -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
}