You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

23 lines
527 B

  1. package middleware
  2. import (
  3. "net/http"
  4. "github.com/google/uuid"
  5. )
  6. const RequestIDHeader = "X-Request-ID"
  7. // RequestID adds a unique X-Request-ID to each request and to the request context.
  8. // The same ID is set on the response header for tracing.
  9. func RequestID(next http.Handler) http.Handler {
  10. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  11. id := r.Header.Get(RequestIDHeader)
  12. if id == "" {
  13. id = uuid.New().String()
  14. }
  15. w.Header().Set(RequestIDHeader, id)
  16. next.ServeHTTP(w, r)
  17. })
  18. }