Skip to content

Commit

Permalink
feat : Function name modification
Browse files Browse the repository at this point in the history
  • Loading branch information
wenjianzhang committed Jul 27, 2020
1 parent be19638 commit f3a425a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
36 changes: 24 additions & 12 deletions cmd/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
}
15 changes: 12 additions & 3 deletions cmd/cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/spf13/cobra"
"go-admin/cmd/api"
"go-admin/cmd/migrate"
"go-admin/global"
"go-admin/tools"
"os"
)

Expand All @@ -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)
Expand Down
18 changes: 8 additions & 10 deletions cmd/migrate/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -34,26 +33,25 @@ 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)
}

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

0 comments on commit f3a425a

Please sign in to comment.