middleware.go 1.3 KB

12345678910111213141516171819202122232425262728
  1. package mux
  2. import "net/http"
  3. // MiddlewareFunc is a function which receives an http.Handler and returns another http.Handler.
  4. // Typically, the returned handler is a closure which does something with the http.ResponseWriter and http.Request passed
  5. // to it, and then calls the handler passed as parameter to the MiddlewareFunc.
  6. type MiddlewareFunc func(http.Handler) http.Handler
  7. // middleware interface is anything which implements a MiddlewareFunc named Middleware.
  8. type middleware interface {
  9. Middleware(handler http.Handler) http.Handler
  10. }
  11. // MiddlewareFunc also implements the middleware interface.
  12. func (mw MiddlewareFunc) Middleware(handler http.Handler) http.Handler {
  13. return mw(handler)
  14. }
  15. // 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.
  16. func (r *Router) Use(mwf MiddlewareFunc) {
  17. r.middlewares = append(r.middlewares, mwf)
  18. }
  19. // 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.
  20. func (r *Router) useInterface(mw middleware) {
  21. r.middlewares = append(r.middlewares, mw)
  22. }