generated from pkg/go-template
feat: 合并pkg同类项
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
80bd452cca
commit
bb60689055
|
@ -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() {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
// Package net 网络有关的库
|
||||
package net
|
||||
package api
|
||||
|
||||
import "encoding/json"
|
||||
|
|
@ -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)
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue