mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-14 13:20:25 +08:00
代码优化
This commit is contained in:
@@ -23,13 +23,12 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type LogInfo struct {
|
type LogInfo struct {
|
||||||
NeedLog bool // 是否需要记录日志
|
|
||||||
LogResp bool // 是否记录返回结果
|
LogResp bool // 是否记录返回结果
|
||||||
Description string // 请求描述
|
Description string // 请求描述
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLogInfo(description string) *LogInfo {
|
func NewLogInfo(description string) *LogInfo {
|
||||||
return &LogInfo{NeedLog: true, Description: description, LogResp: false}
|
return &LogInfo{Description: description, LogResp: false}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *LogInfo) WithLogResp(logResp bool) *LogInfo {
|
func (i *LogInfo) WithLogResp(logResp bool) *LogInfo {
|
||||||
@@ -39,7 +38,7 @@ func (i *LogInfo) WithLogResp(logResp bool) *LogInfo {
|
|||||||
|
|
||||||
func (l *LogInfo) AfterHandle(rc *ReqCtx) {
|
func (l *LogInfo) AfterHandle(rc *ReqCtx) {
|
||||||
li := rc.LogInfo
|
li := rc.LogInfo
|
||||||
if li == nil || !li.NeedLog {
|
if li == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,7 +59,7 @@ func (l *LogInfo) AfterHandle(rc *ReqCtx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getLogMsg(rc *ReqCtx) string {
|
func getLogMsg(rc *ReqCtx) string {
|
||||||
msg := rc.LogInfo.Description
|
msg := rc.LogInfo.Description + fmt.Sprintf(" ->%dms", rc.timed)
|
||||||
if !utils.IsBlank(reflect.ValueOf(rc.ReqParam)) {
|
if !utils.IsBlank(reflect.ValueOf(rc.ReqParam)) {
|
||||||
rb, _ := json.Marshal(rc.ReqParam)
|
rb, _ := json.Marshal(rc.ReqParam)
|
||||||
msg = msg + fmt.Sprintf("\n--> %s", string(rb))
|
msg = msg + fmt.Sprintf("\n--> %s", string(rb))
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
package ctx
|
|
||||||
|
|
||||||
type AppContext struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
type LoginAccount struct {
|
|
||||||
Id uint64
|
|
||||||
Username string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Permission struct {
|
|
||||||
CheckToken bool // 是否检查token
|
|
||||||
Code string // 权限码
|
|
||||||
Name string // 描述
|
|
||||||
}
|
|
||||||
@@ -4,16 +4,11 @@ import (
|
|||||||
"mayfly-go/base/ginx"
|
"mayfly-go/base/ginx"
|
||||||
"mayfly-go/base/model"
|
"mayfly-go/base/model"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
// // 获取数据函数
|
|
||||||
// type getDataFunc func(*ReqCtx) interface{}
|
|
||||||
|
|
||||||
// // 操作函数,无返回数据
|
|
||||||
// type operationFunc func(*ReqCtx)
|
|
||||||
|
|
||||||
// 处理函数
|
// 处理函数
|
||||||
type HandlerFunc func(*ReqCtx)
|
type HandlerFunc func(*ReqCtx)
|
||||||
|
|
||||||
@@ -28,6 +23,7 @@ type ReqCtx struct {
|
|||||||
ReqParam interface{} // 请求参数,主要用于记录日志
|
ReqParam interface{} // 请求参数,主要用于记录日志
|
||||||
ResData interface{} // 响应结果
|
ResData interface{} // 响应结果
|
||||||
err interface{} // 请求错误
|
err interface{} // 请求错误
|
||||||
|
timed int64 // 执行时间
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rc *ReqCtx) Handle(handler HandlerFunc) {
|
func (rc *ReqCtx) Handle(handler HandlerFunc) {
|
||||||
@@ -56,7 +52,9 @@ func (rc *ReqCtx) Handle(handler HandlerFunc) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
begin := time.Now()
|
||||||
handler(rc)
|
handler(rc)
|
||||||
|
rc.timed = time.Now().Sub(begin).Milliseconds()
|
||||||
ginx.SuccessRes(ginCtx, rc.ResData)
|
ginx.SuccessRes(ginCtx, rc.ResData)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
package initialize
|
|
||||||
|
|
||||||
import (
|
|
||||||
"mayfly-go/base/global"
|
|
||||||
|
|
||||||
"gorm.io/driver/mysql"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
"gorm.io/gorm/logger"
|
|
||||||
"gorm.io/gorm/schema"
|
|
||||||
)
|
|
||||||
|
|
||||||
func GormMysql() *gorm.DB {
|
|
||||||
m := global.Config.Mysql
|
|
||||||
if m.Dbname == "" {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
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.Panic("mysql连接失败")
|
|
||||||
return nil
|
|
||||||
} else {
|
|
||||||
sqlDB, _ := db.DB()
|
|
||||||
sqlDB.SetMaxIdleConns(m.MaxIdleConns)
|
|
||||||
sqlDB.SetMaxOpenConns(m.MaxOpenConns)
|
|
||||||
return db
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -44,7 +44,7 @@ func (m *Model) SetBaseInfo(account *LoginAccount) {
|
|||||||
//
|
//
|
||||||
// 若error不为nil则为不存在该记录
|
// 若error不为nil则为不存在该记录
|
||||||
func GetById(model interface{}, id uint64, cols ...string) error {
|
func GetById(model interface{}, id uint64, cols ...string) error {
|
||||||
return global.Db.Debug().Select(cols).Where("id = ?", id).First(model).Error
|
return global.Db.Select(cols).Where("id = ?", id).First(model).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据id更新model,更新字段为model中不为空的值,即int类型不为0,ptr类型不为nil这类字段值
|
// 根据id更新model,更新字段为model中不为空的值,即int类型不为0,ptr类型不为nil这类字段值
|
||||||
@@ -67,27 +67,27 @@ func Insert(model interface{}) error {
|
|||||||
//
|
//
|
||||||
// @param list为数组类型 如 var users []*User,可指定为非model结构体,即只包含需要返回的字段结构体
|
// @param list为数组类型 如 var users []*User,可指定为非model结构体,即只包含需要返回的字段结构体
|
||||||
func ListBy(model interface{}, list interface{}, cols ...string) {
|
func ListBy(model interface{}, list interface{}, cols ...string) {
|
||||||
global.Db.Debug().Model(model).Select(cols).Where(model).Find(list)
|
global.Db.Model(model).Select(cols).Where(model).Find(list)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取满足model中不为空的字段值条件的单个对象。model需为指针类型(需要将查询出来的值赋值给model)
|
// 获取满足model中不为空的字段值条件的单个对象。model需为指针类型(需要将查询出来的值赋值给model)
|
||||||
//
|
//
|
||||||
// 若 error不为nil,则为不存在该记录
|
// 若 error不为nil,则为不存在该记录
|
||||||
func GetBy(model interface{}, cols ...string) error {
|
func GetBy(model interface{}, cols ...string) error {
|
||||||
return global.Db.Debug().Select(cols).Where(model).First(model).Error
|
return global.Db.Select(cols).Where(model).First(model).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取满足conditionModel中不为空的字段值条件的单个对象。model需为指针类型(需要将查询出来的值赋值给model)
|
// 获取满足conditionModel中不为空的字段值条件的单个对象。model需为指针类型(需要将查询出来的值赋值给model)
|
||||||
// @param toModel 需要查询的字段
|
// @param toModel 需要查询的字段
|
||||||
// 若 error不为nil,则为不存在该记录
|
// 若 error不为nil,则为不存在该记录
|
||||||
func GetByConditionTo(conditionModel interface{}, toModel interface{}) error {
|
func GetByConditionTo(conditionModel interface{}, toModel interface{}) error {
|
||||||
return global.Db.Debug().Model(conditionModel).Where(conditionModel).First(toModel).Error
|
return global.Db.Model(conditionModel).Where(conditionModel).First(toModel).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取分页结果
|
// 获取分页结果
|
||||||
func GetPage(pageParam *PageParam, conditionModel interface{}, toModels interface{}, orderBy ...string) PageResult {
|
func GetPage(pageParam *PageParam, conditionModel interface{}, toModels interface{}, orderBy ...string) PageResult {
|
||||||
var count int64
|
var count int64
|
||||||
global.Db.Debug().Model(conditionModel).Where(conditionModel).Count(&count)
|
global.Db.Model(conditionModel).Where(conditionModel).Count(&count)
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
return PageResult{Total: 0, List: []string{}}
|
return PageResult{Total: 0, List: []string{}}
|
||||||
}
|
}
|
||||||
@@ -99,7 +99,7 @@ func GetPage(pageParam *PageParam, conditionModel interface{}, toModels interfac
|
|||||||
} else {
|
} else {
|
||||||
orderByStr = strings.Join(orderBy, ",")
|
orderByStr = strings.Join(orderBy, ",")
|
||||||
}
|
}
|
||||||
global.Db.Debug().Model(conditionModel).Where(conditionModel).Order(orderByStr).Limit(pageSize).Offset((page - 1) * pageSize).Find(toModels)
|
global.Db.Model(conditionModel).Where(conditionModel).Order(orderByStr).Limit(pageSize).Offset((page - 1) * pageSize).Find(toModels)
|
||||||
return PageResult{Total: count, List: toModels}
|
return PageResult{Total: count, List: toModels}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,92 +127,3 @@ func GetListBySql(sql string, params ...interface{}) []map[string]interface{} {
|
|||||||
global.Db.Raw(sql, params).Scan(&maps)
|
global.Db.Raw(sql, params).Scan(&maps)
|
||||||
return maps
|
return maps
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取所有列表数据
|
|
||||||
// model为数组类型 如 var users []*User
|
|
||||||
// func GetList(seter orm.QuerySeter, model interface{}, toModel interface{}) {
|
|
||||||
// _, _ = seter.All(model, getFieldNames(toModel)...)
|
|
||||||
// err := utils.Copy(toModel, model)
|
|
||||||
// biz.BizErrIsNil(err, "实体转换错误")
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func getOrm() orm.Ormer {
|
|
||||||
// return orm.NewOrm()
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // 结果模型缓存
|
|
||||||
// var resultModelCache = make(map[string][]string)
|
|
||||||
|
|
||||||
// // 获取实体对象的字段名
|
|
||||||
// func getFieldNames(obj interface{}) []string {
|
|
||||||
// objType := indirectType(reflect.TypeOf(obj))
|
|
||||||
// cacheKey := objType.PkgPath() + "." + objType.Name()
|
|
||||||
// cache := resultModelCache[cacheKey]
|
|
||||||
// if cache != nil {
|
|
||||||
// return cache
|
|
||||||
// }
|
|
||||||
// cache = getFieldNamesByType("", reflect.TypeOf(obj))
|
|
||||||
// resultModelCache[cacheKey] = cache
|
|
||||||
// return cache
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func indirectType(reflectType reflect.Type) reflect.Type {
|
|
||||||
// for reflectType.Kind() == reflect.Ptr || reflectType.Kind() == reflect.Slice {
|
|
||||||
// reflectType = reflectType.Elem()
|
|
||||||
// }
|
|
||||||
// return reflectType
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func getFieldNamesByType(namePrefix string, reflectType reflect.Type) []string {
|
|
||||||
// var fieldNames []string
|
|
||||||
|
|
||||||
// if reflectType = indirectType(reflectType); reflectType.Kind() == reflect.Struct {
|
|
||||||
// for i := 0; i < reflectType.NumField(); i++ {
|
|
||||||
// t := reflectType.Field(i)
|
|
||||||
// tName := t.Name
|
|
||||||
// // 判断结构体字段是否为结构体,是的话则跳过
|
|
||||||
// it := indirectType(t.Type)
|
|
||||||
// if it.Kind() == reflect.Struct {
|
|
||||||
// itName := it.Name()
|
|
||||||
// // 如果包含Time或time则表示为time类型,无需递归该结构体字段
|
|
||||||
// if !strings.Contains(itName, "BaseModel") && !strings.Contains(itName, "Time") &&
|
|
||||||
// !strings.Contains(itName, "time") {
|
|
||||||
// fieldNames = append(fieldNames, getFieldNamesByType(tName+"__", it)...)
|
|
||||||
// continue
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if t.Anonymous {
|
|
||||||
// fieldNames = append(fieldNames, getFieldNamesByType("", t.Type)...)
|
|
||||||
// } else {
|
|
||||||
// fieldNames = append(fieldNames, namePrefix+tName)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return fieldNames
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func ormParams2Struct(maps []orm.Params, structs interface{}) error {
|
|
||||||
// structsV := reflect.Indirect(reflect.ValueOf(structs))
|
|
||||||
// valType := structsV.Type()
|
|
||||||
// valElemType := valType.Elem()
|
|
||||||
// sliceType := reflect.SliceOf(valElemType)
|
|
||||||
|
|
||||||
// length := len(maps)
|
|
||||||
|
|
||||||
// valSlice := structsV
|
|
||||||
// if valSlice.IsNil() {
|
|
||||||
// // Make a new slice to hold our result, same size as the original data.
|
|
||||||
// valSlice = reflect.MakeSlice(sliceType, length, length)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// for i := 0; i < length; i++ {
|
|
||||||
// err := utils.Map2Struct(maps[i], valSlice.Index(i).Addr().Interface())
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// structsV.Set(valSlice)
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package initialize
|
package starter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"mayfly-go/base/global"
|
"mayfly-go/base/global"
|
||||||
@@ -11,10 +11,11 @@ import (
|
|||||||
|
|
||||||
func GormMysql() *gorm.DB {
|
func GormMysql() *gorm.DB {
|
||||||
m := global.Config.Mysql
|
m := global.Config.Mysql
|
||||||
if m.Dbname == "" {
|
if m == nil || m.Dbname == "" {
|
||||||
|
global.Log.Panic("未找到数据库配置信息")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
global.Log.Infof("连接mysql [%s]", m.Host)
|
||||||
mysqlConfig := mysql.Config{
|
mysqlConfig := mysql.Config{
|
||||||
DSN: m.Dsn(), // DSN data source name
|
DSN: m.Dsn(), // DSN data source name
|
||||||
DefaultStringSize: 191, // string 类型字段的默认长度
|
DefaultStringSize: 191, // string 类型字段的默认长度
|
||||||
@@ -28,9 +29,7 @@ func GormMysql() *gorm.DB {
|
|||||||
SingularTable: true,
|
SingularTable: true,
|
||||||
}, Logger: logger.Default.LogMode(logger.Silent)}
|
}, Logger: logger.Default.LogMode(logger.Silent)}
|
||||||
if db, err := gorm.Open(mysql.New(mysqlConfig), ormConfig); err != nil {
|
if db, err := gorm.Open(mysql.New(mysqlConfig), ormConfig); err != nil {
|
||||||
//global.GVA_LOG.Error("MySQL启动异常", zap.Any("err", err))
|
global.Log.Panic("连接mysql失败! [%s]")
|
||||||
//os.Exit(0)
|
|
||||||
//return nil
|
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
sqlDB, _ := db.DB()
|
sqlDB, _ := db.DB()
|
||||||
28
base/starter/redis.go
Normal file
28
base/starter/redis.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package starter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"mayfly-go/base/global"
|
||||||
|
|
||||||
|
"github.com/go-redis/redis"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ConnRedis() *redis.Client {
|
||||||
|
// 设置redis客户端
|
||||||
|
redisConf := global.Config.Redis
|
||||||
|
if redisConf == nil {
|
||||||
|
global.Log.Panic("未找到redis配置信息")
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
@@ -2,11 +2,11 @@ package starter
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"mayfly-go/base/global"
|
"mayfly-go/base/global"
|
||||||
"mayfly-go/devops/initialize"
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RunServer() {
|
func RunWebServer(web *gin.Engine) {
|
||||||
web := initialize.InitRouter()
|
|
||||||
port := global.Config.Server.GetPort()
|
port := global.Config.Server.GetPort()
|
||||||
if app := global.Config.App; app != nil {
|
if app := global.Config.App; app != nil {
|
||||||
global.Log.Infof("%s- Listening and serving HTTP on %s", app.GetAppInfo(), port)
|
global.Log.Infof("%s- Listening and serving HTTP on %s", app.GetAppInfo(), port)
|
||||||
Binary file not shown.
@@ -1,6 +1,7 @@
|
|||||||
app:
|
app:
|
||||||
name: devops
|
name: devops
|
||||||
version: 1.0.0
|
version: 1.0.0
|
||||||
|
|
||||||
server:
|
server:
|
||||||
# debug release test
|
# debug release test
|
||||||
model: release
|
model: release
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"mayfly-go/devops/db"
|
"mayfly-go/devops/db"
|
||||||
"mayfly-go/devops/models"
|
"mayfly-go/devops/models"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@@ -18,15 +19,19 @@ import (
|
|||||||
// @router /api/dbs [get]
|
// @router /api/dbs [get]
|
||||||
func Dbs(rc *ctx.ReqCtx) {
|
func Dbs(rc *ctx.ReqCtx) {
|
||||||
m := new([]models.Db)
|
m := new([]models.Db)
|
||||||
// querySetter := model.QuerySetter(new(models.Db))
|
|
||||||
rc.ResData = model.GetPage(ginx.GetPageParam(rc.GinCtx), m, new([]vo.SelectDataDbVO))
|
rc.ResData = model.GetPage(ginx.GetPageParam(rc.GinCtx), m, new([]vo.SelectDataDbVO))
|
||||||
}
|
}
|
||||||
|
|
||||||
// @router /api/db/:dbId/select [get]
|
// @router /api/db/:dbId/select [get]
|
||||||
func SelectData(rc *ctx.ReqCtx) {
|
func SelectData(rc *ctx.ReqCtx) {
|
||||||
g := rc.GinCtx
|
g := rc.GinCtx
|
||||||
selectSql := g.Query("selectSql")
|
// 去除前后空格及换行符
|
||||||
|
selectSql := strings.TrimFunc(g.Query("selectSql"), func(r rune) bool {
|
||||||
|
s := string(r)
|
||||||
|
return s == " " || s == "\n"
|
||||||
|
})
|
||||||
rc.ReqParam = selectSql
|
rc.ReqParam = selectSql
|
||||||
|
|
||||||
biz.NotEmpty(selectSql, "selectSql不能为空")
|
biz.NotEmpty(selectSql, "selectSql不能为空")
|
||||||
res, err := db.GetDbInstance(GetDbId(g)).SelectData(selectSql)
|
res, err := db.GetDbInstance(GetDbId(g)).SelectData(selectSql)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -2,20 +2,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"mayfly-go/base/global"
|
"mayfly-go/base/global"
|
||||||
"mayfly-go/base/initialize"
|
"mayfly-go/base/starter"
|
||||||
_ "mayfly-go/devops/routers"
|
"mayfly-go/devops/initialize"
|
||||||
"mayfly-go/mock-server/starter"
|
|
||||||
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
db := initialize.GormMysql()
|
global.Db = starter.GormMysql()
|
||||||
if db == nil {
|
starter.RunWebServer(initialize.InitRouter())
|
||||||
global.Log.Panic("mysql连接失败")
|
|
||||||
} else {
|
|
||||||
global.Db = db
|
|
||||||
}
|
|
||||||
|
|
||||||
starter.RunServer()
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,38 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"mayfly-go/base/global"
|
|
||||||
"mayfly-go/base/rediscli"
|
"mayfly-go/base/rediscli"
|
||||||
|
"mayfly-go/base/starter"
|
||||||
"mayfly-go/mock-server/initialize"
|
"mayfly-go/mock-server/initialize"
|
||||||
_ "mayfly-go/mock-server/routers"
|
|
||||||
"mayfly-go/mock-server/starter"
|
|
||||||
|
|
||||||
"github.com/go-redis/redis"
|
|
||||||
// _ "github.com/go-sql-driver/mysql"
|
// _ "github.com/go-sql-driver/mysql"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// 设置redis客户端
|
rediscli.SetCli(starter.ConnRedis())
|
||||||
redisConf := global.Config.Redis
|
starter.RunWebServer(initialize.InitRouter())
|
||||||
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))
|
|
||||||
}
|
|
||||||
rediscli.SetCli(rdb)
|
|
||||||
|
|
||||||
db := initialize.GormMysql()
|
|
||||||
if db == nil {
|
|
||||||
global.Log.Panic("mysql连接失败")
|
|
||||||
} else {
|
|
||||||
global.Db = db
|
|
||||||
}
|
|
||||||
|
|
||||||
starter.RunServer()
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
package starter
|
|
||||||
|
|
||||||
import (
|
|
||||||
"mayfly-go/base/global"
|
|
||||||
"mayfly-go/mock-server/initialize"
|
|
||||||
)
|
|
||||||
|
|
||||||
func RunServer() {
|
|
||||||
web := initialize.InitRouter()
|
|
||||||
port := global.Config.Server.GetPort()
|
|
||||||
if app := global.Config.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)
|
|
||||||
}
|
|
||||||
web.Run(port)
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user