package main import ( "encoding/json" "git.icechen.cn/monorepo/backend/app/zeus/api/lark/internal/user" "github.com/gofiber/fiber/v2" ctxLogger "github.com/luizsuper/ctxLoggers" ) func main() { router := fiber.New(fiber.Config{ AppName: "api-lark", ErrorHandler: middlewareError, }) // router.Use(middlewareResp) userRouter := router.Group("/user") userRouter.Post("/login", user.LoginHandle) err := router.Listen(":8080") if err != nil { ctxLogger.Error(nil, err.Error()) } } func middlewareResp(c *fiber.Ctx) error { err := c.Next() if err != nil { return err } resp := Response{ Code: 0, Message: "", Data: c.Response().Body(), } return c.JSON(resp) } type Response struct { Code int `json:"code"` Message string `json:"message"` Data json.RawMessage `json:"data"` } func middlewareError(ctx *fiber.Ctx, err error) error { code := fiber.StatusInternalServerError msg := "" if e, ok := err.(*fiber.Error); ok { code = e.Code msg = e.Message } if err != nil { msg = err.Error() } return ctx.Status(code).JSON(Response{ Code: code, Message: msg, Data: nil, }) }