https://github.com/gbrlsnchs/rest
RESTful HTTP handler for Go
https://github.com/gbrlsnchs/rest
go golang http http-handler rest restful
Last synced: 10 days ago
JSON representation
RESTful HTTP handler for Go
- Host: GitHub
- URL: https://github.com/gbrlsnchs/rest
- Owner: gbrlsnchs
- License: mit
- Created: 2018-10-18T19:15:54.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-10-24T18:19:57.000Z (over 6 years ago)
- Last Synced: 2025-02-07T13:54:07.344Z (4 months ago)
- Topics: go, golang, http, http-handler, rest, restful
- Language: Go
- Size: 8.79 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# rest (RESTful HTTP handler for Go)
[](https://travis-ci.org/gbrlsnchs/rest)
[](https://sourcegraph.com/github.com/gbrlsnchs/rest?badge)
[](https://godoc.org/github.com/gbrlsnchs/rest)
[](https://golang.org/doc/go1.10)## About
This package implements a simple RESTful HTTP handler that facilitates receiving requests or sending responses in JSON or XML by using a custom context.## Usage
Full documentation [here](https://godoc.org/github.com/gbrlsnchs/rest).### Installing
#### Go 1.10
`vgo get -u github.com/gbrlsnchs/rest`
#### Go 1.11 or after
`go get -u github.com/gbrlsnchs/rest`### Importing
```go
import (
// ..."github.com/gbrlsnchs/rest"
)
```### Setting a wrapper
#### Consider the following type to be received / sent
```go
type message struct {
content string `json:"content,omitempty"`
}
```#### Now, set the main handler
```go
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)
}),
})
```## Contributing
### How to help
- For bugs and opinions, please [open an issue](https://github.com/gbrlsnchs/rest/issues/new)
- For pushing changes, please [open a pull request](https://github.com/gbrlsnchs/rest/compare)