Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Chain ¶
type Chain []Middleware
Chain acts as a list of http.Handler middlewares. Chain is effectively immutable: once created, it will always hold the same set of middlewares in the same order.
func New ¶
func New(middlewares ...Middleware) Chain
New creates a new chain, memorizing the given list of middlewares. New serves no other function, middlewares are only constructed upon a call to Then().
func (Chain) Append ¶
func (c Chain) Append(middlewares ...Middleware) Chain
Append extends a chain, adding the specified middlewares as the last ones in the request flow.
chain := chain.New(m1, m2) chain = chain.Append(m3, m4) // requests in chain go m1 -> m2 -> m3 -> m4
func (Chain) Then ¶
Then chains the middleware and returns the final http.Handler.
chain.New(m1, m2, m3).Then(h)
is equivalent to:
m1(m2(m3(h)))
When the request comes in, it will be passed to m1, then m2, then m3 and finally, the given handler (assuming every middleware calls the following one).
A chain can be safely reused by calling Then() several times.
chain := chain.New(ratelimitHandler, csrfHandler) indexPipe = chain.Then(indexHandler) authPipe = chain.Then(authHandler)
Note that middlewares are called on every call to Then() and thus several instances of the same middleware will be created when a chain is reused in this previous example. For proper middleware, this should cause no problems.
Then() treats nil as http.DefaultServeMux.