chore: controller ctx
This commit is contained in:
parent
459e3d9c09
commit
d08c56093d
@ -1,10 +1,10 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/loveuer/nf"
|
||||
"github.com/loveuer/nf/nft/resp"
|
||||
"github.com/spf13/cast"
|
||||
"gorm.io/gorm"
|
||||
@ -19,19 +19,19 @@ import (
|
||||
)
|
||||
|
||||
type userController interface {
|
||||
GetUser(c *nf.Ctx, id uint64) (*model.User, error)
|
||||
GetUserByToken(c *nf.Ctx, token string) (*model.User, error)
|
||||
CacheUser(c *nf.Ctx, user *model.User) error
|
||||
CacheToken(c *nf.Ctx, token string, user *model.User) error
|
||||
RmUserCache(c *nf.Ctx, id uint64) error
|
||||
DeleteUser(c *nf.Ctx, id uint64) error
|
||||
GetUser(ctx context.Context, id uint64) (*model.User, error)
|
||||
GetUserByToken(ctx context.Context, token string) (*model.User, error)
|
||||
CacheUser(ctx context.Context, user *model.User) error
|
||||
CacheToken(ctx context.Context, token string, user *model.User) error
|
||||
RmUserCache(ctx context.Context, id uint64) error
|
||||
DeleteUser(ctx context.Context, id uint64) error
|
||||
}
|
||||
|
||||
type uc struct{}
|
||||
|
||||
var _ userController = (*uc)(nil)
|
||||
|
||||
func (u uc) GetUser(c *nf.Ctx, id uint64) (*model.User, error) {
|
||||
func (u uc) GetUser(ctx context.Context, id uint64) (*model.User, error) {
|
||||
var (
|
||||
err error
|
||||
target = new(model.User)
|
||||
@ -41,12 +41,12 @@ func (u uc) GetUser(c *nf.Ctx, id uint64) (*model.User, error) {
|
||||
|
||||
if opt.EnableUserCache {
|
||||
if bs, err = cache.Client.Get(tool.Timeout(3), key); err != nil {
|
||||
log.Warn(c.Context(), "controller.GetUser: get user by cache key=%s err=%v", key, err)
|
||||
log.Warn(ctx, "controller.GetUser: get user by cache key=%s err=%v", key, err)
|
||||
goto ByDB
|
||||
}
|
||||
|
||||
if err = json.Unmarshal(bs, target); err != nil {
|
||||
log.Warn(c.Context(), "controller.GetUser: json unmarshal key=%s by=%s err=%v", key, string(bs), err)
|
||||
log.Warn(ctx, "controller.GetUser: json unmarshal key=%s by=%s err=%v", key, string(bs), err)
|
||||
goto ByDB
|
||||
}
|
||||
|
||||
@ -68,15 +68,15 @@ ByDB:
|
||||
}
|
||||
|
||||
if opt.EnableUserCache {
|
||||
if err = u.CacheUser(c, target); err != nil {
|
||||
log.Warn(c.Context(), "controller.GetUser: cache user key=%s err=%v", key, err)
|
||||
if err = u.CacheUser(ctx, target); err != nil {
|
||||
log.Warn(ctx, "controller.GetUser: cache user key=%s err=%v", key, err)
|
||||
}
|
||||
}
|
||||
|
||||
return target, nil
|
||||
}
|
||||
|
||||
func (u uc) GetUserByToken(c *nf.Ctx, token string) (*model.User, error) {
|
||||
func (u uc) GetUserByToken(ctx context.Context, token string) (*model.User, error) {
|
||||
strs := strings.Split(token, ".")
|
||||
if len(strs) != 3 {
|
||||
return nil, fmt.Errorf("controller.GetUserByToken: jwt token invalid, token=%s", token)
|
||||
@ -88,7 +88,7 @@ func (u uc) GetUserByToken(c *nf.Ctx, token string) (*model.User, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Debug(c.Context(), "controller.GetUserByToken: key=%s cache bytes=%s", key, string(bs))
|
||||
log.Debug(ctx, "controller.GetUserByToken: key=%s cache bytes=%s", key, string(bs))
|
||||
|
||||
userId := cast.ToUint64(string(bs))
|
||||
if userId == 0 {
|
||||
@ -97,19 +97,19 @@ func (u uc) GetUserByToken(c *nf.Ctx, token string) (*model.User, error) {
|
||||
|
||||
var op *model.User
|
||||
|
||||
if op, err = u.GetUser(c, userId); err != nil {
|
||||
if op, err = u.GetUser(ctx, userId); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return op, nil
|
||||
}
|
||||
|
||||
func (u uc) CacheUser(c *nf.Ctx, target *model.User) error {
|
||||
func (u uc) CacheUser(ctx context.Context, target *model.User) error {
|
||||
key := fmt.Sprintf("%s:user:id:%d", opt.CachePrefix, target.Id)
|
||||
return cache.Client.Set(tool.Timeout(3), key, target)
|
||||
}
|
||||
|
||||
func (u uc) CacheToken(c *nf.Ctx, token string, user *model.User) error {
|
||||
func (u uc) CacheToken(ctx context.Context, token string, user *model.User) error {
|
||||
strs := strings.Split(token, ".")
|
||||
if len(strs) != 3 {
|
||||
return fmt.Errorf("controller.CacheToken: jwt token invalid")
|
||||
@ -118,12 +118,12 @@ func (u uc) CacheToken(c *nf.Ctx, token string, user *model.User) error {
|
||||
key := fmt.Sprintf("%s:user:token:%s", opt.CachePrefix, strs[2])
|
||||
return cache.Client.SetEx(tool.Timeout(3), key, user.Id, opt.TokenTimeout)
|
||||
}
|
||||
func (u uc) RmUserCache(c *nf.Ctx, id uint64) error {
|
||||
func (u uc) RmUserCache(ctx context.Context, id uint64) error {
|
||||
key := fmt.Sprintf("%s:user:id:%d", opt.CachePrefix, id)
|
||||
return cache.Client.Del(tool.Timeout(3), key)
|
||||
}
|
||||
|
||||
func (u uc) DeleteUser(c *nf.Ctx, id uint64) error {
|
||||
func (u uc) DeleteUser(ctx context.Context, id uint64) error {
|
||||
var (
|
||||
err error
|
||||
now = time.Now()
|
||||
@ -145,8 +145,8 @@ func (u uc) DeleteUser(c *nf.Ctx, id uint64) error {
|
||||
}
|
||||
|
||||
if opt.EnableUserCache {
|
||||
if err = u.RmUserCache(c, id); err != nil {
|
||||
log.Warn(c.Context(), "controller.DeleteUser: rm user=%d cache err=%v", id, err)
|
||||
if err = u.RmUserCache(ctx, id); err != nil {
|
||||
log.Warn(ctx, "controller.DeleteUser: rm user=%d cache err=%v", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -58,7 +58,7 @@ func AuthLogin(c *nf.Ctx) error {
|
||||
return resp.Resp401(c, nil, err.Error())
|
||||
}
|
||||
|
||||
if err = controller.UserController.CacheUser(c, target); err != nil {
|
||||
if err = controller.UserController.CacheUser(c.Context(), target); err != nil {
|
||||
return resp.RespError(c, err)
|
||||
}
|
||||
|
||||
@ -66,7 +66,7 @@ func AuthLogin(c *nf.Ctx) error {
|
||||
return resp.Resp500(c, err.Error())
|
||||
}
|
||||
|
||||
if err = controller.UserController.CacheToken(c, token, target); err != nil {
|
||||
if err = controller.UserController.CacheToken(c.Context(), token, target); err != nil {
|
||||
return resp.RespError(c, err)
|
||||
}
|
||||
|
||||
@ -116,7 +116,7 @@ func AuthLogout(c *nf.Ctx) error {
|
||||
return resp.Resp401(c, nil)
|
||||
}
|
||||
|
||||
_ = controller.UserController.RmUserCache(c, op.Id)
|
||||
_ = controller.UserController.RmUserCache(c.Context(), op.Id)
|
||||
|
||||
c.Locals(opt.OpLogLocalKey, &oplog.OpLog{
|
||||
Type: model.OpLogTypeLogout,
|
||||
@ -332,7 +332,7 @@ func ManageUserUpdate(c *nf.Ctx) error {
|
||||
return resp.Resp400(c, "未指定目标用户")
|
||||
}
|
||||
|
||||
if target, err = controller.UserController.GetUser(c, req.Id); err != nil {
|
||||
if target, err = controller.UserController.GetUser(c.Context(), req.Id); err != nil {
|
||||
return resp.RespError(c, err)
|
||||
}
|
||||
|
||||
@ -423,7 +423,7 @@ func ManageUserUpdate(c *nf.Ctx) error {
|
||||
return resp.Resp500(c, err.Error())
|
||||
}
|
||||
|
||||
if err = controller.UserController.RmUserCache(c, req.Id); err != nil {
|
||||
if err = controller.UserController.RmUserCache(c.Context(), req.Id); err != nil {
|
||||
return resp.RespError(c, err)
|
||||
}
|
||||
|
||||
@ -465,7 +465,7 @@ func ManageUserDelete(c *nf.Ctx) error {
|
||||
return resp.Resp400(c, nil, "无法删除自己")
|
||||
}
|
||||
|
||||
if target, err = controller.UserController.GetUser(c, req.Id); err != nil {
|
||||
if target, err = controller.UserController.GetUser(c.Context(), req.Id); err != nil {
|
||||
return resp.RespError(c, err)
|
||||
}
|
||||
|
||||
@ -473,7 +473,7 @@ func ManageUserDelete(c *nf.Ctx) error {
|
||||
return resp.Resp403(c, nil)
|
||||
}
|
||||
|
||||
if err = controller.UserController.DeleteUser(c, target.Id); err != nil {
|
||||
if err = controller.UserController.DeleteUser(c.Context(), target.Id); err != nil {
|
||||
return resp.RespError(c, err)
|
||||
}
|
||||
|
||||
|
@ -33,7 +33,7 @@ func NewAuth() nf.HandlerFunc {
|
||||
|
||||
log.Debug(c.Context(), "middleware.NewAuth: token=%s", token)
|
||||
|
||||
target, err := controller.UserController.GetUserByToken(c, token)
|
||||
target, err := controller.UserController.GetUserByToken(c.Context(), token)
|
||||
if err != nil {
|
||||
log.Error(c.Context(), "middleware.NewAuth: get user by token=%s err=%v", token, err)
|
||||
if errors.Is(err, redis.Nil) || errors.Is(err, gredis.ErrKeyNotFound) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user