backend/app/zeus/api/lark/main.go

65 lines
1.2 KiB
Go
Raw Normal View History

2022-01-10 20:21:47 +08:00
package main
2022-01-12 01:44:10 +08:00
import (
"encoding/json"
2022-01-12 14:04:33 +08:00
2022-01-12 01:44:10 +08:00
"git.icechen.cn/monorepo/backend/app/zeus/api/lark/internal/user"
"github.com/gofiber/fiber/v2"
ctxLogger "github.com/luizsuper/ctxLoggers"
)
2022-01-10 20:21:47 +08:00
func main() {
2022-01-12 01:44:10 +08:00
router := fiber.New(fiber.Config{
AppName: "api-lark",
ErrorHandler: middlewareError,
})
2022-01-12 14:04:33 +08:00
// router.Use(middlewareResp)
2022-01-12 01:44:10 +08:00
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"`
2022-01-13 02:33:44 +08:00
Data json.RawMessage `json:"data,omitempty"`
2022-01-12 01:44:10 +08:00
}
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
}
2022-01-12 14:04:33 +08:00
if err != nil {
msg = err.Error()
}
2022-01-12 01:44:10 +08:00
return ctx.Status(code).JSON(Response{
Code: code,
Message: msg,
Data: nil,
2022-01-10 20:21:47 +08:00
})
}