README ¶
github.com/gobuffalo/x/responder
$ go get -u github.com/gobuffalo/x/responder
This packages allows you to easily write "responders" that trigger different functions based on the incoming request Content-Type
header.
For example, if you have a UserList
buffalo.Handler
that you want to be able to respond differently to HTML
or JSON
you can write a couple of responder functions that handle those different requests.
func UserList(c buffalo.Context) error {
// do some work
return responder.Wants("html", func (c buffalo.Context) error {
return c.Render(200, r.HTML("some/template.html"))
}).Wants("json", func (c buffalo.Context) error {
return c.Render(200, r.JSON(user))
}).Respond(c)
}
// Alternate version
func UserList(c buffalo.Context) error {
// do some work
res := responder.Wants("html", func (c buffalo.Context) error {
return c.Render(200, r.HTML("some/template.html"))
})
res.Wants("json", func (c buffalo.Context) error {
return c.Render(200, r.JSON(user))
})
return res.Respond(c)
}
Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Responder ¶
type Responder struct {
// contains filtered or unexported fields
}
Responder holds the mappings of content-type to handler
func Wants ¶
Wants maps a content-type, or part of one ("json", "html", "form", etc...), to a buffalo.Handler to respond with when it gets that content-type, returns a Responder that can be used for further mappings.
func (Responder) Respond ¶
Respond with a mapped buffalo.Handler, if one exists, returns an error if one does not.
func UserList(c buffalo.Context) error { // do some work return responder.Wants("html", func (c buffalo.Context) error { return c.Render(200, r.HTML("some/template.html") }).Wants("json", func (c buffalo.Context) error { return c.Render(200, r.JSON(user)) }).Respond(c) }