feat: 结构整理
parent
28fdd5a348
commit
e321686d4b
|
@ -10,4 +10,11 @@ const (
|
|||
Etcd = "etcd"
|
||||
EtcdEnd = "endpoints"
|
||||
EtcdEndDefault = "127.0.0.1:2379"
|
||||
Port
|
||||
PortDefault = "8080"
|
||||
)
|
||||
|
||||
//mysql 数据库实例名称
|
||||
const (
|
||||
TestDB = "test"
|
||||
)
|
||||
|
|
|
@ -3,4 +3,6 @@ package consts
|
|||
const (
|
||||
EtcConnError = "etcConnectError"
|
||||
ErrorReason = "reason"
|
||||
PanicError = "panicError"
|
||||
GormError = "gormErr"
|
||||
)
|
||||
|
|
|
@ -1,23 +1,20 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"bgm/consts"
|
||||
bgm "bgm/helper"
|
||||
"bgm/model"
|
||||
"bgm/service"
|
||||
"errors"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/rs/xid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func CreateCategoryH(ctx *fiber.Ctx) error {
|
||||
i := ctx.UserContext().Value("local")
|
||||
func CreateCategoryH(ctx *fiber.Ctx, mysqlMap bgm.MysqlMap) error {
|
||||
category := &model.Category{
|
||||
Key: 1,
|
||||
Value: "默认",
|
||||
Gid: xid.New().String(),
|
||||
}
|
||||
if k, ok := i.(*gorm.DB); ok {
|
||||
return service.CreateCategory(category, k)
|
||||
}
|
||||
return errors.New("")
|
||||
return service.CreateCategory(category, mysqlMap[consts.TestDB])
|
||||
|
||||
}
|
||||
|
|
|
@ -1,13 +1,16 @@
|
|||
package bgm
|
||||
|
||||
import (
|
||||
"bgm/consts"
|
||||
"fmt"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func GormMap(dsnPre string) (map[string]*gorm.DB, error) {
|
||||
dsn := fmt.Sprintf("%v/test?charset=utf8mb4&parseTime=True&loc=Local", dsnPre)
|
||||
type MysqlMap map[string]*gorm.DB
|
||||
|
||||
func GormMap(dsnPre string) (MysqlMap, error) {
|
||||
dsn := fmt.Sprintf("%v/%v?charset=utf8mb4&parseTime=True&loc=Local", dsnPre, consts.TestDB)
|
||||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
||||
return map[string]*gorm.DB{"local": db}, err
|
||||
return map[string]*gorm.DB{consts.TestDB: db}, err
|
||||
}
|
||||
|
|
40
main.go
40
main.go
|
@ -5,7 +5,6 @@ import (
|
|||
"bgm/handler"
|
||||
"bgm/helper"
|
||||
"bgm/model"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/logger"
|
||||
|
@ -13,7 +12,6 @@ import (
|
|||
"github.com/gofiber/fiber/v2/middleware/requestid"
|
||||
ctxLogger "github.com/luizsuper/ctxLoggers"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
"os"
|
||||
"runtime"
|
||||
)
|
||||
|
@ -22,10 +20,12 @@ const (
|
|||
createCategory = "/sts"
|
||||
)
|
||||
|
||||
type handleM map[string]func(ctx *fiber.Ctx, mysqlMap bgm.MysqlMap) error
|
||||
|
||||
var (
|
||||
gormMap = map[string]*gorm.DB{}
|
||||
mysqlMap = make(bgm.MysqlMap, 0)
|
||||
err error
|
||||
handlerMap = make(map[string]func(ctx *fiber.Ctx) error, 0)
|
||||
handlerMap = make(handleM, 0)
|
||||
app = fiber.New(fiber.Config{
|
||||
ErrorHandler: func(ctx *fiber.Ctx, err error) error {
|
||||
return ctx.Status(fiber.StatusInternalServerError).JSON(model.Response{Message: "服务器内部错误"})
|
||||
|
@ -49,29 +49,33 @@ func init() {
|
|||
StackTraceHandler: func(c *fiber.Ctx, e interface{}) {
|
||||
buf := make([]byte, 1024*1024)
|
||||
buf = buf[:runtime.Stack(buf, false)]
|
||||
ctxLogger.FError(c, "panic err", zap.String("stack", fmt.Sprintf("panic: %v\n%s\n", e, buf)))
|
||||
ctxLogger.FError(c, consts.PanicError, zap.String(consts.ErrorReason, fmt.Sprintf("panic: %v\n%s\n", e, buf)))
|
||||
},
|
||||
}))
|
||||
gormMap, err = bgm.GormMap(bgm.ConfigMap()[bgm.GetConfigKey(consts.DsnPre)])
|
||||
mysqlMap, err = bgm.GormMap(bgm.ConfigMap()[bgm.GetConfigKey(consts.DsnPre)])
|
||||
if err != nil {
|
||||
ctxLogger.FError(nil, "gorm err", zap.String("gorm", err.Error()))
|
||||
ctxLogger.FError(nil, consts.GormError, zap.String(consts.ErrorReason, err.Error()))
|
||||
os.Exit(-1)
|
||||
}
|
||||
app.Use(func(ctx *fiber.Ctx) error {
|
||||
background := context.Background()
|
||||
ctx.SetUserContext(context.WithValue(background, "local", gormMap["local"]))
|
||||
return ctx.Next()
|
||||
})
|
||||
routerInit()
|
||||
}
|
||||
|
||||
func main() {
|
||||
app.Get("/*", func(ctx *fiber.Ctx) error {
|
||||
defer ctx.UserContext().Done()
|
||||
return handlerMap[ctx.Path()](ctx)
|
||||
})
|
||||
err := app.Listen(":8080")
|
||||
func routerInit() {
|
||||
handlerMap[createCategory] = handler.CreateCategoryH
|
||||
}
|
||||
|
||||
func serverStart() {
|
||||
port := bgm.GetEnvDefault(consts.Port, consts.PortDefault)
|
||||
err = app.Listen(fmt.Sprintf(":%v", port))
|
||||
if err != nil {
|
||||
ctxLogger.Error(nil, "", zap.String("err", err.Error()))
|
||||
os.Exit(-1)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
serverStart()
|
||||
app.Get("/*", func(ctx *fiber.Ctx) error {
|
||||
return handlerMap[ctx.Path()](ctx, mysqlMap)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue