linstrom/server-old/middlewares/injectContextValues.go
2024-08-22 19:57:53 +02:00

18 lines
410 B
Go

package middlewares
import (
"context"
"net/http"
)
func InjectContextValuesBuilder(values map[ContextKey]any) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
for k, v := range values {
ctx = context.WithValue(ctx, k, v)
}
h.ServeHTTP(w, r.WithContext(ctx))
})
}
}