Documentation ¶
Overview ¶
Package httprouter_sandwich is a httprouter-adapter for sandwich that provides the httprouter path parameters to the middleware stack.
The Middleware implementation in this package is identical to the normal sandwich.Middleware except:
- it provides the httprouter.Params type by default for accessing route parameters
- it has a .H field for getting the httprouter handler easily.
Here's a simple example of using this:
s := httprouter_sandwich.TheUsual().Provide(...).With(...) m := httprouter.New() ... m.GET("/user/:id/", s.Provide(userdb).With(getUser).H) ... func getUser(w http.ResponseWriter, p httprouter.Params, udb UserDb) error { userId := p["id"] user, err := udb.Lookup(userId) if err != nil { return err // or wrap with sandwich.Error{...} } return json.NewEncoder(w).Encode(user) }
Index ¶
- type Middleware
- func (m Middleware) Code(pkg, funcName string) string
- func (m Middleware) H(w http.ResponseWriter, r *http.Request, p httprouter.Params)
- func (m Middleware) OnErr(handler interface{}) Middleware
- func (m Middleware) Provide(val interface{}) Middleware
- func (m Middleware) ProvideAs(val, ifacePtr interface{}) Middleware
- func (m Middleware) With(handlers ...interface{}) Middleware
- func (m Middleware) Wrap(before, after interface{}) Middleware
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Middleware ¶
type Middleware sandwich.Middleware
Middleware is the stack of middleware functions that powers sandwich's deliciousness. In addition to the default sandwich.Middleware, this supports the httprouter.Params type so you can get the route parameters in your handlers.
httprouter expects a function rather than a particular interface, so use the .H accessor to get a httprouter-friendly handler.
func New ¶
func New() Middleware
New constructs a clean Middleware instance that provides httprouter's routing params (httprouter.Params), ready for you to start piling on the handlers.
func TheUsual ¶
func TheUsual() Middleware
TheUsual constructs a popular new Middleware instance with some delicious default handlers installed and ready to go: Request logging and simple error handling. It also provides httprouter's routing params (httprouter.Params).
func (Middleware) Code ¶
func (m Middleware) Code(pkg, funcName string) string
Code is the same as (sandwich.Middleware).Code.
func (Middleware) H ¶
func (m Middleware) H(w http.ResponseWriter, r *http.Request, p httprouter.Params)
H is the httprouter middleware handling function. You normally won't call this function directly but rather you'll pass it to httprouter.
func (Middleware) OnErr ¶
func (m Middleware) OnErr(handler interface{}) Middleware
OnErr is the same as (sandwich.Middleware).OnErr, but returns an httprouter_sandwich.Middleware.
func (Middleware) Provide ¶
func (m Middleware) Provide(val interface{}) Middleware
Provide is the same as (sandwich.Middleware).Provide, but returns an httprouter_sandwich.Middleware.
func (Middleware) ProvideAs ¶
func (m Middleware) ProvideAs(val, ifacePtr interface{}) Middleware
ProvideAs is the same as (sandwich.Middleware).ProvideAs, but returns an httprouter_sandwich.Middleware.
func (Middleware) With ¶
func (m Middleware) With(handlers ...interface{}) Middleware
With is the same as (sandwich.Middleware).With, but returns an httprouter_sandwich.Middleware.
func (Middleware) Wrap ¶
func (m Middleware) Wrap(before, after interface{}) Middleware
Wrap is the same as (sandwich.Middleware).Wrap, but returns an httprouter_sandwich.Middleware.