diff --git a/cmd/api/server.go b/cmd/api/server.go index 5ac3e6f2f..bc37f8650 100644 --- a/cmd/api/server.go +++ b/cmd/api/server.go @@ -9,6 +9,7 @@ import ( "go-admin/database" "go-admin/global" mycasbin "go-admin/pkg/casbin" + "go-admin/pkg/logger" "go-admin/router" "go-admin/tools" "go-admin/tools/config" @@ -43,17 +44,18 @@ func init() { } func setup() { + //1. 读取配置 - config.ConfigSetup(configYml) + config.Setup(configYml) //2. 设置日志 - tools.InitLogger() + logger.Setup() //3. 初始化数据库链接 database.Setup(config.DatabaseConfig.Driver) - + //4. 接口访问控制加载 mycasbin.Setup() usageStr := `starting api server` - tools.Logger.Printf("%s\n", usageStr) + global.Logger.Info(usageStr) } func run() error { @@ -75,20 +77,25 @@ func run() error { go func() { // 服务连接 - if config.ApplicationConfig.IsHttps { + if config.SslConfig.Enable { if err := srv.ListenAndServeTLS(config.SslConfig.Pem, config.SslConfig.KeyStr); err != nil && err != http.ErrServerClosed { - tools.Logger.Fatalf("listen: %s \r\n", err) + global.Logger.Fatal("listen: ", err) } } else { if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { - tools.Logger.Fatalf("listen: %s \r\n", err) + global.Logger.Fatal("listen: ", err) } } }() content, _ := ioutil.ReadFile("./static/go-admin.txt") - fmt.Println(string(content)) - fmt.Printf("%s Server Run http://%s:%s/ \r\n", tools.GetCurrentTimeStr(), config.ApplicationConfig.Host, config.ApplicationConfig.Port) - fmt.Printf("%s Swagger URL http://%s:%s/swagger/index.html \r\n", tools.GetCurrentTimeStr(), config.ApplicationConfig.Host, config.ApplicationConfig.Port) + fmt.Println(tools.Red(string(content))) + tip() + fmt.Println(tools.Green("Server run at:")) + fmt.Printf("- Local: http://localhost:%s/ \r\n", config.ApplicationConfig.Port) + fmt.Printf("- Network: http://%s:%s/ \r\n", tools.GetLocaHonst(), config.ApplicationConfig.Port) + fmt.Println(tools.Green("Swagger run at:")) + fmt.Printf("- Local: http://localhost:%s/swagger/index.html \r\n", config.ApplicationConfig.Port) + fmt.Printf("- Network: http://%s:%s/swagger/index.html \r\n", tools.GetLocaHonst(), config.ApplicationConfig.Port) fmt.Printf("%s Enter Control + C Shutdown Server \r\n", tools.GetCurrentTimeStr()) // 等待中断信号以优雅地关闭服务器(设置 5 秒的超时时间) quit := make(chan os.Signal) @@ -99,8 +106,13 @@ func run() error { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() if err := srv.Shutdown(ctx); err != nil { - tools.Logger.Fatal("Server Shutdown:", err) + global.Logger.Fatal("Server Shutdown:", err) } - tools.Logger.Println("Server exiting") + global.Logger.Println("Server exiting") return nil } + +func tip() { + usageStr := `欢迎使用 `+ tools.Green( `go-admin ` +global.Version) + ` 可以使用 ` + tools.Red(`-h`) + ` 查看命令` + fmt.Printf("%s \n\n", usageStr) +} diff --git a/cmd/cobra.go b/cmd/cobra.go index 68eeae15c..11a208354 100644 --- a/cmd/cobra.go +++ b/cmd/cobra.go @@ -6,6 +6,8 @@ import ( "github.com/spf13/cobra" "go-admin/cmd/api" "go-admin/cmd/migrate" + "go-admin/global" + "go-admin/tools" "os" ) @@ -17,17 +19,24 @@ var rootCmd = &cobra.Command{ Long: `go-admin`, Args: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { - return errors.New("requires at least one arg") + tip() + return errors.New(tools.Red("requires at least one arg")) } return nil }, PersistentPreRunE: func(*cobra.Command, []string) error { return nil }, Run: func(cmd *cobra.Command, args []string) { - usageStr := `go-admin 1.0.0 欢迎使用,可以使用 -h 查看命令` - fmt.Printf("%s\n", usageStr) + tip() }, } +func tip() { + usageStr := `欢迎使用 `+ tools.Green( `go-admin ` +global.Version) + ` 可以使用 ` + tools.Red(`-h`) + ` 查看命令` + usageStr1 := `也可以参考 http://doc.zhangwj.com/go-admin-site/guide/ksks.html 里边的【启动】章节` + fmt.Printf("%s\n", usageStr) + fmt.Printf("%s\n", usageStr1) +} + func init() { rootCmd.AddCommand(api.StartCmd) rootCmd.AddCommand(migrate.StartCmd) diff --git a/cmd/migrate/server.go b/cmd/migrate/server.go index 3d4c8b80d..a7094ca70 100644 --- a/cmd/migrate/server.go +++ b/cmd/migrate/server.go @@ -2,12 +2,11 @@ package migrate import ( "fmt" - log "github.com/sirupsen/logrus" "go-admin/database" - orm "go-admin/global" + "go-admin/global" "go-admin/models" "go-admin/models/gorm" - "go-admin/tools" + "go-admin/pkg/logger" "go-admin/tools/config" "github.com/spf13/cobra" @@ -34,18 +33,17 @@ func run() { usage := `start init` fmt.Println(usage) //1. 读取配置 - config.ConfigSetup(configYml) + config.Setup(configYml) //2. 设置日志 - tools.InitLogger() + logger.Setup() //3. 初始化数据库链接 database.Setup(config.DatabaseConfig.Driver) - //4. 数据库迁移 _ = migrateModel() - log.Println("数据库结构初始化成功!") + fmt.Println("数据库结构初始化成功!") //5. 数据初始化完成 if err := models.InitDb(); err != nil { - log.Fatal("数据库基础数据初始化失败!") + global.Logger.Fatal("数据库基础数据初始化失败!") } usage = `数据库基础数据初始化成功` fmt.Println(usage) @@ -53,7 +51,7 @@ func run() { func migrateModel() error { if config.DatabaseConfig.Driver == "mysql" { - orm.Eloquent = orm.Eloquent.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4") + global.Eloquent = global.Eloquent.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4") } - return gorm.AutoMigrate(orm.Eloquent) + return gorm.AutoMigrate(global.Eloquent) }