2025-03-04 12:56:57 +00:00
|
|
|
package http
|
2024-11-26 16:30:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ContextValsMiddleware(pairs map[any]any) HandlerBuilder {
|
|
|
|
return func(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
for key, val := range pairs {
|
|
|
|
ctx = context.WithValue(ctx, key, val)
|
|
|
|
}
|
|
|
|
newRequest := r.WithContext(ctx)
|
|
|
|
h.ServeHTTP(w, newRequest)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|