MuxChain is a small package designed to complement net/http for specifying chains of handlers. With it, you can succinctly compose layers of middleware without introducing large dependencies or effectively defeating the type system.

muxchain.Chain("/", logger, gzipHandler, echoHandler)
http.ListenAndServe(":8080", muxchain.Default)

This specifies that all patterns matched should be handled by the logger, then gzip, then echo. Since we're chaining to the default MuxChain, we can just pass that to http.ListenAndServe. You can see a more complete example in the "sample" directory.


BSD 3-clause (see LICENSE file)




This section is empty.


View Source
var Default = &MuxChain{}


func Chain

func Chain(pattern string, handlers ...http.Handler)

    Chain registers the pattern and http.Handler chain to the DefaultMuxChain.

    func ChainHandlers

    func ChainHandlers(handlers ...http.Handler) http.Handler

      ChainHandlers chains together a set of handlers under an empty path.

      func HandleChain

      func HandleChain(w http.ResponseWriter, req *http.Request, handlers ...http.Handler)

        HandleChain is the utility function chained handlers are responsible for calling when they are complete.


        type ChainedHandler

        type ChainedHandler interface {
        	// ServeHTTPChain allows the handler to do its work and then call the remaining
        	// handler chain. Implementers should call muxchain.HandleChain when complete.
        	ServeHTTPChain(w http.ResponseWriter, req *http.Request, h ...http.Handler)

          ChainedHander allows implementers to call the handlers after them in a muxchain on their own. This allows handlers to defer functions until after the handler chain following them has been completed.

          type ChainedHandlerFunc

          type ChainedHandlerFunc func(w http.ResponseWriter, req *http.Request, handlers ...http.Handler)

            ChainedHandlerFunc represents a handler that is able to be chained to subsequent handlers.

            func (ChainedHandlerFunc) ServeHTTP

            func (c ChainedHandlerFunc) ServeHTTP(w http.ResponseWriter, req *http.Request)

              ServeHTTP allows ChainedHandlerFuncs to implement http.Handler

              func (ChainedHandlerFunc) ServeHTTPChain

              func (c ChainedHandlerFunc) ServeHTTPChain(w http.ResponseWriter, req *http.Request, handlers ...http.Handler)

                ServeHTTPChain allows ChainedHandlerFuncs to be identified

                type CheckedResponseWriter

                type CheckedResponseWriter interface {
                	Written() bool

                  CheckedResponseWriter augments http.ResponseWriter to indicate if the response has been written to.

                  type MuxChain

                  type MuxChain struct {

                  func (*MuxChain) Chain

                  func (m *MuxChain) Chain(pattern string, handlers ...http.Handler)

                    Chain registers a pattern to a sequence of http.Handlers. Upon receiving a request, the mux chain will find the best matching pattern and call that chain of handlers. The handlers will be called in turn until one of them writes a response or the end of the chain is reached. If one of the handlers is a Muxer (e.g. http.ServeMux), the MuxChain will skip it if no pattern matches.

                    func (*MuxChain) ServeHTTPChain

                    func (m *MuxChain) ServeHTTPChain(w http.ResponseWriter, req *http.Request, handlers ...http.Handler)

                    type Muxer

                    type Muxer interface {
                    	Handle(pattern string, handler http.Handler)
                    	Handler(r *http.Request) (h http.Handler, pattern string)
                    	HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))

                      Muxer identifies types that act as a ServeMux.

                      Source Files


                      Path Synopsis