Skip to content

Commit

Permalink
完善token部分
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Nov 23, 2023
1 parent be99290 commit 47933c1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
6 changes: 5 additions & 1 deletion cmd/haobase/www/internal_api/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ func SetToken(ctx *gin.Context) {
return
}

token.Set(req.Token, req.UserId, req.Ttl)
err := token.Set(req.Token, req.UserId, req.Ttl)
if err != nil {
utils.ResponseFailJson(ctx, err.Error())
return
}
utils.ResponseOkJson(ctx, "")
}

Expand Down
17 changes: 14 additions & 3 deletions types/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@ package token

import (
"fmt"
"strings"

"github.com/gomodule/redigo/redis"
"github.com/yzimhao/trading_engine/utils"
"github.com/yzimhao/trading_engine/utils/app"
)

func Set(token string, user_id string, ttl int) {
func Set(token string, user_id string, ttl int) error {
rdc := app.RedisPool().Get()
defer rdc.Close()

if strings.Contains(token, ".") {
return fmt.Errorf("token contains `.`")
}

topic := tokenRedisTopic(token)
rdc.Do("set", topic, user_id)
rdc.Do("expire", topic, ttl)
if _, err := rdc.Do("set", topic, user_id); err != nil {
return err
}
if _, err := rdc.Do("expire", topic, ttl); err != nil {
return err
}

return nil
}

func Get(original_token string) string {
Expand Down

0 comments on commit 47933c1

Please sign in to comment.