feat(requestid): reqID in the header

This commit is contained in:
Muyao CHEN 2024-10-03 13:48:36 +02:00
parent 7024c76032
commit 2a52fb6b88
2 changed files with 12 additions and 3 deletions

View File

@ -2,8 +2,19 @@ package middleware
import "github.com/gin-gonic/gin"
const requestID = "X-Request-Id"
func RequestID() gin.HandlerFunc {
return func(ctx *gin.Context) {
ctx.Set("X-Request-Id", "123")
var rid string
if rid = ctx.GetString(requestID); rid != "" {
// request id exists already
ctx.Next()
}
if rid = ctx.GetHeader(requestID); rid != "" {
ctx.Set(requestID, rid)
}
}
}

View File

@ -9,8 +9,6 @@ import (
"github.com/stretchr/testify/assert"
)
const requestID = "X-Request-Id"
type header struct {
Key string
Value string