[change] optimize the application initializer
This commit is contained in:
parent
8ffb843495
commit
0317b6558f
6 changed files with 125 additions and 106 deletions
10
cmd/start.go
10
cmd/start.go
|
|
@ -122,14 +122,15 @@ var startCmd = &cobra.Command{
|
||||||
|
|
||||||
err = studio.Load(config.Conf)
|
err = studio.Load(config.Conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(color.RedString(L("Studio Load: %s"), err.Error()))
|
// fmt.Println(color.RedString(L("Studio Load: %s"), err.Error()))
|
||||||
os.Exit(2)
|
log.Error("Studio Load: %s", err.Error())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err := studio.Start(config.Conf)
|
err := studio.Start(config.Conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(color.RedString(L("Studio Start: %s"), err.Error()))
|
log.Error("Studio Start: %s", err.Error())
|
||||||
os.Exit(2)
|
return
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
defer studio.Stop()
|
defer studio.Stop()
|
||||||
|
|
@ -140,7 +141,6 @@ var startCmd = &cobra.Command{
|
||||||
printConnectors(false)
|
printConnectors(false)
|
||||||
printStores(false)
|
printStores(false)
|
||||||
printStudio(false, host)
|
printStudio(false, host)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start Tasks
|
// Start Tasks
|
||||||
|
|
|
||||||
|
|
@ -45,16 +45,16 @@ func LoadFrom(envfile string) Config {
|
||||||
|
|
||||||
file, err := filepath.Abs(envfile)
|
file, err := filepath.Abs(envfile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// log.Warn("Can't load env file. %s", err.Error())
|
cfg := Load()
|
||||||
return Load()
|
ReloadLog()
|
||||||
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// load from env
|
||||||
godotenv.Overload(file)
|
godotenv.Overload(file)
|
||||||
// if err != nil {
|
cfg := Load()
|
||||||
// // log.Warn("Can't load env file. %s", err.Error())
|
ReloadLog()
|
||||||
// }
|
return cfg
|
||||||
|
|
||||||
return Load()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load the config
|
// Load the config
|
||||||
|
|
@ -131,7 +131,10 @@ func ReloadLog() {
|
||||||
|
|
||||||
// OpenLog 打开日志
|
// OpenLog 打开日志
|
||||||
func OpenLog() {
|
func OpenLog() {
|
||||||
if Conf.Log != "" {
|
|
||||||
|
if Conf.Log == "" {
|
||||||
|
Conf.Log = filepath.Join(Conf.Root, "logs", "application.log")
|
||||||
|
}
|
||||||
|
|
||||||
if !filepath.IsAbs(Conf.Log) {
|
if !filepath.IsAbs(Conf.Log) {
|
||||||
Conf.Log = filepath.Join(Conf.Root, Conf.Log)
|
Conf.Log = filepath.Join(Conf.Root, Conf.Log)
|
||||||
|
|
@ -158,7 +161,6 @@ func OpenLog() {
|
||||||
|
|
||||||
log.SetOutput(LogOutput)
|
log.SetOutput(LogOutput)
|
||||||
gin.DefaultWriter = io.MultiWriter(LogOutput)
|
gin.DefaultWriter = io.MultiWriter(LogOutput)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CloseLog 关闭日志
|
// CloseLog 关闭日志
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,6 @@ func Load(cfg config.Config) (err error) {
|
||||||
err = loadApp(cfg.AppSource)
|
err = loadApp(cfg.AppSource)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
printErr(cfg.Mode, "Load Application", err)
|
printErr(cfg.Mode, "Load Application", err)
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load Certs
|
// Load Certs
|
||||||
|
|
|
||||||
|
|
@ -5,22 +5,24 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/yaoapp/xun/capsule"
|
"github.com/yaoapp/xun/capsule"
|
||||||
"github.com/yaoapp/yao/config"
|
"github.com/yaoapp/yao/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Check setup programe
|
// Check if the app is installed
|
||||||
|
// true: start setup, false: start app
|
||||||
func Check() bool {
|
func Check() bool {
|
||||||
|
|
||||||
root := appRoot()
|
// check app source
|
||||||
|
if !appSourceExists() {
|
||||||
appfile := filepath.Join(root, "app.yao")
|
|
||||||
if _, err := os.Stat(appfile); err != nil && os.IsNotExist(err) {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check env file
|
||||||
|
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()
|
cfg, err := getConfig()
|
||||||
|
|
@ -32,6 +34,29 @@ func Check() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func appSourceExists() bool {
|
||||||
|
|
||||||
|
appsource := appSource()
|
||||||
|
|
||||||
|
if strings.HasSuffix(appsource, ".pkg") || strings.HasPrefix(appsource, "bin:") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// check app.yao/app.json/app.jsonc
|
||||||
|
root := appRoot()
|
||||||
|
appfiles := []string{"app.yao", "app.json", "app.jsonc"}
|
||||||
|
exist := false
|
||||||
|
for _, appfile := range appfiles {
|
||||||
|
appfile = filepath.Join(root, appfile)
|
||||||
|
if _, err := os.Stat(appfile); err == nil {
|
||||||
|
exist = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return exist
|
||||||
|
}
|
||||||
|
|
||||||
// ValidateHosting host ports
|
// ValidateHosting host ports
|
||||||
func ValidateHosting(option map[string]string) error {
|
func ValidateHosting(option map[string]string) error {
|
||||||
if option["YAO_PORT"] == "" {
|
if option["YAO_PORT"] == "" {
|
||||||
|
|
@ -87,6 +112,10 @@ func ValidateDB(option map[string]string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func appSource() string {
|
||||||
|
return os.Getenv("YAO_APP_SOURCE")
|
||||||
|
}
|
||||||
|
|
||||||
func appRoot() string {
|
func appRoot() string {
|
||||||
|
|
||||||
root := os.Getenv("YAO_ROOT")
|
root := os.Getenv("YAO_ROOT")
|
||||||
|
|
@ -117,30 +146,32 @@ func getConfig() (config.Config, error) {
|
||||||
|
|
||||||
func hasInstalled(cfg config.Config) bool {
|
func hasInstalled(cfg config.Config) bool {
|
||||||
|
|
||||||
switch cfg.DB.Driver {
|
|
||||||
|
|
||||||
case "sqlite3":
|
|
||||||
if cfg.DB.Primary != nil && len(cfg.DB.Primary) > 0 {
|
|
||||||
|
|
||||||
dbfile, err := filepath.Abs(cfg.DB.Primary[0])
|
|
||||||
if err != nil {
|
|
||||||
return false
|
return false
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := os.Stat(dbfile); err != nil && os.IsNotExist(err) {
|
// switch cfg.DB.Driver {
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
// case "sqlite3":
|
||||||
}
|
// if cfg.DB.Primary != nil && len(cfg.DB.Primary) > 0 {
|
||||||
break
|
|
||||||
|
|
||||||
case "mysql":
|
// dbfile, err := filepath.Abs(cfg.DB.Primary[0])
|
||||||
if cfg.DB.Primary != nil && len(cfg.DB.Primary) > 0 {
|
// if err != nil {
|
||||||
return true
|
// return false
|
||||||
}
|
// }
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
// if _, err := os.Stat(dbfile); err != nil && os.IsNotExist(err) {
|
||||||
|
// return false
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return false
|
||||||
|
// }
|
||||||
|
// break
|
||||||
|
|
||||||
|
// case "mysql":
|
||||||
|
// if cfg.DB.Primary != nil && len(cfg.DB.Primary) > 0 {
|
||||||
|
// return true
|
||||||
|
// }
|
||||||
|
// break
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return false
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,12 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/yaoapp/gou/model"
|
"github.com/yaoapp/gou/model"
|
||||||
"github.com/yaoapp/gou/process"
|
"github.com/yaoapp/gou/process"
|
||||||
"github.com/yaoapp/kun/log"
|
"github.com/yaoapp/kun/log"
|
||||||
|
"github.com/yaoapp/yao/config"
|
||||||
"github.com/yaoapp/yao/data"
|
"github.com/yaoapp/yao/data"
|
||||||
"github.com/yaoapp/yao/engine"
|
"github.com/yaoapp/yao/engine"
|
||||||
"github.com/yaoapp/yao/widgets/app"
|
"github.com/yaoapp/yao/widgets/app"
|
||||||
|
|
@ -98,12 +100,28 @@ func Install(payload map[string]map[string]string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = makeMigrate(root)
|
cfg, err := getConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = makeSetup(root)
|
// Load engine
|
||||||
|
err = engine.Load(cfg)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
engine.Unload()
|
||||||
|
time.Sleep(time.Millisecond * 200)
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Migrage & Setup
|
||||||
|
err = makeMigrate(root, cfg)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = makeSetup(root, cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -206,16 +224,6 @@ func makeDirs(root string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.MkdirAll(filepath.Join(root, "services"), os.ModePerm)
|
|
||||||
if err != nil && !os.IsExist(err) {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = os.MkdirAll(filepath.Join(root, "studio"), 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) {
|
if err != nil && !os.IsExist(err) {
|
||||||
return err
|
return err
|
||||||
|
|
@ -231,9 +239,7 @@ func makeDirs(root string) error {
|
||||||
|
|
||||||
func makeInit(root string) error {
|
func makeInit(root string) error {
|
||||||
|
|
||||||
// if the app.json exist ignore
|
if appSourceExists() {
|
||||||
file := filepath.Join("app.json")
|
|
||||||
if _, err := os.Stat(filepath.Join(root, file)); err == nil { // exists
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -264,17 +270,7 @@ func makeInit(root string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeMigrate(root string) error {
|
func makeMigrate(root string, cfg config.Config) error {
|
||||||
|
|
||||||
cfg, err := getConfig()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = engine.Load(cfg)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do Stuff Here
|
// Do Stuff Here
|
||||||
for _, mod := range model.Models {
|
for _, mod := range model.Models {
|
||||||
|
|
@ -297,17 +293,7 @@ func makeMigrate(root string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeSetup(root string) error {
|
func makeSetup(root string, cfg config.Config) error {
|
||||||
|
|
||||||
cfg, err := getConfig()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = engine.Load(cfg)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if app.Setting != nil && app.Setting.Setup != "" {
|
if app.Setting != nil && app.Setting.Setup != "" {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -137,6 +137,7 @@ func Complete() {
|
||||||
|
|
||||||
// Stop stop the studio api server
|
// Stop stop the studio api server
|
||||||
func Stop() {
|
func Stop() {
|
||||||
|
engine.Unload()
|
||||||
shutdown <- true
|
shutdown <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue