mStar
9d803a94ee
- 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
19 lines
414 B
Go
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)
|
|
})
|
|
}
|
|
}
|