chi
v1.0.0
Documentation
pkg.go.dev ↗Functions
URLParam
func URLParam(ctx context.Context, key string) string
URLParam returns a url paramter from the routing context.
Types
Context
type Context struct {
context.Context
// URL parameter key and values
Params params
// Routing path override used by subrouters
RoutePath string
}
A Context is the default routing context set on the root node of a request context to track URL parameters and an optional routing path.
NewContext
func NewContext() *Context
NewContext returns a new routing context object.
RouteContext
func RouteContext(ctx context.Context) *Context
RouteContext returns chi's routing context object that holds url params and a routing path for subrouters.
Handler
type Handler interface {
ServeHTTPC(context.Context, http.ResponseWriter, *http.Request)
}
Handler is like net/http's http.Handler, but also includes a mechanism for serving requests with a context.
HandlerFunc
type HandlerFunc func(context.Context, http.ResponseWriter, *http.Request)
HandlerFunc is like net/http's http.HandlerFunc, but supports a context object.
HandlerFunc.ServeHTTP
func (h HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP provides compatibility with http.Handler.
HandlerFunc.ServeHTTPC
func (h HandlerFunc) ServeHTTPC(ctx context.Context, w http.ResponseWriter, r *http.Request)
ServeHTTPC wraps ServeHTTP with a context parameter.
Mux
type Mux struct {
// contains filtered or unexported fields
}
A 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 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.
NewMux
func NewMux() *Mux
NewMux returns a new Mux object with an optional parent context.
NewRouter
func NewRouter() *Mux
NewRouter returns a new Mux object that implements the Router interface. It accepts an optional parent context.Context argument used by all request contexts useful for signaling a server shutdown.
Mux.Connect
func (mx *Mux) Connect(pattern string, handlers ...interface{})
Connect adds a route that matches a CONNECT http method and the `pattern` for the `handlers` chain.
Mux.Delete
func (mx *Mux) Delete(pattern string, handlers ...interface{})
Delete adds a route that matches a DELETE http method and the `pattern` for the `handlers` chain.
Mux.FileServer
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.
Mux.Get
func (mx *Mux) Get(pattern string, handlers ...interface{})
Get adds a route that matches a GET http method and the `pattern` for the `handlers` chain.
Mux.Group
func (mx *Mux) Group(fn func(r Router)) Router
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 the same middleware(s). See _examples/ for an example usage.
Mux.Handle
func (mx *Mux) Handle(pattern string, handlers ...interface{})
Handle adds a route for all http methods that match the `pattern` for the `handlers` chain.
Mux.Head
func (mx *Mux) Head(pattern string, handlers ...interface{})
Head adds a route that matches a HEAD http method and the `pattern` for the `handlers` chain.
Mux.Mount
func (mx *Mux) Mount(path string, handlers ...interface{})
Mount attaches another mux 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/ for example usage.
Mux.NotFound
func (mx *Mux) NotFound(h HandlerFunc)
NotFound sets a custom http.HandlerFunc for missing routes on the treeRouter.
Mux.Options
func (mx *Mux) Options(pattern string, handlers ...interface{})
Options adds a route that matches a OPTIONS http method and the `pattern` for the `handlers` chain.
Mux.Patch
func (mx *Mux) Patch(pattern string, handlers ...interface{})
Patch adds a route that matches a PATCH http method and the `pattern` for the `handlers` chain.
Mux.Post
func (mx *Mux) Post(pattern string, handlers ...interface{})
Post adds a route that matches a POST http method and the `pattern` for the `handlers` chain.
Mux.Put
func (mx *Mux) Put(pattern string, handlers ...interface{})
Put adds a route that matches a PUT http method and the `pattern` for the `handlers` chain.
Mux.Route
func (mx *Mux) Route(pattern string, fn func(r Router)) Router
Route creates a new Mux with a fresh middleware stack and mounts it along the `pattern`. This is very simiular to the Group, but attaches the group along a new routing path. See _examples/ for example usage.
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.
Mux.ServeHTTPC
func (mx *Mux) ServeHTTPC(ctx context.Context, w http.ResponseWriter, r *http.Request)
ServeHTTPC is chi's Handler method that adds a context.Context argument to the standard ServeHTTP handler function.
Mux.Trace
func (mx *Mux) Trace(pattern string, handlers ...interface{})
Trace adds a route that matches a TRACE http method and the `pattern` for the `handlers` chain.
Mux.Use
func (mx *Mux) Use(mws ...interface{})
Use appends a middleware handler to the Mux middleware stack.
Router
type Router interface {
http.Handler
Handler
Use(middlewares ...interface{})
Group(fn func(r Router)) Router
Route(pattern string, fn func(r Router)) Router
Mount(pattern string, handlers ...interface{})
Handle(pattern string, handlers ...interface{})
NotFound(h HandlerFunc)
Connect(pattern string, handlers ...interface{})
Head(pattern string, handlers ...interface{})
Get(pattern string, handlers ...interface{})
Post(pattern string, handlers ...interface{})
Put(pattern string, handlers ...interface{})
Patch(pattern string, handlers ...interface{})
Delete(pattern string, handlers ...interface{})
Trace(pattern string, handlers ...interface{})
Options(pattern string, handlers ...interface{})
}
A Router consisting of the core routing methods used by chi's Mux.
NOTE, the plan: hopefully once net/context makes it into the stdlib and net/http supports a request context, we will remove the chi.Handler interface, and the Router argument types will be http.Handler instead of interface{}.
WalkFn
type WalkFn func(path string, handler Handler) bool
WalkFn is used when walking the tree. Takes a key and value, returning if iteration should be terminated.