summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/go-chi/chi/middleware/value.go
blob: fbbd0393fb986aada69e9d9d4bc234c7ed5c41eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package middleware

import (
	"context"
	"net/http"
)

// WithValue is a middleware that sets a given key/value in a context chain.
func WithValue(key interface{}, val interface{}) func(next http.Handler) http.Handler {
	return func(next http.Handler) http.Handler {
		fn := func(w http.ResponseWriter, r *http.Request) {
			r = r.WithContext(context.WithValue(r.Context(), key, val))
			next.ServeHTTP(w, r)
		}
		return http.HandlerFunc(fn)
	}
}