整理requestId入口,兼容istio的header返回

This commit is contained in:
linwenxiang committed 2021-01-29 14:53:52 +08:00
1 parent 128739f097
commit f67e653abe
1 file changed
+8 -11
+8 -11
View File
@@ -3,17 +3,17 @@ package tools
import ( import (
"errors" "errors"
"fmt" "fmt"
"gorm.io/gorm"
"log" "log"
"runtime" "runtime"
"strconv" "strconv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/spf13/cast"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
) )
const TrafficKey = "X-Request-Id"
func CompareHashAndPassword(e string, p string) (bool, error) { func CompareHashAndPassword(e string, p string) (bool, error) {
err := bcrypt.CompareHashAndPassword([]byte(e), []byte(p)) err := bcrypt.CompareHashAndPassword([]byte(e), []byte(p))
if err != nil { if err != nil {
@@ -56,15 +56,12 @@ func HasError(err error, msg string, code ...int) {
// GenerateMsgIDFromContext 生成msgID // GenerateMsgIDFromContext 生成msgID
func GenerateMsgIDFromContext(c *gin.Context) string { func GenerateMsgIDFromContext(c *gin.Context) string {
var msgID string requestId := c.GetHeader(TrafficKey)
data, ok := c.Get("msgID") if requestId == "" {
if !ok { fmt.Println("no", requestId)
msgID = uuid.New().String() c.Header(TrafficKey, requestId)
c.Set("msgID", msgID)
return msgID
} }
msgID = cast.ToString(data) return requestId
return msgID
} }
// GetOrm 获取orm连接 // GetOrm 获取orm连接