github.com/icyphox/x@v0.0.355-0.20220311094250-029bd783e8b8/httpx/chan_handler.go (about)

     1  package httpx
     2  
     3  import "net/http"
     4  
     5  type chanHandler <-chan http.HandlerFunc
     6  
     7  var _ http.Handler = chanHandler(nil)
     8  
     9  func (c chanHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    10  	(<-c)(w, r)
    11  }
    12  
    13  // NewChanHandler returns a new handler and corresponding channel for sending handler funcs.
    14  // Useful for testing. The argument buf specifies the channel capacity, so pass 0 for a sync handler.
    15  func NewChanHandler(buf int) (http.Handler, chan<- http.HandlerFunc) {
    16  	c := make(chan http.HandlerFunc, buf)
    17  	return chanHandler(c), c
    18  }