This package implements a simple RESTful HTTP handler that facilitates receiving requests or sending responses in JSON or XML by using a custom context.
Full documentation here.
vgo get -u github.com/gbrlsnchs/rest
go get -u github.com/gbrlsnchs/rest
import (
// ...
"github.com/gbrlsnchs/rest"
)
type message struct {
content string `json:"content,omitempty"`
}
http.Handle("/", &rest.Wrapper{
Handler: rest.HandlerFunc(func(ctx *rest.Context) {
var ping message
if err := ctx.ReceiveJSON(&ping); err != nil {
// handle error
}
if ping.content != "ping" {
ctx.Send(http.StatusBadRequest)
return
}
pong := message{"pong"}
ctx.SendJSON(pong, http.StatusOK)
}),
RecoverHandler: rest.HandlerFunc(func(ctx *rest.Context) {
ctx.Send(http.StatusInternalServerError)
}),
})
- For bugs and opinions, please open an issue
- For pushing changes, please open a pull request