44 lines
516 B
Go
44 lines
516 B
Go
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type contextKey string
|
|
|
|
const RequestIDKey contextKey = "request_id"
|
|
|
|
func RequestID(
|
|
next http.Handler,
|
|
) http.Handler {
|
|
|
|
return http.HandlerFunc(
|
|
func(
|
|
w http.ResponseWriter,
|
|
r *http.Request,
|
|
) {
|
|
|
|
id := uuid.NewString()
|
|
|
|
ctx := context.WithValue(
|
|
r.Context(),
|
|
RequestIDKey,
|
|
id,
|
|
)
|
|
|
|
w.Header().Set(
|
|
"X-Request-ID",
|
|
id,
|
|
)
|
|
|
|
next.ServeHTTP(
|
|
w,
|
|
r.WithContext(ctx),
|
|
)
|
|
},
|
|
)
|
|
}
|