From bb606890554c77967e17985c9aa58084fd7646f3 Mon Sep 17 00:00:00 2001 From: liuhaotian Date: Thu, 13 Jan 2022 14:28:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=90=88=E5=B9=B6pkg=E5=90=8C=E7=B1=BB?= =?UTF-8?q?=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/brahma/api/murder/main.go | 24 +++--------------- app/zeus/api/lark/internal/user/user.go | 4 +-- app/zeus/api/lark/main.go | 7 +++--- pkg/{net => api}/api_response.go | 3 +-- pkg/{third_party => api}/fiber.go | 30 ++++++++++++++++++----- pkg/{validator => api}/param_validator.go | 10 +++----- 6 files changed, 37 insertions(+), 41 deletions(-) rename pkg/{net => api}/api_response.go (82%) rename pkg/{third_party => api}/fiber.go (61%) rename pkg/{validator => api}/param_validator.go (85%) diff --git a/app/brahma/api/murder/main.go b/app/brahma/api/murder/main.go index 41f4836..fa2af3e 100755 --- a/app/brahma/api/murder/main.go +++ b/app/brahma/api/murder/main.go @@ -1,20 +1,17 @@ package main import ( - "context" "fmt" "git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/config" "git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/error_process" "git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/gorm_helper" "git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/handler" + "git.icechen.cn/monorepo/backend/pkg/api" "github.com/gofiber/fiber/v2" - "github.com/gofiber/fiber/v2/middleware/logger" - "github.com/gofiber/fiber/v2/middleware/recover" "github.com/gofiber/fiber/v2/middleware/requestid" ctxLogger "github.com/luizsuper/ctxLoggers" "go.uber.org/zap" "os" - "runtime" ) const ( @@ -54,25 +51,10 @@ func ResourceInit() { func appInit() { //requestId app.Use(requestid.New()) - //logger - app.Use(logger.New(logger.Config{ - Format: "${respHeader:X-Request-ID} ${status} - ${method} ${path} ${resBody}\n", - })) //recover - app.Use(recover.New(recover.Config{ - EnableStackTrace: true, - StackTraceHandler: func(c *fiber.Ctx, e interface{}) { - buf := make([]byte, 1024*1024) - buf = buf[:runtime.Stack(buf, false)] - fmt.Printf("panic: %v\n%s\n", e, buf) - ctxLogger.FError(c, error_process.PanicError, zap.String("", fmt.Sprintf("panic: %v\n%s\n", e, buf))) - }, - })) + app.Use(api.FibberRecover) //requestId for gorm - app.Use(func(ctx *fiber.Ctx) error { - ctx.SetUserContext(context.WithValue(context.Background(), fiber.HeaderXRequestID, ctx.Response().Header.Peek(fiber.HeaderXRequestID))) - return ctx.Next() - }) + app.Use(api.FibberUserCtxWithTrace) } func routerInit() { diff --git a/app/zeus/api/lark/internal/user/user.go b/app/zeus/api/lark/internal/user/user.go index 954d153..943784b 100644 --- a/app/zeus/api/lark/internal/user/user.go +++ b/app/zeus/api/lark/internal/user/user.go @@ -1,9 +1,9 @@ package user import ( + "git.icechen.cn/monorepo/backend/pkg/api" "git.icechen.cn/monorepo/backend/pkg/proto/zeus/lark" "git.icechen.cn/monorepo/backend/pkg/rpc" - "git.icechen.cn/monorepo/backend/pkg/validator" "github.com/gofiber/fiber/v2" ctxLogger "github.com/luizsuper/ctxLoggers" "go.uber.org/zap" @@ -14,7 +14,7 @@ func LoginHandle(c *fiber.Ctx) error { Code string `json:"code" validate:"required"` Email string `json:"email" label:"邮箱" validate:"email"` }{} - err := validator.Bind(c, &p) + err := api.Bind(c, &p) if err != nil { return err } diff --git a/app/zeus/api/lark/main.go b/app/zeus/api/lark/main.go index 562703a..4cbb6d4 100755 --- a/app/zeus/api/lark/main.go +++ b/app/zeus/api/lark/main.go @@ -1,9 +1,8 @@ package main import ( - "git.icechen.cn/monorepo/backend/pkg/third_party" - "git.icechen.cn/monorepo/backend/app/zeus/api/lark/internal/user" + "git.icechen.cn/monorepo/backend/pkg/api" "github.com/gofiber/fiber/v2" ctxLogger "github.com/luizsuper/ctxLoggers" ) @@ -11,9 +10,9 @@ import ( func main() { router := fiber.New(fiber.Config{ AppName: "api-lark", - ErrorHandler: third_party.FiberErrHandler, + ErrorHandler: api.FiberErrHandler, }) - router.Use(third_party.FibberRespHandler) + router.Use(api.FibberRespHandler) userRouter := router.Group("/user") userRouter.Post("/login", user.LoginHandle) diff --git a/pkg/net/api_response.go b/pkg/api/api_response.go similarity index 82% rename from pkg/net/api_response.go rename to pkg/api/api_response.go index b53a7f2..93c95c0 100644 --- a/pkg/net/api_response.go +++ b/pkg/api/api_response.go @@ -1,5 +1,4 @@ -// Package net 网络有关的库 -package net +package api import "encoding/json" diff --git a/pkg/third_party/fiber.go b/pkg/api/fiber.go similarity index 61% rename from pkg/third_party/fiber.go rename to pkg/api/fiber.go index 8bc78da..fa2e25e 100644 --- a/pkg/third_party/fiber.go +++ b/pkg/api/fiber.go @@ -1,8 +1,8 @@ -// Package third_party 关于所有第三方框架的二次封装 -package third_party +// Package api 关于所有第三方框架的二次封装 +package api import ( - "git.icechen.cn/monorepo/backend/pkg/net" + "context" "github.com/gofiber/fiber/v2" ) @@ -19,7 +19,7 @@ func FiberErrHandler(ctx *fiber.Ctx, err error) error { msg = err.Error() } - return ctx.Status(code).JSON(net.Response{ + return ctx.Status(code).JSON(Response{ Code: code, Message: msg, Data: nil, @@ -29,7 +29,7 @@ func FiberErrHandler(ctx *fiber.Ctx, err error) error { // FibberRespHandler fiber resp的统一处理 func FibberRespHandler(c *fiber.Ctx) error { if respContentType := string(c.Response().Header.ContentType()); respContentType == fiber.MIMEApplicationJSON { - resp := net.Response{ + resp := Response{ Code: 0, Message: "", Data: c.Response().Body(), @@ -40,7 +40,25 @@ func FibberRespHandler(c *fiber.Ctx) error { return c.Next() } -//5 funcs for err 2 fiberErr +// FibberRecover fiberPanicRecover +func FibberRecover(c *fiber.Ctx) error { + if respContentType := string(c.Response().Header.ContentType()); respContentType == fiber.MIMEApplicationJSON { + resp := Response{ + Code: 0, + Message: "", + Data: c.Response().Body(), + } + return c.JSON(resp) + } + + return c.Next() +} + +// FibberUserCtxWithTrace 把fibberCtx的traceId 置入UserCtx中 +func FibberUserCtxWithTrace(c *fiber.Ctx) error { + c.SetUserContext(context.WithValue(context.Background(), fiber.HeaderXRequestID, c.Response().Header.Peek(fiber.HeaderXRequestID))) + return c.Next() +} func NewFError(msg string) error { return fiber.NewError(fiber.StatusInternalServerError, msg) diff --git a/pkg/validator/param_validator.go b/pkg/api/param_validator.go similarity index 85% rename from pkg/validator/param_validator.go rename to pkg/api/param_validator.go index 83dfa7e..7b3c35b 100644 --- a/pkg/validator/param_validator.go +++ b/pkg/api/param_validator.go @@ -1,9 +1,7 @@ -// Package validator 校验器集合包 -package validator +package api import ( "fmt" - "git.icechen.cn/monorepo/backend/pkg/third_party" "github.com/go-playground/locales/zh_Hans_CN" ut "github.com/go-playground/universal-translator" "github.com/go-playground/validator/v10" @@ -45,11 +43,11 @@ func init() { func Bind(c *fiber.Ctx, out interface{}) error { if reflect.TypeOf(out).Kind() != reflect.Ptr { - return third_party.NewFError("bind must ptr") + return NewFError("bind must ptr") } if err := c.BodyParser(out); err != nil { - return third_party.WarpFError(err) + return WarpFError(err) } err := Validate.Struct(out) @@ -62,7 +60,7 @@ func Bind(c *fiber.Ctx, out interface{}) error { } err = fmt.Errorf("%s", strings.Join(errString, ",")) } - return third_party.WarpFErrorWithCode(fiber.StatusBadRequest, err) + return WarpFErrorWithCode(fiber.StatusBadRequest, err) } return nil }