https://github.com/vbatoufflet/httprouter
Basic HTTP router for Go
https://github.com/vbatoufflet/httprouter
golang http router
Last synced: 9 months ago
JSON representation
Basic HTTP router for Go
- Host: GitHub
- URL: https://github.com/vbatoufflet/httprouter
- Owner: vbatoufflet
- License: bsd-3-clause
- Archived: true
- Created: 2016-11-04T16:32:14.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2020-04-12T13:14:57.000Z (almost 6 years ago)
- Last Synced: 2025-03-16T12:16:58.936Z (about 1 year ago)
- Topics: golang, http, router
- Language: Go
- Size: 23.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# httprouter: HTTP router [![GoDoc][godoc-badge]][godoc-url] [![Travis CI][travis-badge]][travis-url]
Basic HTTP router for Go.
## Example
The following code:
```go
package main
import (
"fmt"
"log"
"net/http"
"batou.dev/httprouter"
)
func main() {
r := httprouter.NewRouter()
r.Endpoint("/foo").
Get(handleFoo).
Post(handleFoo)
r.Endpoint("/bar/:baz").
Get(handleBar)
r.Endpoint("/*").
Get(handleDefault)
s := &http.Server{
Addr: ":8080",
Handler: r,
}
err := s.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}
func handleBar(rw http.ResponseWriter, r *http.Request) {
fmt.Fprintf(rw, "Received %q\n", httprouter.ContextParam(r, "baz").(string))
}
func handleDefault(rw http.ResponseWriter, r *http.Request) {
fmt.Fprintln(rw, "Default here!")
}
func handleFoo(rw http.ResponseWriter, r *http.Request) {
fmt.Fprintf(rw, "Received %q request\n", r.Method)
}
```
will give you:
```
# curl http://localhost:8080/foo
Received "GET" request
# curl -X POST http://localhost:8080/foo
Received "POST" request
# curl http://localhost:8080/bar/baz
Received "baz"
# curl http://localhost:8080/
Default here!
```
[godoc-badge]: https://godoc.org/batou.dev/httprouter?status.svg
[godoc-url]: https://godoc.org/batou.dev/httprouter
[travis-badge]: https://api.travis-ci.org/vbatoufflet/httprouter.svg
[travis-url]: https://travis-ci.org/vbatoufflet/httprouter