forked from gorilla/mux
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] Add middleware support as discussed in gorilla#293 (gorilla#294)
* mux.Router now has a `Use` method that allows you to add middleware to request processing.
- Loading branch information
Showing
5 changed files
with
519 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,28 @@ | ||
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) { | ||
r.middlewares = append(r.middlewares, mwf) | ||
} | ||
|
||
// 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) | ||
} |
Oops, something went wrong.