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