Skip to content

Commit

Permalink
Merge pull request #796 from trheyi/main
Browse files Browse the repository at this point in the history
Enhance yao start output
  • Loading branch information
trheyi authored Nov 29, 2024
2 parents 656bc16 + c6cdf11 commit 675a0c4
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 948 deletions.
13 changes: 0 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@ artifacts-linux: clean
echo "BASE=__yao_admin_root" > ../xgen-v1.0/packages/xgen/.env
cd ../xgen-v1.0 && pnpm install --no-frozen-lockfile && pnpm run build

# Setup UI
cd ../xgen-v1.0/packages/setup && pnpm install --no-frozen-lockfile && pnpm run build

# Init Application
cd ../yao-init && rm -rf .git
cd ../yao-init && rm -rf .gitignore
Expand All @@ -169,7 +166,6 @@ artifacts-linux: clean
# ** new repository: https://github.com/YaoApp/dui.git **
mkdir -p .tmp/data/xgen
cp -r ./ui .tmp/data/ui
cp -r ../xgen-v1.0/packages/setup/build .tmp/data/xgen/setup
cp -r ../xgen-v1.0/packages/xgen/dist .tmp/data/xgen/v1.0
cp -r ../yao-init .tmp/data/init
cp -r yao .tmp/data/
Expand Down Expand Up @@ -207,9 +203,6 @@ artifacts-macos: clean
echo "BASE=__yao_admin_root" > ../xgen-v1.0/packages/xgen/.env
cd ../xgen-v1.0 && pnpm install --no-frozen-lockfile && pnpm run build

# Setup UI
cd ../xgen-v1.0/packages/setup && pnpm install --no-frozen-lockfile && pnpm run build

# Init Application
cd ../yao-init && rm -rf .git
cd ../yao-init && rm -rf .gitignore
Expand All @@ -228,7 +221,6 @@ artifacts-macos: clean
# ** new repository: https://github.com/YaoApp/dui.git **
mkdir -p .tmp/data/xgen
cp -r ./ui .tmp/data/ui
cp -r ../xgen-v1.0/packages/setup/build .tmp/data/xgen/setup
cp -r ../xgen-v1.0/packages/xgen/dist .tmp/data/xgen/v1.0
cp -r ../yao-init .tmp/data/init
cp -r yao .tmp/data/
Expand Down Expand Up @@ -296,10 +288,6 @@ release: clean
echo "BASE=__yao_admin_root" > .tmp/xgen/v1.0/packages/xgen/.env
cd .tmp/xgen/v1.0 && pnpm install --no-frozen-lockfile && pnpm run build

# Setup UI
cd .tmp/xgen/v1.0/packages/setup && pnpm install --no-frozen-lockfile && pnpm run build


# Checkout init
git clone https://github.com/YaoApp/yao-init.git .tmp/yao-init
rm -rf .tmp/yao-init/.git
Expand All @@ -320,7 +308,6 @@ release: clean
cp -r ./yao .tmp/data/yao
cp -r ./sui/libsui .tmp/data/libsui
cp -r .tmp/xgen/v0.9/dist .tmp/data/xgen/v0.9
cp -r .tmp/xgen/v1.0/packages/setup/build .tmp/data/xgen/setup
cp -r .tmp/xgen/v1.0/packages/xgen/dist .tmp/data/xgen/v1.0
cp -r .tmp/yao-init .tmp/data/init
go-bindata -fs -pkg data -o data/bindata.go -prefix ".tmp/data/" .tmp/data/...
Expand Down
162 changes: 107 additions & 55 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/yaoapp/yao/service"
"github.com/yaoapp/yao/setup"
"github.com/yaoapp/yao/share"
"github.com/yaoapp/yao/studio"
itask "github.com/yaoapp/yao/task"
)

