goutils/middleware/context.go
mStar 9d803a94ee
Add middlewares
- Builder for chaining multiple middlewares on top of a base handler
- Middleware for adding a map of values to the request context
- Middleware for adding zerolog logging to the request
2024-11-26 17:30:05 +01:00

19 lines
414 B
Go

package middleware
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)
})
}
}