Blame vendor/github.com/go-chi/chi/README.md

Packit Service 509fd4
# chi
Packit Service 509fd4
Packit Service 509fd4
Packit Service 509fd4
[![GoDoc Widget]][GoDoc] [![Travis Widget]][Travis]
Packit Service 509fd4
Packit Service 509fd4
`chi` is a lightweight, idiomatic and composable router for building Go HTTP services. It's
Packit Service 509fd4
especially good at helping you write large REST API services that are kept maintainable as your
Packit Service 509fd4
project grows and changes. `chi` is built on the new `context` package introduced in Go 1.7 to
Packit Service 509fd4
handle signaling, cancelation and request-scoped values across a handler chain.
Packit Service 509fd4
Packit Service 509fd4
The focus of the project has been to seek out an elegant and comfortable design for writing
Packit Service 509fd4
REST API servers, written during the development of the Pressly API service that powers our
Packit Service 509fd4
public API service, which in turn powers all of our client-side applications.
Packit Service 509fd4
Packit Service 509fd4
The key considerations of chi's design are: project structure, maintainability, standard http
Packit Service 509fd4
handlers (stdlib-only), developer productivity, and deconstructing a large system into many small
Packit Service 509fd4
parts. The core router `github.com/go-chi/chi` is quite small (less than 1000 LOC), but we've also
Packit Service 509fd4
included some useful/optional subpackages: [middleware](/middleware), [render](https://github.com/go-chi/render) and [docgen](https://github.com/go-chi/docgen). We hope you enjoy it too!
Packit Service 509fd4
Packit Service 509fd4
## Install
Packit Service 509fd4
Packit Service 509fd4
`go get -u github.com/go-chi/chi`
Packit Service 509fd4
Packit Service 509fd4
Packit Service 509fd4
## Features
Packit Service 509fd4
Packit Service 509fd4
* **Lightweight** - cloc'd in ~1000 LOC for the chi router
Packit Service 509fd4
* **Fast** - yes, see [benchmarks](#benchmarks)
Packit Service 509fd4
* **100% compatible with net/http** - use any http or middleware pkg in the ecosystem that is also compatible with `net/http`
Packit Service 509fd4
* **Designed for modular/composable APIs** - middlewares, inline middlewares, route groups and subrouter mounting
Packit Service 509fd4
* **Context control** - built on new `context` package, providing value chaining, cancelations and timeouts
Packit Service 509fd4
* **Robust** - in production at Pressly, CloudFlare, Heroku, 99Designs, and many others (see [discussion](https://github.com/go-chi/chi/issues/91))
Packit Service 509fd4
* **Doc generation** - `docgen` auto-generates routing documentation from your source to JSON or Markdown
Packit Service 509fd4
* **No external dependencies** - plain ol' Go stdlib + net/http
Packit Service 509fd4
Packit Service 509fd4
Packit Service 509fd4
## Examples
Packit Service 509fd4
Packit Service 509fd4
See [_examples/](https://github.com/go-chi/chi/blob/master/_examples/) for a variety of examples.
Packit Service 509fd4
Packit Service 509fd4
Packit Service 509fd4
**As easy as:**
Packit Service 509fd4
Packit Service 509fd4
```go
Packit Service 509fd4
package main
Packit Service 509fd4
Packit Service 509fd4
import (
Packit Service 509fd4
	"net/http"
Packit Service 509fd4
	"github.com/go-chi/chi"
Packit Service 509fd4
)
Packit Service 509fd4
Packit Service 509fd4
func main() {
Packit Service 509fd4
	r := chi.NewRouter()
Packit Service 509fd4
	r.Get("/", func(w http.ResponseWriter, r *http.Request) {
Packit Service 509fd4
		w.Write([]byte("welcome"))
Packit Service 509fd4
	})
Packit Service 509fd4
	http.ListenAndServe(":3000", r)
Packit Service 509fd4
}
Packit Service 509fd4
```
Packit Service 509fd4
Packit Service 509fd4
**REST Preview:**
Packit Service 509fd4
Packit Service 509fd4
Here is a little preview of how routing looks like with chi. Also take a look at the generated routing docs
Packit Service 509fd4
in JSON ([routes.json](https://github.com/go-chi/chi/blob/master/_examples/rest/routes.json)) and in
Packit Service 509fd4
Markdown ([routes.md](https://github.com/go-chi/chi/blob/master/_examples/rest/routes.md)).
Packit Service 509fd4
Packit Service 509fd4
I highly recommend reading the source of the [examples](https://github.com/go-chi/chi/blob/master/_examples/) listed
Packit Service 509fd4
above, they will show you all the features of chi and serve as a good form of documentation.
Packit Service 509fd4
Packit Service 509fd4
```go
Packit Service 509fd4
import (
Packit Service 509fd4
  //...
Packit Service 509fd4
  "context"
Packit Service 509fd4
  "github.com/go-chi/chi"
Packit Service 509fd4
  "github.com/go-chi/chi/middleware"
Packit Service 509fd4
)
Packit Service 509fd4
Packit Service 509fd4
func main() {
Packit Service 509fd4
  r := chi.NewRouter()
Packit Service 509fd4
Packit Service 509fd4
  // A good base middleware stack
Packit Service 509fd4
  r.Use(middleware.RequestID)
Packit Service 509fd4
  r.Use(middleware.RealIP)
Packit Service 509fd4
  r.Use(middleware.Logger)
Packit Service 509fd4
  r.Use(middleware.Recoverer)
Packit Service 509fd4
Packit Service 509fd4
  // Set a timeout value on the request context (ctx), that will signal
Packit Service 509fd4
  // through ctx.Done() that the request has timed out and further
Packit Service 509fd4
  // processing should be stopped.
Packit Service 509fd4
  r.Use(middleware.Timeout(60 * time.Second))
Packit Service 509fd4
Packit Service 509fd4
  r.Get("/", func(w http.ResponseWriter, r *http.Request) {
Packit Service 509fd4
    w.Write([]byte("hi"))
Packit Service 509fd4
  })
Packit Service 509fd4
Packit Service 509fd4
  // RESTy routes for "articles" resource
Packit Service 509fd4
  r.Route("/articles", func(r chi.Router) {
Packit Service 509fd4
    r.With(paginate).Get("/", listArticles)                           // GET /articles
Packit Service 509fd4
    r.With(paginate).Get("/{month}-{day}-{year}", listArticlesByDate) // GET /articles/01-16-2017
Packit Service 509fd4
Packit Service 509fd4
    r.Post("/", createArticle)                                        // POST /articles
Packit Service 509fd4
    r.Get("/search", searchArticles)                                  // GET /articles/search
Packit Service 509fd4
Packit Service 509fd4
    // Regexp url parameters:
Packit Service 509fd4
    r.Get("/{articleSlug:[a-z-]+}", getArticleBySlug)                // GET /articles/home-is-toronto
Packit Service 509fd4
Packit Service 509fd4
    // Subrouters:
Packit Service 509fd4
    r.Route("/{articleID}", func(r chi.Router) {
Packit Service 509fd4
      r.Use(ArticleCtx)
Packit Service 509fd4
      r.Get("/", getArticle)                                          // GET /articles/123
Packit Service 509fd4
      r.Put("/", updateArticle)                                       // PUT /articles/123
Packit Service 509fd4
      r.Delete("/", deleteArticle)                                    // DELETE /articles/123
Packit Service 509fd4
    })
Packit Service 509fd4
  })
Packit Service 509fd4
Packit Service 509fd4
  // Mount the admin sub-router
Packit Service 509fd4
  r.Mount("/admin", adminRouter())
Packit Service 509fd4
Packit Service 509fd4
  http.ListenAndServe(":3333", r)
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
func ArticleCtx(next http.Handler) http.Handler {
Packit Service 509fd4
  return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Packit Service 509fd4
    articleID := chi.URLParam(r, "articleID")
Packit Service 509fd4
    article, err := dbGetArticle(articleID)
Packit Service 509fd4
    if err != nil {
Packit Service 509fd4
      http.Error(w, http.StatusText(404), 404)
Packit Service 509fd4
      return
Packit Service 509fd4
    }
Packit Service 509fd4
    ctx := context.WithValue(r.Context(), "article", article)
Packit Service 509fd4
    next.ServeHTTP(w, r.WithContext(ctx))
Packit Service 509fd4
  })
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
func getArticle(w http.ResponseWriter, r *http.Request) {
Packit Service 509fd4
  ctx := r.Context()
Packit Service 509fd4
  article, ok := ctx.Value("article").(*Article)
Packit Service 509fd4
  if !ok {
Packit Service 509fd4
    http.Error(w, http.StatusText(422), 422)
Packit Service 509fd4
    return
Packit Service 509fd4
  }
Packit Service 509fd4
  w.Write([]byte(fmt.Sprintf("title:%s", article.Title)))
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
// A completely separate router for administrator routes
Packit Service 509fd4
func adminRouter() http.Handler {
Packit Service 509fd4
  r := chi.NewRouter()
Packit Service 509fd4
  r.Use(AdminOnly)
Packit Service 509fd4
  r.Get("/", adminIndex)
Packit Service 509fd4
  r.Get("/accounts", adminListAccounts)
Packit Service 509fd4
  return r
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
func AdminOnly(next http.Handler) http.Handler {
Packit Service 509fd4
  return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Packit Service 509fd4
    ctx := r.Context()
Packit Service 509fd4
    perm, ok := ctx.Value("acl.permission").(YourPermissionType)
Packit Service 509fd4
    if !ok || !perm.IsAdmin() {
Packit Service 509fd4
      http.Error(w, http.StatusText(403), 403)
Packit Service 509fd4
      return
Packit Service 509fd4
    }
Packit Service 509fd4
    next.ServeHTTP(w, r)
Packit Service 509fd4
  })
Packit Service 509fd4
}
Packit Service 509fd4
```
Packit Service 509fd4
Packit Service 509fd4
Packit Service 509fd4
## Router design
Packit Service 509fd4
Packit Service 509fd4
chi's router is based on a kind of [Patricia Radix trie](https://en.wikipedia.org/wiki/Radix_tree).
Packit Service 509fd4
The router is fully compatible with `net/http`.
Packit Service 509fd4
Packit Service 509fd4
Built on top of the tree is the `Router` interface:
Packit Service 509fd4
Packit Service 509fd4
```go
Packit Service 509fd4
// Router consisting of the core routing methods used by chi's Mux,
Packit Service 509fd4
// using only the standard net/http.
Packit Service 509fd4
type Router interface {
Packit Service 509fd4
	http.Handler
Packit Service 509fd4
	Routes
Packit Service 509fd4
Packit Service 509fd4
	// Use appends one of more middlewares onto the Router stack.
Packit Service 509fd4
	Use(middlewares ...func(http.Handler) http.Handler)
Packit Service 509fd4
Packit Service 509fd4
	// With adds inline middlewares for an endpoint handler.
Packit Service 509fd4
	With(middlewares ...func(http.Handler) http.Handler) Router
Packit Service 509fd4
Packit Service 509fd4
	// Group adds a new inline-Router along the current routing
Packit Service 509fd4
	// path, with a fresh middleware stack for the inline-Router.
Packit Service 509fd4
	Group(fn func(r Router)) Router
Packit Service 509fd4
Packit Service 509fd4
	// Route mounts a sub-Router along a `pattern`` string.
Packit Service 509fd4
	Route(pattern string, fn func(r Router)) Router
Packit Service 509fd4
Packit Service 509fd4
	// Mount attaches another http.Handler along ./pattern/*
Packit Service 509fd4
	Mount(pattern string, h http.Handler)
Packit Service 509fd4
Packit Service 509fd4
	// Handle and HandleFunc adds routes for `pattern` that matches
Packit Service 509fd4
	// all HTTP methods.
Packit Service 509fd4
	Handle(pattern string, h http.Handler)
Packit Service 509fd4
	HandleFunc(pattern string, h http.HandlerFunc)
Packit Service 509fd4
Packit Service 509fd4
	// Method and MethodFunc adds routes for `pattern` that matches
Packit Service 509fd4
	// the `method` HTTP method.
Packit Service 509fd4
	Method(method, pattern string, h http.Handler)
Packit Service 509fd4
	MethodFunc(method, pattern string, h http.HandlerFunc)
Packit Service 509fd4
Packit Service 509fd4
	// HTTP-method routing along `pattern`
Packit Service 509fd4
	Connect(pattern string, h http.HandlerFunc)
Packit Service 509fd4
	Delete(pattern string, h http.HandlerFunc)
Packit Service 509fd4
	Get(pattern string, h http.HandlerFunc)
Packit Service 509fd4
	Head(pattern string, h http.HandlerFunc)
Packit Service 509fd4
	Options(pattern string, h http.HandlerFunc)
Packit Service 509fd4
	Patch(pattern string, h http.HandlerFunc)
Packit Service 509fd4
	Post(pattern string, h http.HandlerFunc)
Packit Service 509fd4
	Put(pattern string, h http.HandlerFunc)
Packit Service 509fd4
	Trace(pattern string, h http.HandlerFunc)
Packit Service 509fd4
Packit Service 509fd4
	// NotFound defines a handler to respond whenever a route could
Packit Service 509fd4
	// not be found.
Packit Service 509fd4
	NotFound(h http.HandlerFunc)
Packit Service 509fd4
Packit Service 509fd4
	// MethodNotAllowed defines a handler to respond whenever a method is
Packit Service 509fd4
	// not allowed.
Packit Service 509fd4
	MethodNotAllowed(h http.HandlerFunc)
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
// Routes interface adds two methods for router traversal, which is also
Packit Service 509fd4
// used by the github.com/go-chi/docgen package to generate documentation for Routers.
Packit Service 509fd4
type Routes interface {
Packit Service 509fd4
	// Routes returns the routing tree in an easily traversable structure.
Packit Service 509fd4
	Routes() []Route
Packit Service 509fd4
Packit Service 509fd4
	// Middlewares returns the list of middlewares in use by the router.
Packit Service 509fd4
	Middlewares() Middlewares
Packit Service 509fd4
Packit Service 509fd4
	// Match searches the routing tree for a handler that matches
Packit Service 509fd4
	// the method/path - similar to routing a http request, but without
Packit Service 509fd4
	// executing the handler thereafter.
Packit Service 509fd4
	Match(rctx *Context, method, path string) bool
Packit Service 509fd4
}
Packit Service 509fd4
```
Packit Service 509fd4
Packit Service 509fd4
Each routing method accepts a URL `pattern` and chain of `handlers`. The URL pattern
Packit Service 509fd4
supports named params (ie. `/users/{userID}`) and wildcards (ie. `/admin/*`). URL parameters
Packit Service 509fd4
can be fetched at runtime by calling `chi.URLParam(r, "userID")` for named parameters
Packit Service 509fd4
and `chi.URLParam(r, "*")` for a wildcard parameter.
Packit Service 509fd4
Packit Service 509fd4
Packit Service 509fd4
### Middleware handlers
Packit Service 509fd4
Packit Service 509fd4
chi's middlewares are just stdlib net/http middleware handlers. There is nothing special
Packit Service 509fd4
about them, which means the router and all the tooling is designed to be compatible and
Packit Service 509fd4
friendly with any middleware in the community. This offers much better extensibility and reuse
Packit Service 509fd4
of packages and is at the heart of chi's purpose.
Packit Service 509fd4
Packit Service 509fd4
Here is an example of a standard net/http middleware handler using the new request context
Packit Service 509fd4
available in Go. This middleware sets a hypothetical user identifier on the request
Packit Service 509fd4
context and calls the next handler in the chain.
Packit Service 509fd4
Packit Service 509fd4
```go
Packit Service 509fd4
// HTTP middleware setting a value on the request context
Packit Service 509fd4
func MyMiddleware(next http.Handler) http.Handler {
Packit Service 509fd4
  return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Packit Service 509fd4
    ctx := context.WithValue(r.Context(), "user", "123")
Packit Service 509fd4
    next.ServeHTTP(w, r.WithContext(ctx))
Packit Service 509fd4
  })
Packit Service 509fd4
}
Packit Service 509fd4
```
Packit Service 509fd4
Packit Service 509fd4
Packit Service 509fd4
### Request handlers
Packit Service 509fd4
Packit Service 509fd4
chi uses standard net/http request handlers. This little snippet is an example of a http.Handler
Packit Service 509fd4
func that reads a user identifier from the request context - hypothetically, identifying
Packit Service 509fd4
the user sending an authenticated request, validated+set by a previous middleware handler.
Packit Service 509fd4
Packit Service 509fd4
```go
Packit Service 509fd4
// HTTP handler accessing data from the request context.
Packit Service 509fd4
func MyRequestHandler(w http.ResponseWriter, r *http.Request) {
Packit Service 509fd4
  user := r.Context().Value("user").(string)
Packit Service 509fd4
  w.Write([]byte(fmt.Sprintf("hi %s", user)))
Packit Service 509fd4
}
Packit Service 509fd4
```
Packit Service 509fd4
Packit Service 509fd4
Packit Service 509fd4
### URL parameters
Packit Service 509fd4
Packit Service 509fd4
chi's router parses and stores URL parameters right onto the request context. Here is
Packit Service 509fd4
an example of how to access URL params in your net/http handlers. And of course, middlewares
Packit Service 509fd4
are able to access the same information.
Packit Service 509fd4
Packit Service 509fd4
```go
Packit Service 509fd4
// HTTP handler accessing the url routing parameters.
Packit Service 509fd4
func MyRequestHandler(w http.ResponseWriter, r *http.Request) {
Packit Service 509fd4
  userID := chi.URLParam(r, "userID") // from a route like /users/{userID}
Packit Service 509fd4
Packit Service 509fd4
  ctx := r.Context()
Packit Service 509fd4
  key := ctx.Value("key").(string)
Packit Service 509fd4
Packit Service 509fd4
  w.Write([]byte(fmt.Sprintf("hi %v, %v", userID, key)))
Packit Service 509fd4
}
Packit Service 509fd4
```
Packit Service 509fd4
Packit Service 509fd4
Packit Service 509fd4
## Middlewares
Packit Service 509fd4
Packit Service 509fd4
chi comes equipped with an optional `middleware` package, providing a suite of standard
Packit Service 509fd4
`net/http` middlewares. Please note, any middleware in the ecosystem that is also compatible
Packit Service 509fd4
with `net/http` can be used with chi's mux.
Packit Service 509fd4
Packit Service 509fd4
### Core middlewares
Packit Service 509fd4
Packit Service 509fd4
-----------------------------------------------------------------------------------------------------------
Packit Service 509fd4
| chi/middleware Handler | description                                                                     |
Packit Service 509fd4
|:----------------------|:---------------------------------------------------------------------------------
Packit Service 509fd4
| AllowContentType      | Explicit whitelist of accepted request Content-Types                            |
Packit Service 509fd4
| Compress              | Gzip compression for clients that accept compressed responses                   |
Packit Service 509fd4
| GetHead               | Automatically route undefined HEAD requests to GET handlers                     |
Packit Service 509fd4
| Heartbeat             | Monitoring endpoint to check the servers pulse                                  |
Packit Service 509fd4
| Logger                | Logs the start and end of each request with the elapsed processing time         |
Packit Service 509fd4
| NoCache               | Sets response headers to prevent clients from caching                           |
Packit Service 509fd4
| Profiler              | Easily attach net/http/pprof to your routers                                    |
Packit Service 509fd4
| RealIP                | Sets a http.Request's RemoteAddr to either X-Forwarded-For or X-Real-IP         |
Packit Service 509fd4
| Recoverer             | Gracefully absorb panics and prints the stack trace                             |
Packit Service 509fd4
| RequestID             | Injects a request ID into the context of each request                           |
Packit Service 509fd4
| RedirectSlashes       | Redirect slashes on routing paths                                               |
Packit Service 509fd4
| SetHeader             | Short-hand middleware to set a response header key/value                        |
Packit Service 509fd4
| StripSlashes          | Strip slashes on routing paths                                                  |
Packit Service 509fd4
| Throttle              | Puts a ceiling on the number of concurrent requests                             |
Packit Service 509fd4
| Timeout               | Signals to the request context when the timeout deadline is reached             |
Packit Service 509fd4
| URLFormat             | Parse extension from url and put it on request context                          |
Packit Service 509fd4
| WithValue             | Short-hand middleware to set a key/value on the request context                 |
Packit Service 509fd4
-----------------------------------------------------------------------------------------------------------
Packit Service 509fd4
Packit Service 509fd4
### Auxiliary middlewares & packages
Packit Service 509fd4
Packit Service 509fd4
Please see https://github.com/go-chi for additional packages.
Packit Service 509fd4
Packit Service 509fd4
--------------------------------------------------------------------------------------------------------------------
Packit Service 509fd4
| package                                            | description                                                 |
Packit Service 509fd4
|:---------------------------------------------------|:-------------------------------------------------------------
Packit Service 509fd4
| [cors](https://github.com/go-chi/cors)             | Cross-origin resource sharing (CORS)                        |
Packit Service 509fd4
| [docgen](https://github.com/go-chi/docgen)         | Print chi.Router routes at runtime                          |
Packit Service 509fd4
| [jwtauth](https://github.com/go-chi/jwtauth)       | JWT authentication                                          |
Packit Service 509fd4
| [hostrouter](https://github.com/go-chi/hostrouter) | Domain/host based request routing                           |
Packit Service 509fd4
| [httpcoala](https://github.com/go-chi/httpcoala)   | HTTP request coalescer                                      |
Packit Service 509fd4
| [chi-authz](https://github.com/casbin/chi-authz)   | Request ACL via https://github.com/hsluoyz/casbin           |
Packit Service 509fd4
| [phi](https://github.com/fate-lovely/phi)          | Port chi to [fasthttp](https://github.com/valyala/fasthttp) |
Packit Service 509fd4
--------------------------------------------------------------------------------------------------------------------
Packit Service 509fd4
Packit Service 509fd4
please [submit a PR](./CONTRIBUTING.md) if you'd like to include a link to a chi-compatible middleware
Packit Service 509fd4
Packit Service 509fd4
Packit Service 509fd4
## context?
Packit Service 509fd4
Packit Service 509fd4
`context` is a tiny pkg that provides simple interface to signal context across call stacks
Packit Service 509fd4
and goroutines. It was originally written by [Sameer Ajmani](https://github.com/Sajmani)
Packit Service 509fd4
and is available in stdlib since go1.7.
Packit Service 509fd4
Packit Service 509fd4
Learn more at https://blog.golang.org/context
Packit Service 509fd4
Packit Service 509fd4
and..
Packit Service 509fd4
* Docs: https://golang.org/pkg/context
Packit Service 509fd4
* Source: https://github.com/golang/go/tree/master/src/context
Packit Service 509fd4
Packit Service 509fd4
Packit Service 509fd4
## Benchmarks
Packit Service 509fd4
Packit Service 509fd4
The benchmark suite: https://github.com/pkieltyka/go-http-routing-benchmark
Packit Service 509fd4
Packit Service 509fd4
Results as of Jan 9, 2019 with Go 1.11.4 on Linux X1 Carbon laptop
Packit Service 509fd4
Packit Service 509fd4
```shell
Packit Service 509fd4
BenchmarkChi_Param            3000000         475 ns/op       432 B/op      3 allocs/op
Packit Service 509fd4
BenchmarkChi_Param5           2000000         696 ns/op       432 B/op      3 allocs/op
Packit Service 509fd4
BenchmarkChi_Param20          1000000        1275 ns/op       432 B/op      3 allocs/op
Packit Service 509fd4
BenchmarkChi_ParamWrite       3000000         505 ns/op       432 B/op      3 allocs/op
Packit Service 509fd4
BenchmarkChi_GithubStatic     3000000         508 ns/op       432 B/op      3 allocs/op
Packit Service 509fd4
BenchmarkChi_GithubParam      2000000         669 ns/op       432 B/op      3 allocs/op
Packit Service 509fd4
BenchmarkChi_GithubAll          10000      134627 ns/op     87699 B/op    609 allocs/op
Packit Service 509fd4
BenchmarkChi_GPlusStatic      3000000         402 ns/op       432 B/op      3 allocs/op
Packit Service 509fd4
BenchmarkChi_GPlusParam       3000000         500 ns/op       432 B/op      3 allocs/op
Packit Service 509fd4
BenchmarkChi_GPlus2Params     3000000         586 ns/op       432 B/op      3 allocs/op
Packit Service 509fd4
BenchmarkChi_GPlusAll          200000        7237 ns/op      5616 B/op     39 allocs/op
Packit Service 509fd4
BenchmarkChi_ParseStatic      3000000         408 ns/op       432 B/op      3 allocs/op
Packit Service 509fd4
BenchmarkChi_ParseParam       3000000         488 ns/op       432 B/op      3 allocs/op
Packit Service 509fd4
BenchmarkChi_Parse2Params     3000000         551 ns/op       432 B/op      3 allocs/op
Packit Service 509fd4
BenchmarkChi_ParseAll          100000       13508 ns/op     11232 B/op     78 allocs/op
Packit Service 509fd4
BenchmarkChi_StaticAll          20000       81933 ns/op     67826 B/op    471 allocs/op
Packit Service 509fd4
```
Packit Service 509fd4
Packit Service 509fd4
Comparison with other routers: https://gist.github.com/pkieltyka/123032f12052520aaccab752bd3e78cc
Packit Service 509fd4
Packit Service 509fd4
NOTE: the allocs in the benchmark above are from the calls to http.Request's
Packit Service 509fd4
`WithContext(context.Context)` method that clones the http.Request, sets the `Context()`
Packit Service 509fd4
on the duplicated (alloc'd) request and returns it the new request object. This is just
Packit Service 509fd4
how setting context on a request in Go works.
Packit Service 509fd4
Packit Service 509fd4
Packit Service 509fd4
## Credits
Packit Service 509fd4
Packit Service 509fd4
* Carl Jackson for https://github.com/zenazn/goji
Packit Service 509fd4
  * Parts of chi's thinking comes from goji, and chi's middleware package
Packit Service 509fd4
    sources from goji.
Packit Service 509fd4
* Armon Dadgar for https://github.com/armon/go-radix
Packit Service 509fd4
* Contributions: [@VojtechVitek](https://github.com/VojtechVitek)
Packit Service 509fd4
Packit Service 509fd4
We'll be more than happy to see [your contributions](./CONTRIBUTING.md)!
Packit Service 509fd4
Packit Service 509fd4
Packit Service 509fd4
## Beyond REST
Packit Service 509fd4
Packit Service 509fd4
chi is just a http router that lets you decompose request handling into many smaller layers.
Packit Service 509fd4
Many companies including Pressly.com (of course) use chi to write REST services for their public
Packit Service 509fd4
APIs. But, REST is just a convention for managing state via HTTP, and there's a lot of other pieces
Packit Service 509fd4
required to write a complete client-server system or network of microservices.
Packit Service 509fd4
Packit Service 509fd4
Looking ahead beyond REST, I also recommend some newer works in the field coming from
Packit Service 509fd4
[gRPC](https://github.com/grpc/grpc-go), [NATS](https://nats.io), [go-kit](https://github.com/go-kit/kit)
Packit Service 509fd4
and even [graphql](https://github.com/graphql-go/graphql). They're all pretty cool with their
Packit Service 509fd4
own unique approaches and benefits. Specifically, I'd look at gRPC since it makes client-server
Packit Service 509fd4
communication feel like a single program on a single computer, no need to hand-write a client library
Packit Service 509fd4
and the request/response payloads are typed contracts. NATS is pretty amazing too as a super
Packit Service 509fd4
fast and lightweight pub-sub transport that can speak protobufs, with nice service discovery -
Packit Service 509fd4
an excellent combination with gRPC.
Packit Service 509fd4
Packit Service 509fd4
Packit Service 509fd4
## License
Packit Service 509fd4
Packit Service 509fd4
Copyright (c) 2015-present [Peter Kieltyka](https://github.com/pkieltyka)
Packit Service 509fd4
Packit Service 509fd4
Licensed under [MIT License](./LICENSE)
Packit Service 509fd4
Packit Service 509fd4
[GoDoc]: https://godoc.org/github.com/go-chi/chi
Packit Service 509fd4
[GoDoc Widget]: https://godoc.org/github.com/go-chi/chi?status.svg
Packit Service 509fd4
[Travis]: https://travis-ci.org/go-chi/chi
Packit Service 509fd4
[Travis Widget]: https://travis-ci.org/go-chi/chi.svg?branch=master