feat: 合并pkg同类项
continuous-integration/drone/push Build is passing Details

master
liuhaotian 2022-01-13 14:28:50 +08:00
parent 80bd452cca
commit bb60689055
6 changed files with 37 additions and 41 deletions

View File

@ -1,20 +1,17 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/config" "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/error_process"
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/gorm_helper" "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/app/brahma/api/murder/internal/handler"
"git.icechen.cn/monorepo/backend/pkg/api"
"github.com/gofiber/fiber/v2" "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" "github.com/gofiber/fiber/v2/middleware/requestid"
ctxLogger "github.com/luizsuper/ctxLoggers" ctxLogger "github.com/luizsuper/ctxLoggers"
"go.uber.org/zap" "go.uber.org/zap"
"os" "os"
"runtime"
) )
const ( const (
@ -54,25 +51,10 @@ func ResourceInit() {
func appInit() { func appInit() {
//requestId //requestId
app.Use(requestid.New()) app.Use(requestid.New())
//logger
app.Use(logger.New(logger.Config{
Format: "${respHeader:X-Request-ID} ${status} - ${method} ${path} ${resBody}\n",
}))
//recover //recover
app.Use(recover.New(recover.Config{ app.Use(api.FibberRecover)
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)))
},
}))
//requestId for gorm //requestId for gorm
app.Use(func(ctx *fiber.Ctx) error { app.Use(api.FibberUserCtxWithTrace)
ctx.SetUserContext(context.WithValue(context.Background(), fiber.HeaderXRequestID, ctx.Response().Header.Peek(fiber.HeaderXRequestID)))
return ctx.Next()
})
} }
func routerInit() { func routerInit() {

View File

@ -1,9 +1,9 @@
package user package user
import ( import (
"git.icechen.cn/monorepo/backend/pkg/api"
"git.icechen.cn/monorepo/backend/pkg/proto/zeus/lark" "git.icechen.cn/monorepo/backend/pkg/proto/zeus/lark"
"git.icechen.cn/monorepo/backend/pkg/rpc" "git.icechen.cn/monorepo/backend/pkg/rpc"
"git.icechen.cn/monorepo/backend/pkg/validator"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
ctxLogger "github.com/luizsuper/ctxLoggers" ctxLogger "github.com/luizsuper/ctxLoggers"
"go.uber.org/zap" "go.uber.org/zap"
@ -14,7 +14,7 @@ func LoginHandle(c *fiber.Ctx) error {
Code string `json:"code" validate:"required"` Code string `json:"code" validate:"required"`
Email string `json:"email" label:"邮箱" validate:"email"` Email string `json:"email" label:"邮箱" validate:"email"`
}{} }{}
err := validator.Bind(c, &p) err := api.Bind(c, &p)
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,9 +1,8 @@
package main package main
import ( 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/app/zeus/api/lark/internal/user"
"git.icechen.cn/monorepo/backend/pkg/api"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
ctxLogger "github.com/luizsuper/ctxLoggers" ctxLogger "github.com/luizsuper/ctxLoggers"
) )
@ -11,9 +10,9 @@ import (
func main() { func main() {
router := fiber.New(fiber.Config{ router := fiber.New(fiber.Config{
AppName: "api-lark", AppName: "api-lark",
ErrorHandler: third_party.FiberErrHandler, ErrorHandler: api.FiberErrHandler,
}) })
router.Use(third_party.FibberRespHandler) router.Use(api.FibberRespHandler)
userRouter := router.Group("/user") userRouter := router.Group("/user")
userRouter.Post("/login", user.LoginHandle) userRouter.Post("/login", user.LoginHandle)

View File

@ -1,5 +1,4 @@
// Package net 网络有关的库 package api
package net
import "encoding/json" import "encoding/json"

View File

@ -1,8 +1,8 @@
// Package third_party 关于所有第三方框架的二次封装 // Package api 关于所有第三方框架的二次封装
package third_party package api
import ( import (
"git.icechen.cn/monorepo/backend/pkg/net" "context"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
@ -19,7 +19,7 @@ func FiberErrHandler(ctx *fiber.Ctx, err error) error {
msg = err.Error() msg = err.Error()
} }
return ctx.Status(code).JSON(net.Response{ return ctx.Status(code).JSON(Response{
Code: code, Code: code,
Message: msg, Message: msg,
Data: nil, Data: nil,
@ -29,7 +29,7 @@ func FiberErrHandler(ctx *fiber.Ctx, err error) error {
// FibberRespHandler fiber resp的统一处理 // FibberRespHandler fiber resp的统一处理
func FibberRespHandler(c *fiber.Ctx) error { func FibberRespHandler(c *fiber.Ctx) error {
if respContentType := string(c.Response().Header.ContentType()); respContentType == fiber.MIMEApplicationJSON { if respContentType := string(c.Response().Header.ContentType()); respContentType == fiber.MIMEApplicationJSON {
resp := net.Response{ resp := Response{
Code: 0, Code: 0,
Message: "", Message: "",
Data: c.Response().Body(), Data: c.Response().Body(),
@ -40,7 +40,25 @@ func FibberRespHandler(c *fiber.Ctx) error {
return c.Next() 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 { func NewFError(msg string) error {
return fiber.NewError(fiber.StatusInternalServerError, msg) return fiber.NewError(fiber.StatusInternalServerError, msg)

View File

@ -1,9 +1,7 @@
// Package validator 校验器集合包 package api
package validator
import ( import (
"fmt" "fmt"
"git.icechen.cn/monorepo/backend/pkg/third_party"
"github.com/go-playground/locales/zh_Hans_CN" "github.com/go-playground/locales/zh_Hans_CN"
ut "github.com/go-playground/universal-translator" ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
@ -45,11 +43,11 @@ func init() {
func Bind(c *fiber.Ctx, out interface{}) error { func Bind(c *fiber.Ctx, out interface{}) error {
if reflect.TypeOf(out).Kind() != reflect.Ptr { 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 { if err := c.BodyParser(out); err != nil {
return third_party.WarpFError(err) return WarpFError(err)
} }
err := Validate.Struct(out) err := Validate.Struct(out)
@ -62,7 +60,7 @@ func Bind(c *fiber.Ctx, out interface{}) error {
} }
err = fmt.Errorf("%s", strings.Join(errString, ",")) err = fmt.Errorf("%s", strings.Join(errString, ","))
} }
return third_party.WarpFErrorWithCode(fiber.StatusBadRequest, err) return WarpFErrorWithCode(fiber.StatusBadRequest, err)
} }
return nil return nil
} }