https://github.com/zimbatm/go-httpresource
simple HTTP end-point
https://github.com/zimbatm/go-httpresource
go http
Last synced: 8 months ago
JSON representation
simple HTTP end-point
- Host: GitHub
- URL: https://github.com/zimbatm/go-httpresource
- Owner: zimbatm
- License: mit
- Created: 2019-04-22T14:53:59.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T11:55:42.000Z (over 2 years ago)
- Last Synced: 2025-07-11T10:38:26.486Z (9 months ago)
- Topics: go, http
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-httpresource - simple HTTP end-point
The go stdlib `net/http` library is quite good for providing a simple routing
experience but some times you want just a bit more. This library is designed
to work in conjuction and add a dispatch on HTTP methods to the mix. Some
times it's nice to have APIs being a bit more REST-ful-y.
## Example
[$ examples/simple.go](examples/simple.go)
```go
package main
import (
"net/http"
"github.com/zimbatm/go-httpresource"
)
func main() {
// Only reply to GET and HEAD requests
http.Handle("/hello", httpresource.GET(func (w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
}))
// A more complex example
http.Handle("/users", httpresource.Resource {
GET: http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
// Return the list of all users
}),
POST: http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
// Create a new user
}),
})
err := http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
}
```
## Related projects
* [Gorilla Mux](https://github.com/gorilla/mux) - A more featureful all-in-one
HTTP router
## License
MIT (c) 2019 - zimbatm and contributors