chi
v1.0.3
Documentation
pkg.go.dev ↗Package chi provides a Go+-authored, bounded Chi-compatible registration facade backed by immutable compiled route snapshots.
Variables
RouteCtxKey
var RouteCtxKey = &contextKey{name: "RouteContext"}
Functions
Fold
func Fold[R any](m MatchOutcome, cs MatchOutcomeCases[R]) R
Fold reduces MatchOutcome by one-level case analysis.
RegisterMethod
func RegisterMethod(method string)
URLParam
func URLParam(r *http.Request, key string) string
URLParamFromCtx
func URLParamFromCtx(ctx context.Context, key string) string
Walk
func Walk(r Routes, walkFn WalkFunc) error
Types
ChainHandler
type ChainHandler struct {
Endpoint http.Handler
Middlewares Middlewares
// contains filtered or unexported fields
}
ChainHandler.ServeHTTP
func (handler *ChainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
Conflict
type Conflict struct {
Kind ConflictKind
Method string
Left string
Right string
}
Conflict.Error
func (c Conflict) Error() string
ConflictKind
type ConflictKind uint8
DuplicateRoute (2 declarations)
const ( DuplicateRoute ConflictKind = iota + 1 AmbiguousRoute )
Context
type Context struct {
Routes Routes
RoutePath string
RouteMethod string
URLParams RouteParams
RoutePatterns []string
// contains filtered or unexported fields
}
NewRouteContext
func NewRouteContext() *Context
RouteContext
func RouteContext(ctx context.Context) *Context
Context.Reset
func (x *Context) Reset()
Context.RoutePattern
func (x *Context) RoutePattern() string
Context.URLParam
func (x *Context) URLParam(key string) string
MatchOutcome
type MatchOutcome interface {
// contains filtered or unexported methods
}
MatchOutcome is the exhaustive Go+ semantic lookup result. Compatibility Match remains boolean, while new code must distinguish a route match, a method mismatch, and an absent route without executing a handler.
MatchOutcomeCases
type MatchOutcomeCases[R any] struct {
MatchedRoute func(Route RouteInfo, Params map[string]string) R
MethodMismatch func(Allowed []string) R
RouteMissing func() R
}
MatchOutcomeCases selects one handler per MatchOutcome variant for Fold.
MatchedRoute
type MatchedRoute struct {
Route RouteInfo
Params map[string]string
}
MethodMismatch
type MethodMismatch struct {
Allowed []string
}
Middlewares
type Middlewares []func(http.Handler) http.Handler
Chain
func Chain(middlewares ...func(http.Handler) http.Handler) Middlewares
NewChain
func NewChain(middlewares ...func(http.Handler) http.Handler) Middlewares
Middlewares.Handler
func (middlewares Middlewares) Handler(handler http.Handler) http.Handler
Middlewares.HandlerFunc
func (middlewares Middlewares) HandlerFunc(handler http.HandlerFunc) http.Handler
Mux
type Mux struct {
// contains filtered or unexported fields
}
NewMux
func NewMux() *Mux
NewRouter
func NewRouter() *Mux
Mux.Compile
func (m *Mux) Compile() (*Snapshot, []Conflict)
Mux.Connect
func (m *Mux) Connect(p string, h http.HandlerFunc)
Mux.Delete
func (m *Mux) Delete(p string, h http.HandlerFunc)
Mux.Find
func (m *Mux) Find(ctx *Context, method, path string) string
Mux.Get
func (m *Mux) Get(p string, h http.HandlerFunc)
Mux.Group
func (m *Mux) Group(fn func(Router)) Router
Mux.Handle
func (m *Mux) Handle(pattern string, handler http.Handler)
Mux.HandleFunc
func (m *Mux) HandleFunc(pattern string, handler http.HandlerFunc)
Mux.Head
func (m *Mux) Head(p string, h http.HandlerFunc)
Mux.Match
func (m *Mux) Match(ctx *Context, method, path string) bool
Mux.Method
func (m *Mux) Method(method, pattern string, handler http.Handler)
Mux.MethodFunc
func (m *Mux) MethodFunc(method, pattern string, handler http.HandlerFunc)
Mux.MethodNotAllowed
func (m *Mux) MethodNotAllowed(handler http.HandlerFunc)
Mux.MethodNotAllowedHandler
func (m *Mux) MethodNotAllowedHandler() http.HandlerFunc
MethodNotAllowedHandler returns the configured 405 handler or Chi's default zero-argument responder. Upstream's variadic element type is private, so external consumers can only call the public method without arguments.
Mux.Middlewares
func (m *Mux) Middlewares() Middlewares
Mux.Mount
func (m *Mux) Mount(pattern string, handler http.Handler)
Mux.NotFound
func (m *Mux) NotFound(handler http.HandlerFunc)
Mux.NotFoundHandler
func (m *Mux) NotFoundHandler() http.HandlerFunc
Mux.Options
func (m *Mux) Options(p string, h http.HandlerFunc)
Mux.Patch
func (m *Mux) Patch(p string, h http.HandlerFunc)
Mux.Post
func (m *Mux) Post(p string, h http.HandlerFunc)
Mux.Put
func (m *Mux) Put(p string, h http.HandlerFunc)
Mux.Query
func (m *Mux) Query(p string, h http.HandlerFunc)
Mux.Route
func (m *Mux) Route(pattern string, fn func(Router)) Router
Mux.Routes
func (m *Mux) Routes() []Route
Mux.ServeHTTP
func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)
Mux.Trace
func (m *Mux) Trace(p string, h http.HandlerFunc)
Mux.Typed
func (m *Mux) Typed(method string, pattern stdroute.Pattern, handler stdroute.Handler)
Typed registers the Go+-authored pattern-indexed handler boundary. Go+ statically requires the handler and its parameter keys to share the pattern index; this facade consumes the erased ordinary-Go handler.
Mux.Use
func (m *Mux) Use(middlewares ...func(http.Handler) http.Handler)
Mux.UseCapability
func (m *Mux) UseCapability(middleware stdroute.Middleware)
UseCapability consumes capability-indexed Go+ middleware at the ordinary-Go compatibility boundary.
Mux.With
func (m *Mux) With(middlewares ...func(http.Handler) http.Handler) Router
Route
type Route struct {
SubRoutes Routes
Handlers map[string]http.Handler
Pattern string
}
RouteInfo
type RouteInfo struct {
Method string
Pattern string
ParamNames []string
Handler http.Handler
SubRoutes Routes
}
RouteInfo is immutable flat metadata for documentation and OpenAPI generation without traversing private router nodes.
RouteMissing
type RouteMissing struct{}
RouteParams
type RouteParams struct{ Keys, Values []string }
RouteParams.Add
func (s *RouteParams) Add(key, value string)
Router
type Router interface {
http.Handler
Routes
Use(...func(http.Handler) http.Handler)
With(...func(http.Handler) http.Handler) Router
Group(func(Router)) Router
Route(string, func(Router)) Router
Mount(string, http.Handler)
Handle(string, http.Handler)
HandleFunc(string, http.HandlerFunc)
Method(string, string, http.Handler)
MethodFunc(string, string, http.HandlerFunc)
Connect(string, http.HandlerFunc)
Delete(string, http.HandlerFunc)
Get(string, http.HandlerFunc)
Head(string, http.HandlerFunc)
Options(string, http.HandlerFunc)
Patch(string, http.HandlerFunc)
Post(string, http.HandlerFunc)
Put(string, http.HandlerFunc)
Query(string, http.HandlerFunc)
Trace(string, http.HandlerFunc)
NotFound(http.HandlerFunc)
MethodNotAllowed(http.HandlerFunc)
}
Routes
type Routes interface {
Routes() []Route
Middlewares() Middlewares
Match(*Context, string, string) bool
Find(*Context, string, string) string
}
Snapshot
type Snapshot struct {
// contains filtered or unexported fields
}
Snapshot.Find
func (s *Snapshot) Find(ctx *Context, method, path string) string
Snapshot.Match
func (s *Snapshot) Match(ctx *Context, method, path string) bool
Snapshot.Metadata
func (s *Snapshot) Metadata() []RouteInfo
Snapshot.Middlewares
func (s *Snapshot) Middlewares() Middlewares
Snapshot.Resolve
func (s *Snapshot) Resolve(method, path string) MatchOutcome
Snapshot.Routes
func (s *Snapshot) Routes() []Route
Snapshot.ServeHTTP
func (s *Snapshot) ServeHTTP(w http.ResponseWriter, r *http.Request)
WalkFunc
type WalkFunc func(method, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error