19 lines
408 B
Go
19 lines
408 B
Go
package http
|
|
|
|
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)
|
|
})
|
|
}
|
|
}
|