feat: 封装中间件
continuous-integration/drone/push Build is passing Details

master
icechen 2022-01-14 01:28:43 +08:00
parent e033b49ba1
commit e932171ffa
3 changed files with 39 additions and 19 deletions

View File

@ -1,6 +1,7 @@
package user
import (
"git.icechen.cn/monorepo/backend/app/zeus/service/lark/pkg/admin"
"git.icechen.cn/monorepo/backend/pkg/api"
"git.icechen.cn/monorepo/backend/pkg/proto/zeus/lark"
"git.icechen.cn/monorepo/backend/pkg/rpc"
@ -27,22 +28,8 @@ func LoginHandle(c *fiber.Ctx) error {
return c.JSON(response)
}
// UserInfoHandle 管理员信息
func UserInfoHandle(c *fiber.Ctx) error {
var p = struct {
Token string `json:"token"`
}{}
err := api.Bind(c, &p)
if err != nil {
return err
}
response, err := lark.NewUserClient(rpc.GetServiceConn("lark")).Info(c.Context(), &lark.InfoRequest{
Token: p.Token,
})
if err != nil {
return err
}
return c.JSON(response)
// InfoHandle 管理员信息
func InfoHandle(c *fiber.Ctx) error {
userInfo := admin.GetUserInfo(c)
return c.JSON(userInfo)
}

View File

@ -2,6 +2,7 @@ package main
import (
"git.icechen.cn/monorepo/backend/app/zeus/api/lark/internal/user"
"git.icechen.cn/monorepo/backend/app/zeus/service/lark/pkg/admin"
"git.icechen.cn/monorepo/backend/pkg/api"
"github.com/gofiber/fiber/v2"
ctxLogger "github.com/luizsuper/ctxLoggers"
@ -17,7 +18,7 @@ func main() {
userRouter := router.Group("/user")
userRouter.Post("/login", user.LoginHandle)
userRouter.Post("/info", user.UserInfoHandle)
userRouter.Post("/info", admin.MiddlewareLarkAdmin, user.InfoHandle)
err := router.Listen(":8080")
if err != nil {

View File

@ -0,0 +1,32 @@
package admin
import (
"git.icechen.cn/monorepo/backend/pkg/proto/zeus/lark"
"git.icechen.cn/monorepo/backend/pkg/rpc"
"github.com/gofiber/fiber/v2"
)
const AuthorizationHeader = "Authorization"
const UserInfoKey = "user_info"
type UserInfo = *lark.InfoResponse
// MiddlewareLarkAdmin 飞书管理员token校验
func MiddlewareLarkAdmin(c *fiber.Ctx) error {
token := c.Get(AuthorizationHeader)
response, err := lark.NewUserClient(rpc.GetServiceConn("lark")).Info(c.Context(), &lark.InfoRequest{
Token: token,
})
if err != nil {
return err
}
c.Context().SetUserValue(UserInfoKey, response)
return c.Next()
}
// GetUserInfo 获取用户信息
func GetUserInfo(c *fiber.Ctx) UserInfo {
return c.Context().UserValue(UserInfoKey).(UserInfo)
}