github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/go-chi/chi/middleware/value.go (about)

     1  package middleware
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/hellobchain/newcryptosm/http"
     7  )
     8  
     9  // WithValue is a middleware that sets a given key/value in a context chain.
    10  func WithValue(key interface{}, val interface{}) func(next http.Handler) http.Handler {
    11  	return func(next http.Handler) http.Handler {
    12  		fn := func(w http.ResponseWriter, r *http.Request) {
    13  			r = r.WithContext(context.WithValue(r.Context(), key, val))
    14  			next.ServeHTTP(w, r)
    15  		}
    16  		return http.HandlerFunc(fn)
    17  	}
    18  }