refactor: code review

This commit is contained in:
meilin.huang
2022-08-10 19:46:17 +08:00
parent f2d9e7786d
commit f913510d3c
16 changed files with 232 additions and 128 deletions

View File

@@ -1,16 +1,17 @@
package starter
import (
"fmt"
"mayfly-go/pkg/config"
"mayfly-go/pkg/global"
)
func PrintBanner() {
global.Log.Print(`
__ _
_ __ ___ __ _ _ _ / _| |_ _ __ _ ___
| '_ ' _ \ / _' | | | | |_| | | | |_____ / _' |/ _ \
| | | | | | (_| | |_| | _| | |_| |_____| (_| | (_) |
|_| |_| |_|\__,_|\__, |_| |_|\__, | \__, |\___/
|___/ |___/ |___/
`)
func printBanner() {
global.Log.Print(fmt.Sprintf(`
__ _
_ __ ___ __ _ _ _ / _| |_ _ __ _ ___
| '_ ' _ \ / _' | | | | |_| | | | |_____ / _' |/ _ \
| | | | | | (_| | |_| | _| | |_| |_____| (_| | (_) | version: %s
|_| |_| |_|\__,_|\__, |_| |_|\__, | \__, |\___/
|___/ |___/ |___/ `, config.Version))
}

View File

@@ -10,11 +10,11 @@ import (
"gorm.io/gorm/schema"
)
func InitDb() {
global.Db = GormMysql()
func initDb() {
global.Db = gormMysql()
}
func GormMysql() *gorm.DB {
func gormMysql() *gorm.DB {
m := config.Conf.Mysql
if m == nil || m.Dbname == "" {
global.Log.Panic("未找到数据库配置信息")

23
server/pkg/starter/run.go Normal file
View File

@@ -0,0 +1,23 @@
package starter
import (
"mayfly-go/pkg/config"
"mayfly-go/pkg/ctx"
"mayfly-go/pkg/logger"
)
func RunWebServer() {
// 初始化config.yml配置文件映射信息
config.Init()
// 初始化日志配置信息
logger.Init()
// 初始化jwt key与expire time等
ctx.InitTokenConfig()
// 打印banner
printBanner()
// 初始化并赋值数据库全局变量
initDb()
// 运行web服务
runWebServer()
}

View File

@@ -8,7 +8,7 @@ import (
"mayfly-go/pkg/global"
)
func RunWebServer() {
func runWebServer() {
// 权限处理器
ctx.UseBeforeHandlerInterceptor(ctx.PermissionHandler)
// 日志处理器
@@ -21,11 +21,7 @@ func RunWebServer() {
server := config.Conf.Server
port := server.GetPort()
if app := config.Conf.App; app != nil {
global.Log.Infof("%s- Listening and serving HTTP on %s", app.GetAppInfo(), port)
} else {
global.Log.Infof("Listening and serving HTTP on %s", port)
}
global.Log.Infof("Listening and serving HTTP on %s", port)
var err error
if server.Tls != nil && server.Tls.Enable {