You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
1.3 KiB
30 lines
1.3 KiB
package mux |
|
|
|
import "net/http" |
|
|
|
// MiddlewareFunc is a function which receives an http.Handler and returns another http.Handler. |
|
// Typically, the returned handler is a closure which does something with the http.ResponseWriter and http.Request passed |
|
// to it, and then calls the handler passed as parameter to the MiddlewareFunc. |
|
type MiddlewareFunc func(http.Handler) http.Handler |
|
|
|
// middleware interface is anything which implements a MiddlewareFunc named Middleware. |
|
type middleware interface { |
|
Middleware(handler http.Handler) http.Handler |
|
} |
|
|
|
// MiddlewareFunc also implements the middleware interface. |
|
func (mw MiddlewareFunc) Middleware(handler http.Handler) http.Handler { |
|
return mw(handler) |
|
} |
|
|
|
// Use appends a MiddlewareFunc to the chain. Middleware can be used to intercept or otherwise modify requests and/or responses, and are executed in the order that they are applied to the Router. |
|
func (r *Router) Use(mwf ...MiddlewareFunc) { |
|
for _, fn := range mwf { |
|
r.middlewares = append(r.middlewares, fn) |
|
} |
|
} |
|
|
|
// useInterface appends a middleware to the chain. Middleware can be used to intercept or otherwise modify requests and/or responses, and are executed in the order that they are applied to the Router. |
|
func (r *Router) useInterface(mw middleware) { |
|
r.middlewares = append(r.middlewares, mw) |
|
}
|
|
|