refactor: 后端包结构重构、去除无用的文件

This commit is contained in:
meilin.huang
2022-06-02 17:41:11 +08:00
parent 51d06ab206
commit b2dc9dff0b
234 changed files with 749 additions and 816 deletions

View File

@@ -0,0 +1,16 @@
package starter
import (
"mayfly-go/pkg/global"
)
func PrintBanner() {
global.Log.Print(`
__ _
_ __ ___ __ _ _ _ / _| |_ _ __ _ ___
| '_ ' _ \ / _' | | | | |_| | | | |_____ / _' |/ _ \
| | | | | | (_| | |_| | _| | |_| |_____| (_| | (_) |
|_| |_| |_|\__,_|\__, |_| |_|\__, | \__, |\___/
|___/ |___/ |___/
`)
}

View File

@@ -0,0 +1,45 @@
package starter
import (
"mayfly-go/pkg/config"
"mayfly-go/pkg/global"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
)
func InitDb() {
global.Db = GormMysql()
}
func GormMysql() *gorm.DB {
m := config.Conf.Mysql
if m == nil || m.Dbname == "" {
global.Log.Panic("未找到数据库配置信息")
return nil
}
global.Log.Infof("连接mysql [%s]", m.Host)
mysqlConfig := mysql.Config{
DSN: m.Dsn(), // DSN data source name
DefaultStringSize: 191, // string 类型字段的默认长度
DisableDatetimePrecision: true, // 禁用 datetime 精度MySQL 5.6 之前的数据库不支持
DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引
DontSupportRenameColumn: true, // 用 `change` 重命名列MySQL 8 之前的数据库和 MariaDB 不支持重命名列
SkipInitializeWithVersion: false, // 根据版本自动配置
}
ormConfig := &gorm.Config{NamingStrategy: schema.NamingStrategy{
TablePrefix: "t_",
SingularTable: true,
}, Logger: logger.Default.LogMode(logger.Silent)}
if db, err := gorm.Open(mysql.New(mysqlConfig), ormConfig); err != nil {
global.Log.Panicf("连接mysql失败! [%s]", err.Error())
return nil
} else {
sqlDB, _ := db.DB()
sqlDB.SetMaxIdleConns(m.MaxIdleConns)
sqlDB.SetMaxOpenConns(m.MaxOpenConns)
return db
}
}

View File

@@ -0,0 +1,34 @@
package starter
import (
"fmt"
"mayfly-go/pkg/config"
"mayfly-go/pkg/global"
"github.com/go-redis/redis"
)
func InitRedis() {
global.RedisCli = ConnRedis()
}
func ConnRedis() *redis.Client {
// 设置redis客户端
redisConf := config.Conf.Redis
if redisConf == nil {
global.Log.Panic("未找到redis配置信息")
return nil
}
global.Log.Infof("连接redis [%s:%d]", redisConf.Host, redisConf.Port)
rdb := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", redisConf.Host, redisConf.Port),
Password: redisConf.Password, // no password set
DB: redisConf.Db, // use default DB
})
// 测试连接
_, e := rdb.Ping().Result()
if e != nil {
global.Log.Panic(fmt.Sprintf("连接redis失败! [%s:%d]", redisConf.Host, redisConf.Port))
}
return rdb
}

View File

@@ -0,0 +1,34 @@
package starter
import (
"mayfly-go/initialize"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/config"
"mayfly-go/pkg/ctx"
"mayfly-go/pkg/global"
)
func RunWebServer() {
// 权限处理器
ctx.UseBeforeHandlerInterceptor(ctx.PermissionHandler)
// 日志处理器
ctx.UseAfterHandlerInterceptor(ctx.LogHandler)
// 注册路由
web := initialize.InitRouter()
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)
}
var err error
if server.Tls != nil && server.Tls.Enable {
err = web.RunTLS(port, server.Tls.CertFile, server.Tls.KeyFile)
} else {
err = web.Run(port)
}
biz.ErrIsNilAppendErr(err, "服务启动失败: %s")
}