[fix] setup & config
This commit is contained in:
parent
0c438d088a
commit
ce4aaf667f
8 changed files with 37 additions and 25 deletions
|
|
@ -23,6 +23,7 @@ var packCmd = &cobra.Command{
|
||||||
Long: L("Package the application into a single file"),
|
Long: L("Package the application into a single file"),
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
|
config.Init()
|
||||||
cfg := config.Conf
|
cfg := config.Conf
|
||||||
output, err := filepath.Abs(filepath.Join(cfg.Root, "dist"))
|
output, err := filepath.Abs(filepath.Join(cfg.Root, "dist"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -144,8 +144,9 @@ func Execute() {
|
||||||
|
|
||||||
// Boot 设定配置
|
// Boot 设定配置
|
||||||
func Boot() {
|
func Boot() {
|
||||||
root := config.Conf.Root
|
|
||||||
|
|
||||||
|
config.Init()
|
||||||
|
root := config.Conf.Root
|
||||||
if appPath != "" {
|
if appPath != "" {
|
||||||
r, err := filepath.Abs(appPath)
|
r, err := filepath.Abs(appPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -156,7 +157,13 @@ func Boot() {
|
||||||
|
|
||||||
config.Conf = config.LoadFrom(filepath.Join(root, ".env"))
|
config.Conf = config.LoadFrom(filepath.Join(root, ".env"))
|
||||||
|
|
||||||
|
if share.BUILDIN {
|
||||||
|
os.Setenv("YAO_APP_SOURCE", "::binary")
|
||||||
|
config.Conf.AppSource = "::binary"
|
||||||
|
}
|
||||||
|
|
||||||
if yazFile != "" {
|
if yazFile != "" {
|
||||||
|
os.Setenv("YAO_APP_SOURCE", yazFile)
|
||||||
config.Conf.AppSource = yazFile
|
config.Conf.AppSource = yazFile
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ var runCmd = &cobra.Command{
|
||||||
}()
|
}()
|
||||||
|
|
||||||
Boot()
|
Boot()
|
||||||
|
|
||||||
cfg := config.Conf
|
cfg := config.Conf
|
||||||
cfg.Session.IsCLI = true
|
cfg.Session.IsCLI = true
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
|
|
|
||||||
14
cmd/start.go
14
cmd/start.go
|
|
@ -42,12 +42,19 @@ var startCmd = &cobra.Command{
|
||||||
defer share.SessionStop()
|
defer share.SessionStop()
|
||||||
defer plugin.KillAll()
|
defer plugin.KillAll()
|
||||||
|
|
||||||
|
// recive interrupt signal
|
||||||
|
interrupt := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
|
||||||
|
|
||||||
|
Boot()
|
||||||
|
|
||||||
// Setup
|
// Setup
|
||||||
if setup.Check() {
|
if setup.Check() {
|
||||||
go setup.Start()
|
go setup.Start()
|
||||||
select {
|
select {
|
||||||
case <-setup.Done:
|
case <-setup.Done:
|
||||||
setup.Stop()
|
setup.Stop()
|
||||||
|
Boot()
|
||||||
break
|
break
|
||||||
case <-setup.Canceled:
|
case <-setup.Canceled:
|
||||||
os.Exit(1)
|
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
|
// force debug
|
||||||
if startDebug {
|
if startDebug {
|
||||||
config.Development()
|
config.Development()
|
||||||
|
|
|
||||||
|
|
@ -25,10 +25,17 @@ var LogOutput *os.File // 日志文件
|
||||||
// DSLExtensions the dsl file Extensions
|
// DSLExtensions the dsl file Extensions
|
||||||
var DSLExtensions = []string{"*.yao", "*.json", "*.jsonc"}
|
var DSLExtensions = []string{"*.yao", "*.json", "*.jsonc"}
|
||||||
|
|
||||||
func init() {
|
// Init setting
|
||||||
|
func Init() {
|
||||||
|
|
||||||
filename, _ := filepath.Abs(filepath.Join(".", ".env"))
|
filename, _ := filepath.Abs(filepath.Join(".", ".env"))
|
||||||
if _, err := os.Stat(filename); errors.Is(err, os.ErrNotExist) {
|
if _, err := os.Stat(filename); errors.Is(err, os.ErrNotExist) {
|
||||||
Conf = Load()
|
Conf = Load()
|
||||||
|
if Conf.Mode == "production" {
|
||||||
|
Production()
|
||||||
|
} else if Conf.Mode == "development" {
|
||||||
|
Development()
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -88,12 +95,6 @@ func Load() Config {
|
||||||
if !filepath.IsAbs(cfg.DataRoot) {
|
if !filepath.IsAbs(cfg.DataRoot) {
|
||||||
cfg.DataRoot, _ = filepath.Abs(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
|
return cfg
|
||||||
|
|
@ -101,6 +102,7 @@ func Load() Config {
|
||||||
|
|
||||||
// Production 设定为生产环境
|
// Production 设定为生产环境
|
||||||
func Production() {
|
func Production() {
|
||||||
|
os.Setenv("YAO_MODE", "production")
|
||||||
Conf.Mode = "production"
|
Conf.Mode = "production"
|
||||||
log.SetLevel(log.InfoLevel)
|
log.SetLevel(log.InfoLevel)
|
||||||
log.SetFormatter(log.TEXT)
|
log.SetFormatter(log.TEXT)
|
||||||
|
|
@ -113,6 +115,7 @@ func Production() {
|
||||||
|
|
||||||
// Development 设定为开发环境
|
// Development 设定为开发环境
|
||||||
func Development() {
|
func Development() {
|
||||||
|
os.Setenv("YAO_MODE", "development")
|
||||||
Conf.Mode = "development"
|
Conf.Mode = "development"
|
||||||
log.SetLevel(log.TraceLevel)
|
log.SetLevel(log.TraceLevel)
|
||||||
log.SetFormatter(log.TEXT)
|
log.SetFormatter(log.TEXT)
|
||||||
|
|
|
||||||
|
|
@ -252,6 +252,7 @@ func loadApp(root string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
application.Load(app)
|
application.Load(app)
|
||||||
|
config.Init() // Reset Config
|
||||||
data.RemoveApp()
|
data.RemoveApp()
|
||||||
|
|
||||||
} else if strings.HasSuffix(root, ".yaz") {
|
} else if strings.HasSuffix(root, ".yaz") {
|
||||||
|
|
@ -260,6 +261,7 @@ func loadApp(root string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
application.Load(app)
|
application.Load(app)
|
||||||
|
config.Init() // Reset Config
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
app, err = application.OpenFromDisk(root) // Load app from Disk
|
app, err = application.OpenFromDisk(root) // Load app from Disk
|
||||||
|
|
|
||||||
|
|
@ -25,10 +25,7 @@ func Check() bool {
|
||||||
root := appRoot()
|
root := appRoot()
|
||||||
envfile := filepath.Join(root, ".env")
|
envfile := filepath.Join(root, ".env")
|
||||||
if _, err := os.Stat(envfile); err != nil && os.IsNotExist(err) {
|
if _, err := os.Stat(envfile); err != nil && os.IsNotExist(err) {
|
||||||
cfg, err := getConfig()
|
return true
|
||||||
if err != nil || !hasInstalled(cfg) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
|
|
@ -37,8 +34,7 @@ func Check() bool {
|
||||||
func appSourceExists() bool {
|
func appSourceExists() bool {
|
||||||
|
|
||||||
appsource := appSource()
|
appsource := appSource()
|
||||||
|
if strings.HasSuffix(appsource, ".yaz") || strings.HasPrefix(appsource, "::binary") {
|
||||||
if strings.HasSuffix(appsource, ".pkg") || strings.HasPrefix(appsource, "bin:") {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -219,12 +219,14 @@ func makeLog(root string) error {
|
||||||
|
|
||||||
func makeDirs(root string) error {
|
func makeDirs(root string) error {
|
||||||
|
|
||||||
err := os.MkdirAll(filepath.Join(root, "public"), os.ModePerm)
|
if !appSourceExists() {
|
||||||
if err != nil && !os.IsExist(err) {
|
err := os.MkdirAll(filepath.Join(root, "public"), os.ModePerm)
|
||||||
return err
|
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) {
|
if err != nil && !os.IsExist(err) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue