Skip to content

Commit

Permalink
Update README and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Kieltyka committed Mar 31, 2016
1 parent 83c2045 commit 93dc859
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 78 deletions.
50 changes: 26 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ scaled very well.

## Features

* Lightweight - cloc`d in ~600 LOC for the chi router
* Fast - yes, see [benchmarks](#benchmarks)
* Expressive routing - middlewares, inline middleware groups/chains, and subrouter mounting
* Request context control (value chaining, deadlines and timeouts) - built on `net/context`
* Robust (tested, used in production)
* **Lightweight** - cloc'd in <1000 LOC for the chi router
* **Fast** - yes, see [benchmarks](#benchmarks)
* **Zero allocations** - no GC pressure during routing
* **Designed for modular/composable APIs** - middlewares, inline middleware groups/chains, and subrouter mounting
* **Context control** - built on `net/context` with value chaining, deadlines and timeouts
* **Robust** - tested / used in production

## Router design

Expand Down Expand Up @@ -122,7 +123,7 @@ func StdHandler(w http.ResponseWriter, r *http.Request) {
```go
// net/context HTTP request handler
func CtxHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
userID := chi.URLParams(ctx)["userID"] // from a route like /users/:userID
userID := chi.URLParam(ctx, "userID") // from a route like /users/:userID
key := ctx.Value("key").(string)
w.Write([]byte(fmt.Sprintf("hi %v, %v", userID, key)))
}
Expand Down Expand Up @@ -200,7 +201,7 @@ func main() {

func ArticleCtx(next chi.Handler) chi.Handler {
return chi.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
articleID := chi.URLParams(ctx)["articleID"]
articleID := chi.URLParam(ctx, "articleID")
article, err := dbGetArticle(articleID)
if err != nil {
http.Error(w, http.StatusText(404), 404)
Expand Down Expand Up @@ -284,11 +285,24 @@ See discussions:

The benchmark suite: https://github.com/pkieltyka/go-http-routing-benchmark

The results as of Nov. 6, 2015 - https://gist.github.com/pkieltyka/505b07b09f5c63e36ef5

Note: by design, chi allocates new routing URLParams map for each request, as opposed
to reusing URLParams from a pool.

```shell
BenchmarkChi_Param 10000000 181 ns/op 0 B/op 0 allocs/op
BenchmarkChi_Param5 3000000 570 ns/op 0 B/op 0 allocs/op
BenchmarkChi_Param20 1000000 2057 ns/op 0 B/op 0 allocs/op
BenchmarkChi_ParamWrite 5000000 245 ns/op 0 B/op 0 allocs/op
BenchmarkChi_GithubStatic 5000000 250 ns/op 0 B/op 0 allocs/op
BenchmarkChi_GithubParam 2000000 589 ns/op 0 B/op 0 allocs/op
BenchmarkChi_GithubAll 10000 102664 ns/op 0 B/op 0 allocs/op
BenchmarkChi_GPlusStatic 10000000 161 ns/op 0 B/op 0 allocs/op
BenchmarkChi_GPlusParam 5000000 291 ns/op 0 B/op 0 allocs/op
BenchmarkChi_GPlus2Params 5000000 393 ns/op 0 B/op 0 allocs/op
BenchmarkChi_GPlusAll 300000 4335 ns/op 0 B/op 0 allocs/op
BenchmarkChi_ParseStatic 10000000 162 ns/op 0 B/op 0 allocs/op
BenchmarkChi_ParseParam 10000000 227 ns/op 0 B/op 0 allocs/op
BenchmarkChi_Parse2Params 5000000 327 ns/op 0 B/op 0 allocs/op
BenchmarkChi_ParseAll 200000 7368 ns/op 0 B/op 0 allocs/op
BenchmarkChi_StaticAll 30000 57990 ns/op 0 B/op 0 allocs/op
```

## Credits

Expand All @@ -298,18 +312,6 @@ to reusing URLParams from a pool.
* Armon Dadgar for https://github.com/armon/go-radix
* Contributions: [@VojtechVitek](https://github.com/VojtechVitek)


## TODO

* Mux options
* Trailing slash?
* Case insensitive paths?
* GET for HEAD requests (auto fallback)?
* Register error handler (500's), ServerError() handler?
* HTTP2 example
* both http 1.1 and http2 automatically.. just turn it on :)
* Regexp support in router "/:id([0-9]+)" or "#id^[0-9]+$" or ..

We'll be more than happy to see [your contributions](./CONTRIBUTING.md)!

## License
Expand Down
6 changes: 3 additions & 3 deletions _examples/rest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"time"

"github.com/pressly/chi"
"github.com/pressly/chi/_examples/rest/render"
"github.com/pressly/chi/middleware"
"github.com/pressly/chi/render"
"golang.org/x/net/context"
)

Expand Down Expand Up @@ -113,7 +113,7 @@ type Article struct {

func ArticleCtx(next chi.Handler) chi.Handler {
return chi.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
articleID := chi.URLParams(ctx)["articleID"]
articleID := chi.URLParam(ctx, "articleID")
article, err := dbGetArticle(articleID)
if err != nil {
http.Error(w, http.StatusText(404), 404)
Expand Down Expand Up @@ -229,7 +229,7 @@ func adminRouter() http.Handler { // or chi.Router {
w.Write([]byte("admin: list accounts.."))
})
r.Get("/users/:userId", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf("admin: view user id %v", chi.URLParams(ctx)["userId"])))
w.Write([]byte(fmt.Sprintf("admin: view user id %v", chi.URLParam(ctx, "userId"))))
})
return r
}
Expand Down
49 changes: 0 additions & 49 deletions _examples/rest/render/render.go

This file was deleted.

2 changes: 1 addition & 1 deletion _examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func createAccount(w http.ResponseWriter, r *http.Request) {
}

func getAccount(ctx context.Context, w http.ResponseWriter, r *http.Request) {
accountID := chi.URLParams(ctx)["accountID"]
accountID := chi.URLParam(ctx, "accountID")
account := ctx.Value("account").(string)
w.Write([]byte(fmt.Sprintf("get account id:%s details:%s", accountID, account)))
}
Expand Down
2 changes: 1 addition & 1 deletion mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestMux(t *testing.T) {
_ = pingAll2

pingOne := func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
idParam := URLParam(ctx, "id") // from outside: chi.URLParams(ctx)
idParam := URLParam(ctx, "id")

w.WriteHeader(200)
w.Write([]byte(fmt.Sprintf("ping one id: %s", idParam)))
Expand Down

0 comments on commit 93dc859

Please sign in to comment.