修改日志记录所有参数方式,修改redis设置hash方法
This commit is contained in:
parent
3ee0bc61bd
commit
97f8935911
10
main.go
10
main.go
|
|
@ -1,7 +1,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt"
|
||||
|
|
@ -40,10 +39,9 @@ func writeLogger(c *gin.Context) {
|
|||
if method == "POST" {
|
||||
params = c.Request.PostForm.Encode()
|
||||
if params == "" {
|
||||
// 克隆请求
|
||||
req := c.Request.Clone(context.Background())
|
||||
// 读取原始请求体内容
|
||||
bodyBytes, _ := io.ReadAll(req.Body)
|
||||
// 请求体
|
||||
bodyBytes, _ := io.ReadAll(c.Request.Body)
|
||||
c.Request.Body = io.NopCloser(strings.NewReader(string(bodyBytes))) // Write body back, so other handler can read it too
|
||||
params = string(bodyBytes)
|
||||
}
|
||||
}
|
||||
|
|
@ -55,7 +53,7 @@ func writeLogger(c *gin.Context) {
|
|||
|
||||
func JWTAuthMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
go writeLogger(c)
|
||||
writeLogger(c)
|
||||
// 从请求头中获取 JWT 令牌
|
||||
tokenString := c.Request.Header.Get("token")
|
||||
|
||||
|
|
|
|||
|
|
@ -88,7 +88,7 @@ func SetHashWithTime(key string, id int, name, email string, duration time.Durat
|
|||
// 设置redis hash,设置过期时间
|
||||
func SetHash(key string, data map[string]interface{}) bool {
|
||||
ctx := context.Background()
|
||||
err := redisClient.HMSet(ctx, key, data).Err()
|
||||
err := redisClient.HSet(ctx, key, data).Err()
|
||||
if err != nil {
|
||||
fmt.Println("%v :Error setting hash: %v", key, err)
|
||||
return false
|
||||
|
|
|
|||
Loading…
Reference in New Issue