wisski-cloud-distillery/pkg/httpx/wrap.go
2023-01-15 19:41:00 +01:00

14 lines
416 B
Go

package httpx
import (
"context"
"net/http"
)
// WithContextWrapper generates a new handler that wraps the context of each request with the wrapper function.
func WithContextWrapper(handler http.Handler, wrapper func(context.Context) context.Context) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(wrapper(r.Context()))
handler.ServeHTTP(w, r)
})
}