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 package user
import ( 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/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"
@ -27,22 +28,8 @@ func LoginHandle(c *fiber.Ctx) error {
return c.JSON(response) return c.JSON(response)
} }
// UserInfoHandle 管理员信息 // InfoHandle 管理员信息
func UserInfoHandle(c *fiber.Ctx) error { func InfoHandle(c *fiber.Ctx) error {
var p = struct { userInfo := admin.GetUserInfo(c)
Token string `json:"token"` return c.JSON(userInfo)
}{}
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)
} }

View File

@ -2,6 +2,7 @@ package main
import ( import (
"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/app/zeus/service/lark/pkg/admin"
"git.icechen.cn/monorepo/backend/pkg/api" "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"
@ -17,7 +18,7 @@ func main() {
userRouter := router.Group("/user") userRouter := router.Group("/user")
userRouter.Post("/login", user.LoginHandle) userRouter.Post("/login", user.LoginHandle)
userRouter.Post("/info", user.UserInfoHandle) userRouter.Post("/info", admin.MiddlewareLarkAdmin, user.InfoHandle)
err := router.Listen(":8080") err := router.Listen(":8080")
if err != nil { 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)
}