Expand All @@ -49,17 +48,27 @@ var startCmd = &cobra.Command{
Boot()

// Setup
if setup.Check() {
go setup.Start()
select {
case <-setup.Done:
setup.Stop()
Boot()
break
case <-setup.Canceled:
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)
break
}
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
Expand Down Expand Up @@ -103,50 +112,59 @@ var startCmd = &cobra.Command{
fmt.Println(color.WhiteString(L("Root")), color.GreenString(" %s", root))
}

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

root, _ := adminRoot()
urls := []string{fmt.Sprintf("http://%s:%s", host, port)}
if host == "0.0.0.0" {
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" {

// Start Studio Server
go func() {

err = studio.Load(config.Conf)
if err != nil {
// fmt.Println(color.RedString(L("Studio Load: %s"), err.Error()))
log.Error("Studio Load: %s", err.Error())
return
}

err := studio.Start(config.Conf)
if err != nil {
log.Error("Studio Start: %s", err.Error())
return
}
}()
defer studio.Stop()
// Yao Studio will be deprecated in the future
// go func() {

// err = studio.Load(config.Conf)
// if err != nil {
// // fmt.Println(color.RedString(L("Studio Load: %s"), err.Error()))
// log.Error("Studio Load: %s", err.Error())
// return
// }

// err := studio.Start(config.Conf)
// if err != nil {
// log.Error("Studio Start: %s", err.Error())
// return
// }
// }()
// defer studio.Stop()

printApis(false)
printTasks(false)
printSchedules(false)
printConnectors(false)
printStores(false)
printStudio(false, host)
// printStudio(false, host)

}

for _, url := range urls {
fmt.Println(color.CyanString("\n%s", url))
fmt.Println(color.WhiteString("--------------------------"))
fmt.Println(color.WhiteString(L("Website")), color.GreenString(" %s", url))
fmt.Println(color.WhiteString(L("Admin")), color.GreenString(" %s/%s/login/admin", url, strings.Trim(root, "/")))
fmt.Println(color.WhiteString(L("API")), color.GreenString(" %s/api", url))
}
fmt.Println("")

// Print welcome message for the new application
if isnew {
printWelcome()
}

// Start Tasks
Expand All @@ -161,7 +179,7 @@ var startCmd = &cobra.Command{
srv, err := service.Start(config.Conf)
defer func() {
service.Stop(srv)
fmt.Println(color.GreenString(L("✨EXITED✨")))
fmt.Println(color.GreenString(L("✨Exited successfully!")))
}()

if err != nil {
Expand Down Expand Up @@ -193,11 +211,12 @@ var startCmd = &cobra.Command{

switch v {
case http.READY:
fmt.Println(color.GreenString(L("✨LISTENING✨")))
fmt.Println(color.GreenString(L("✨Server is up and running...")))
fmt.Println(color.GreenString("✨Ctrl+C to stop"))
break

case http.CLOSED:
fmt.Println(color.GreenString(L("✨EXITED✨")))
fmt.Println(color.GreenString(L("✨Exited successfully!")))
watchDone <- 1
return

Expand All @@ -218,6 +237,30 @@ var startCmd = &cobra.Command{
},
}

func install() error {
// Copy the app source files from the binary
err := setup.Install(config.Conf.Root)
if err != nil {
return err
}

// Reload the application engine
Boot()

// load the application engine
err = engine.Load(config.Conf, engine.LoadOption{Action: "start"})
if err != nil {
return err
}

err = setup.Initialize(config.Conf.Root, config.Conf)
if err != nil {
return err
}

return nil
}

func adminRoot() (string, int) {
adminRoot := "/yao/"
if share.App.AdminRoot != "" {
Expand All @@ -229,6 +272,15 @@ func adminRoot() (string, int) {
return adminRoot, adminRootLen
}

func printWelcome() {
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("")
}

func printConnectors(silent bool) {

if len(connector.Connectors) == 0 {
Expand All @@ -246,8 +298,8 @@ func printConnectors(silent bool) {
fmt.Println(color.WhiteString(L("Connectors List (%d)"), len(connector.Connectors)))
fmt.Println(color.WhiteString("---------------------------------"))
for name := range connector.Connectors {
fmt.Printf(color.CyanString("[Connector]"))
fmt.Printf(color.WhiteString(" %s\t loaded\n", name))
fmt.Print(color.CyanString("[Connector]"))
fmt.Print(color.WhiteString(" %s\t loaded\n", name))
}
}

Expand All @@ -267,8 +319,8 @@ func printStores(silent bool) {
fmt.Println(color.WhiteString(L("Stores List (%d)"), len(connector.Connectors)))
fmt.Println(color.WhiteString("---------------------------------"))
for name := range store.Pools {
fmt.Printf(color.CyanString("[Store]"))
fmt.Printf(color.WhiteString(" %s\t loaded\n", name))
fmt.Print(color.CyanString("[Store]"))
fmt.Print(color.WhiteString(" %s\t loaded\n", name))
}
}

Expand All @@ -285,13 +337,13 @@ func printStudio(silent bool, host string) {
fmt.Println(color.WhiteString("\n---------------------------------"))
fmt.Println(color.WhiteString(L("Yao Studio Server")))
fmt.Println(color.WhiteString("---------------------------------"))
fmt.Printf(color.CyanString("HOST : "))
fmt.Printf(color.WhiteString(" %s\n", config.Conf.Host))
fmt.Printf(color.CyanString("PORT : "))
fmt.Printf(color.WhiteString(" %d\n", config.Conf.Studio.Port))
fmt.Print(color.CyanString("HOST : "))
fmt.Print(color.WhiteString(" %s\n", config.Conf.Host))
fmt.Print(color.CyanString("PORT : "))
fmt.Print(color.WhiteString(" %d\n", config.Conf.Studio.Port))
if config.Conf.Studio.Auto {
fmt.Printf(color.CyanString("SECRET: "))
fmt.Printf(color.WhiteString(" %s\n", config.Conf.Studio.Secret))
fmt.Print(color.CyanString("SECRET: "))
fmt.Print(color.WhiteString(" %s\n", config.Conf.Studio.Secret))
}
}

Expand Down Expand Up @@ -320,8 +372,8 @@ func printSchedules(silent bool) {
if sch.TaskName != "" {
process = fmt.Sprintf("Task: %s", sch.TaskName)
}
fmt.Printf(color.CyanString("[Schedule] %s %s", sch.Schedule, name))
fmt.Printf(color.WhiteString("\t%s\t%s\n", sch.Name, process))
fmt.Print(color.CyanString("[Schedule] %s %s", sch.Schedule, name))
fmt.Print(color.WhiteString("\t%s\t%s\n", sch.Name, process))
}
}

Expand All @@ -342,8 +394,8 @@ func printTasks(silent bool) {
fmt.Println(color.WhiteString(L("Tasks List (%d)"), len(task.Tasks)))
fmt.Println(color.WhiteString("---------------------------------"))
for _, t := range task.Tasks {
fmt.Printf(color.CyanString("[Task] %s", t.Option.Name))
fmt.Printf(color.WhiteString("\t workers: %d\n", t.Option.WorkerNums))
fmt.Print(color.CyanString("[Task] %s", t.Option.Name))
fmt.Print(color.WhiteString("\t workers: %d\n", t.Option.WorkerNums))
}
}

Expand Down
14 changes: 8 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 675a0c4

Please sign in to comment.