From e321686d4babd2e029ffaa7d9d603a9d93c3402b Mon Sep 17 00:00:00 2001 From: liuhaotian Date: Tue, 14 Dec 2021 14:31:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BB=93=E6=9E=84=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- consts/config.go | 7 +++++++ consts/errors.go | 2 ++ handler/category.go | 13 +++++-------- helper/gorm_helper.go | 9 ++++++--- main.go | 40 ++++++++++++++++++++++------------------ 5 files changed, 42 insertions(+), 29 deletions(-) diff --git a/consts/config.go b/consts/config.go index 97a6534..fe7bfee 100644 --- a/consts/config.go +++ b/consts/config.go @@ -10,4 +10,11 @@ const ( Etcd = "etcd" EtcdEnd = "endpoints" EtcdEndDefault = "127.0.0.1:2379" + Port + PortDefault = "8080" +) + +//mysql 数据库实例名称 +const ( + TestDB = "test" ) diff --git a/consts/errors.go b/consts/errors.go index 2bd696d..d3d220e 100644 --- a/consts/errors.go +++ b/consts/errors.go @@ -3,4 +3,6 @@ package consts const ( EtcConnError = "etcConnectError" ErrorReason = "reason" + PanicError = "panicError" + GormError = "gormErr" ) diff --git a/handler/category.go b/handler/category.go index 52c6f91..a01c85f 100644 --- a/handler/category.go +++ b/handler/category.go @@ -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]) + } diff --git a/helper/gorm_helper.go b/helper/gorm_helper.go index 703c589..c67fba0 100644 --- a/helper/gorm_helper.go +++ b/helper/gorm_helper.go @@ -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 } diff --git a/main.go b/main.go index af1e4df..0722eab 100644 --- a/main.go +++ b/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) + }) +}