修改日志记录所有参数方式,修改redis设置hash方法

This commit is contained in:
junleea 2024-06-13 11:11:05 +08:00
parent 3ee0bc61bd
commit 97f8935911
2 changed files with 5 additions and 7 deletions

10
main.go
View File

@ -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")

View File

@ -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