u
This commit is contained in:
1 parent
8a3dea8545
commit
1903861354
21 files changed
+335
-236
No files matched your search
@@ -20,7 +20,7 @@ func Logger() fiber.Handler {
|
||||
duration := time.Since(start)
|
||||
|
||||
// 记录日志
|
||||
log.Infow("API Request",
|
||||
log.WithContext(c.Context()).Infow("API Request",
|
||||
"method", c.Method(),
|
||||
"path", c.Path(),
|
||||
"status", c.Response().StatusCode(),
|
||||
|
||||
@@ -17,46 +17,60 @@ const CtxSpaceIDKey = "space_id"
|
||||
func SpaceGuard() fiber.Handler {
|
||||
return func(c fiber.Ctx) error {
|
||||
userID, err := GetUserID(c)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
spaceIDStr := strings.TrimSpace(c.Get("SPACE_ID"))
|
||||
|
||||
spaceID, err := strconv.ParseInt(spaceIDStr, 10, 64)
|
||||
if err != nil || spaceID <= 0 {
|
||||
return responsex.SpaceError(c, "请选择空间")
|
||||
}
|
||||
|
||||
// 校验
|
||||
// 1 先查 Redis
|
||||
inSpace, err := redisx.SpaceIsMember(spaceID, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if inSpace {
|
||||
c.Locals(CtxSpaceIDKey, spaceID)
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
// 检验空间是否存在
|
||||
space, err := db.Queries.GetSpaceById(c.Context(), spaceID)
|
||||
|
||||
// 2 Redis miss → 查数据库
|
||||
role, err := db.Queries.GetUserSpaceRole(c.Context(), repo.GetUserSpaceRoleParams{
|
||||
SpaceID: spaceID,
|
||||
UserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
if errorx.IsNotFound(err) {
|
||||
// 未找到
|
||||
return responsex.SpaceError(c, "空间已失效")
|
||||
return responsex.SpaceError(c, "未加入空间")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// 空间有效,判断是否在该空间内
|
||||
role, err := db.Queries.GetUserSpaceRole(c.Context(), repo.GetUserSpaceRoleParams{
|
||||
SpaceID: spaceID,
|
||||
UserID: spaceID,
|
||||
})
|
||||
if err != nil {
|
||||
if errorx.IsNotFound(err) {
|
||||
|
||||
}
|
||||
}
|
||||
// 3 写回 Redis(重建缓存)
|
||||
_ = redisx.SpaceAddOrUpdateMember(spaceID, userID, int(role))
|
||||
c.Locals(CtxSpaceIDKey, spaceID)
|
||||
|
||||
// 4 放行
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func GetSpaceID(c fiber.Ctx) (int64, error) {
|
||||
v := c.Locals(CtxSpaceIDKey)
|
||||
|
||||
if v == nil {
|
||||
return 0, responsex.SpaceError(c, "空间无效")
|
||||
}
|
||||
|
||||
spaceID, ok := v.(int64)
|
||||
|
||||
if !ok {
|
||||
return 0, responsex.SpaceError(c, "空间无效")
|
||||
}
|
||||
|
||||
return spaceID, nil
|
||||
}
|
||||
@@ -10,13 +10,22 @@ import (
|
||||
func SetupRouters(app *fiber.App) {
|
||||
api := app.Group(config.GetConf().Server.BaseUrl)
|
||||
|
||||
public := api.Group("")
|
||||
//private := api.Group("", middleware.Auth())
|
||||
// 1️⃣ 公开接口(无需登录)
|
||||
publicGroup := api.Group("")
|
||||
|
||||
// 2️⃣ 需要登录
|
||||
//authGroup := api.Group("", middleware.Auth())
|
||||
//
|
||||
//// 3️⃣ 需要登录 + 需要空间
|
||||
//spaceGroup := api.Group("", middleware.Auth(), middleware.SpaceGuard())
|
||||
|
||||
// --------------------
|
||||
// auth
|
||||
authRouter := public.Group("/auth")
|
||||
authRouter := publicGroup.Group("/auth")
|
||||
authRouter.Post("/login/qq", auth.LoginQq)
|
||||
authRouter.Post("/login/wechat", auth.LoginWechat)
|
||||
|
||||
// 其他的都带中间件
|
||||
// 示例
|
||||
// authGroup.Get("/user/profile", user.GetProfile)
|
||||
// spaceGroup.Get("/space/info", space.GetInfo)
|
||||
}
|
||||
Reference in new issue
Block a user