Documentation ¶
Overview ¶
Package chi is a small, idiomatic and composable router for building HTTP services.
chi requires Go 1.7 or newer.
Example:
package main import ( "net/http" "github.com/pressly/chi" "github.com/pressly/chi/middleware" ) func main() { r := chi.NewRouter() r.Use(middleware.Logger) r.Use(middleware.Recoverer) r.Get("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("root.")) }) http.ListenAndServe(":3333", r) }
See github.com/pressly/chi/_examples/ for more in-depth examples.
Index ¶
- Variables
- func ServerBaseContext(h http.Handler, baseCtx context.Context) http.Handler
- func URLParam(r *http.Request, key string) string
- func URLParamFromCtx(ctx context.Context, key string) string
- type ChainHandler
- type Context
- type Middlewares
- type Mux
- func (mx *Mux) Connect(pattern string, handlerFn http.HandlerFunc)
- func (mx *Mux) Delete(pattern string, handlerFn http.HandlerFunc)
- func (mx *Mux) FileServer(path string, root http.FileSystem)
- func (mx *Mux) Get(pattern string, handlerFn http.HandlerFunc)
- func (mx *Mux) Group(fn func(r Router)) Router
- func (mx *Mux) Handle(pattern string, handler http.Handler)
- func (mx *Mux) HandleFunc(pattern string, handlerFn http.HandlerFunc)
- func (mx *Mux) Head(pattern string, handlerFn http.HandlerFunc)
- func (mx *Mux) MethodNotAllowed(handlerFn http.HandlerFunc)
- func (mx *Mux) MethodNotAllowedHandler() http.HandlerFunc
- func (mx *Mux) Middlewares() Middlewares
- func (mx *Mux) Mount(pattern string, handler http.Handler)
- func (mx *Mux) NotFound(handlerFn http.HandlerFunc)
- func (mx *Mux) NotFoundHandler() http.HandlerFunc
- func (mx *Mux) Options(pattern string, handlerFn http.HandlerFunc)
- func (mx *Mux) Patch(pattern string, handlerFn http.HandlerFunc)
- func (mx *Mux) Post(pattern string, handlerFn http.HandlerFunc)
- func (mx *Mux) Put(pattern string, handlerFn http.HandlerFunc)
- func (mx *Mux) Route(pattern string, fn func(r Router)) Router
- func (mx *Mux) Routes() []Route
- func (mx *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (mx *Mux) Trace(pattern string, handlerFn http.HandlerFunc)
- func (mx *Mux) Use(middlewares ...func(http.Handler) http.Handler)
- func (mx *Mux) With(middlewares ...func(http.Handler) http.Handler) Router
- type Route
- type Router
- type Routes
Constants ¶
This section is empty.
Variables ¶
var (
RouteCtxKey = &contextKey{"RouteContext"}
)
Functions ¶
func ServerBaseContext ¶
ServerBaseContext wraps an http.Handler to set the request context to the `baseCtx`.
Types ¶
type ChainHandler ¶
type ChainHandler struct { Middlewares Middlewares Endpoint http.Handler // contains filtered or unexported fields }
func (*ChainHandler) ServeHTTP ¶
func (c *ChainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type Context ¶
type Context struct { // URL routing parameter key and values. URLParams params // Routing path override used by subrouters. RoutePath string // Routing pattern matching the path. RoutePattern string // Routing patterns throughout the lifecycle of the request, // across all connected routers. RoutePatterns []string }
Context is the default routing context set on the root node of a request context to track URL parameters and an optional routing path.
func NewRouteContext ¶
func NewRouteContext() *Context
NewRouteContext returns a new routing Context object.
func RouteContext ¶ added in v1.0.0
RouteContext returns chi's routing Context object from a http.Request Context.
type Middlewares ¶
Middlewares type is a slice of standard middleware handlers with methods to compose middleware chains and http.Handler's.
func Chain ¶
func Chain(middlewares ...func(http.Handler) http.Handler) Middlewares
Chain returns a Middlewares type from a slice of middleware handlers.
func (Middlewares) Handler ¶
func (mws Middlewares) Handler(h http.Handler) http.Handler
Handler builds and returns a http.Handler from the chain of middlewares, with `h http.Handler` as the final handler.
func (Middlewares) HandlerFunc ¶
func (mws Middlewares) HandlerFunc(h http.HandlerFunc) http.Handler
HandlerFunc builds and returns a http.Handler from the chain of middlewares, with `h http.Handler` as the final handler.
type Mux ¶
type Mux struct {
// contains filtered or unexported fields
}
Mux is a simple HTTP route multiplexer that parses a request path, records any URL params, and executes an end handler. It implements the http.Handler interface and is friendly with the standard library.
Mux is designed to be fast, minimal and offer a powerful API for building modular and composable HTTP services with a large set of handlers. It's particularly useful for writing large REST API services that break a handler into many smaller parts composed of middlewares and end handlers.
func NewMux ¶
func NewMux() *Mux
NewMux returns a newly initialized Mux object that implements the Router interface.
func NewRouter ¶
func NewRouter() *Mux
NewRouter returns a new Mux object that implements the Router interface.
func (*Mux) Connect ¶
func (mx *Mux) Connect(pattern string, handlerFn http.HandlerFunc)
Connect adds the route `pattern` that matches a CONNECT http method to execute the `handlerFn` http.HandlerFunc.
func (*Mux) Delete ¶
func (mx *Mux) Delete(pattern string, handlerFn http.HandlerFunc)
Delete adds the route `pattern` that matches a DELETE http method to execute the `handlerFn` http.HandlerFunc.
func (*Mux) FileServer ¶ added in v1.0.0
func (mx *Mux) FileServer(path string, root http.FileSystem)
FileServer conveniently sets up a http.FileServer handler to serve static files from a http.FileSystem.
func (*Mux) Get ¶
func (mx *Mux) Get(pattern string, handlerFn http.HandlerFunc)
Get adds the route `pattern` that matches a GET http method to execute the `handlerFn` http.HandlerFunc.
func (*Mux) Group ¶
Group creates a new inline-Mux with a fresh middleware stack. It's useful for a group of handlers along the same routing path that use an additional set of middlewares. See _examples/.
func (*Mux) Handle ¶
Handle adds the route `pattern` that matches any http method to execute the `handler` http.Handler.
func (*Mux) HandleFunc ¶
func (mx *Mux) HandleFunc(pattern string, handlerFn http.HandlerFunc)
HandleFunc adds the route `pattern` that matches any http method to execute the `handlerFn` http.HandlerFunc.
func (*Mux) Head ¶
func (mx *Mux) Head(pattern string, handlerFn http.HandlerFunc)
Head adds the route `pattern` that matches a HEAD http method to execute the `handlerFn` http.HandlerFunc.
func (*Mux) MethodNotAllowed ¶
func (mx *Mux) MethodNotAllowed(handlerFn http.HandlerFunc)
MethodNotAllowed sets a custom http.HandlerFunc for routing paths where the method is unresolved. The default handler returns a 405 with an empty body.
func (*Mux) MethodNotAllowedHandler ¶
func (mx *Mux) MethodNotAllowedHandler() http.HandlerFunc
MethodNotAllowedHandler returns the default Mux 405 responder whenever a method cannot be resolved for a route.
func (*Mux) Middlewares ¶
func (mx *Mux) Middlewares() Middlewares
func (*Mux) Mount ¶
Mount attaches another http.Handler or chi Router as a subrouter along a routing path. It's very useful to split up a large API as many independent routers and compose them as a single service using Mount. See _examples/.
Note that Mount() simply sets a wildcard along the `pattern` that will continue routing at the `handler`, which in most cases is another chi.Router. As a result, if you define two Mount() routes on the exact same pattern the mount will panic.
func (*Mux) NotFound ¶
func (mx *Mux) NotFound(handlerFn http.HandlerFunc)
NotFound sets a custom http.HandlerFunc for routing paths that could not be found. The default 404 handler is `http.NotFound`.
func (*Mux) NotFoundHandler ¶
func (mx *Mux) NotFoundHandler() http.HandlerFunc
NotFoundHandler returns the default Mux 404 responder whenever a route cannot be found.
func (*Mux) Options ¶
func (mx *Mux) Options(pattern string, handlerFn http.HandlerFunc)
Options adds the route `pattern` that matches a OPTIONS http method to execute the `handlerFn` http.HandlerFunc.
func (*Mux) Patch ¶
func (mx *Mux) Patch(pattern string, handlerFn http.HandlerFunc)
Patch adds the route `pattern` that matches a PATCH http method to execute the `handlerFn` http.HandlerFunc.
func (*Mux) Post ¶
func (mx *Mux) Post(pattern string, handlerFn http.HandlerFunc)
Post adds the route `pattern` that matches a POST http method to execute the `handlerFn` http.HandlerFunc.
func (*Mux) Put ¶
func (mx *Mux) Put(pattern string, handlerFn http.HandlerFunc)
Put adds the route `pattern` that matches a PUT http method to execute the `handlerFn` http.HandlerFunc.
func (*Mux) Route ¶
Route creates a new Mux with a fresh middleware stack and mounts it along the `pattern` as a subrouter. Effectively, this is a short-hand call to Mount. See _examples/.
func (*Mux) ServeHTTP ¶
func (mx *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP is the single method of the http.Handler interface that makes Mux interoperable with the standard library. It uses a sync.Pool to get and reuse routing contexts for each request.
func (*Mux) Trace ¶
func (mx *Mux) Trace(pattern string, handlerFn http.HandlerFunc)
Trace adds the route `pattern` that matches a TRACE http method to execute the `handlerFn` http.HandlerFunc.
func (*Mux) Use ¶
Use appends a middleware handler to the Mux middleware stack.
The middleware stack for any Mux will execute before searching for a matching route to a specific handler, which provides opportunity to respond early, change the course of the request execution, or set request-scoped values for the next http.Handler.
type Router ¶
type Router interface { http.Handler Routes // Use appends one of more middlewares onto the Router stack. Use(middlewares ...func(http.Handler) http.Handler) // With adds inline middlewares for an endpoint handler. With(middlewares ...func(http.Handler) http.Handler) Router // Group adds a new inline-Router along the current routing // path, with a fresh middleware stack for the inline-Router. Group(fn func(r Router)) Router // Route mounts a sub-Router along a `pattern“ string. Route(pattern string, fn func(r Router)) Router // Mount attaches another http.Handler along ./pattern/* Mount(pattern string, h http.Handler) // Handle and HandleFunc adds routes for `pattern` that matches // all HTTP methods. Handle(pattern string, h http.Handler) HandleFunc(pattern string, h http.HandlerFunc) // HTTP-method routing along `pattern` Connect(pattern string, h http.HandlerFunc) Delete(pattern string, h http.HandlerFunc) Get(pattern string, h http.HandlerFunc) Head(pattern string, h http.HandlerFunc) Options(pattern string, h http.HandlerFunc) Patch(pattern string, h http.HandlerFunc) Post(pattern string, h http.HandlerFunc) Put(pattern string, h http.HandlerFunc) Trace(pattern string, h http.HandlerFunc) // NotFound defines a handler to respond whenever a route could // not be found. NotFound(h http.HandlerFunc) // MethodNotAllowed defines a handler to respond whenever a method is // not allowed. MethodNotAllowed(h http.HandlerFunc) }
Router consisting of the core routing methods used by chi's Mux, using only the standard net/http.
type Routes ¶
type Routes interface { // Routes returns the routing tree in an easily traversable structure. Routes() []Route // Middlewares returns the list of middlewares in use by the router. Middlewares() Middlewares }
Routes interface adds two methods for router traversal, which is also used by the `docgen` subpackage to generation documentation for Routers.
Directories ¶
Path | Synopsis |
---|---|
_examples
|
|
limits
Limits ====== This example demonstrates the use of Timeout, CloseNotify, and Throttle middlewares.
|
Limits ====== This example demonstrates the use of Timeout, CloseNotify, and Throttle middlewares. |
logging
Custom Structured Logger ======================== This example demonstrates how to use middleware.RequestLogger, middleware.LogFormatter and middleware.LogEntry to build a structured logger using the amazing Sirupsen/logrus package as the logging backend.
|
Custom Structured Logger ======================== This example demonstrates how to use middleware.RequestLogger, middleware.LogFormatter and middleware.LogEntry to build a structured logger using the amazing Sirupsen/logrus package as the logging backend. |
rest
REST ==== This example demonstrates a HTTP REST web service with some fixture data.
|
REST ==== This example demonstrates a HTTP REST web service with some fixture data. |
todos-resource
Todos Resource ============== This example demonstrates a project structure that defines a subrouter and its handlers on a struct, and mounting them as subrouters to a parent router.
|
Todos Resource ============== This example demonstrates a project structure that defines a subrouter and its handlers on a struct, and mounting them as subrouters to a parent router. |
versions
Versions ======== This example demonstrates the use of the render subpackage, with a quick concept for how to support multiple api versions.
|
Versions ======== This example demonstrates the use of the render subpackage, with a quick concept for how to support multiple api versions. |