package middleware import ( "net/http" "github.com/google/uuid" ) const RequestIDHeader = "X-Request-ID" // RequestID adds a unique X-Request-ID to each request and to the request context. // The same ID is set on the response header for tracing. func RequestID(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { id := r.Header.Get(RequestIDHeader) if id == "" { id = uuid.New().String() } w.Header().Set(RequestIDHeader, id) next.ServeHTTP(w, r) }) }