diff options
Diffstat (limited to 'weed/server/common.go')
| -rw-r--r-- | weed/server/common.go | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/weed/server/common.go b/weed/server/common.go index 5dad9d81b..516f8bf1c 100644 --- a/weed/server/common.go +++ b/weed/server/common.go @@ -3,9 +3,12 @@ package weed_server import ( "bufio" "bytes" + "context" "encoding/json" "errors" "fmt" + "github.com/google/uuid" + "google.golang.org/grpc/metadata" "io" "io/fs" "mime/multipart" @@ -421,3 +424,21 @@ func ProcessRangeRequest(r *http.Request, w http.ResponseWriter, totalSize int64 } return nil } + +func requestIDMiddleware(h http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + reqID := r.Header.Get(util.RequestIdHttpHeader) + if reqID == "" { + reqID = uuid.New().String() + } + + ctx := context.WithValue(r.Context(), util.RequestIDKey, reqID) + ctx = metadata.NewOutgoingContext(ctx, + metadata.New(map[string]string{ + util.RequestIDKey: reqID, + })) + + w.Header().Set(util.RequestIdHttpHeader, reqID) + h(w, r.WithContext(ctx)) + } +} |
