router

package
v0.7.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2024 License: MPL-2.0, MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// RouteCtxKey is the context.Context key to store the request context.
	RouteCtxKey = &contextKey{"RouteContext"}
)

Functions

func AllURLParams added in v0.7.1

func AllURLParams(ctx *fasthttp.RequestCtx) map[string]string

AllURLParams returns the map of the url parameters from a fasthttp.Request object.

func RegisterMethod added in v0.7.1

func RegisterMethod(method string) error

RegisterMethod adds support for custom HTTP method handlers, available via Router#Method and Router#MethodFunc

func URLParam added in v0.7.1

func URLParam(ctx *fasthttp.RequestCtx, key string) string

URLParam returns the url parameter from a fasthttp.Request object.

func Walk added in v0.7.1

func Walk(r Routes, walkFn WalkFunc) error

Walk walks any router tree that implements Routes interface.

Types

type Context added in v0.7.1

type Context struct {
	Routes Routes

	// Routing path/method override used during the route search.
	// See Mux#routeHTTP method.
	RoutePath   string
	RouteMethod string

	// URLParams are the stack of routeParams captured during the
	// routing lifecycle across a stack of sub-routers.
	URLParams RouteParams

	// Routing pattern stack throughout the lifecycle of the request,
	// across all connected routers. It is a record of all matching
	// patterns across a stack of sub-routers.
	RoutePatterns []string
	// contains filtered or unexported fields
}

Context is the default routing context set on the root node of a request context to track route patterns, URL parameters and an optional routing path.

func NewRouteContext added in v0.7.1

func NewRouteContext() *Context

NewRouteContext returns a new routing Context object.

func RouteContext added in v0.7.1

func RouteContext(ctx *fasthttp.RequestCtx) *Context

RouteContext returns chi's routing Context object from a http.Request Context.

func (*Context) Reset added in v0.7.1

func (x *Context) Reset()

Reset a routing context to its initial state.

func (*Context) RoutePattern added in v0.7.1

func (x *Context) RoutePattern() string

RoutePattern builds the routing pattern string for the particular request, at the particular point during routing. This means, the value will change throughout the execution of a request in a router. That is why its advised to only use this value after calling the next handler.

For example,

func Instrument(next web.Handler) web.Handler {
	return web.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		next.ServeHTTP(w, r)
		routePattern := chi.RouteContext(r.Context()).RoutePattern()
		measure(w, r, routePattern)
	})
}

func (*Context) URLParam added in v0.7.1

func (x *Context) URLParam(key string) string

URLParam returns the corresponding URL parameter value from the request routing context.

type Handler added in v0.7.1

type Handler func(ctx *fasthttp.RequestCtx) error

A Handler is a type that handles an http request within our own little mini framework.

type Mux added in v0.7.1

type Mux struct {
	// contains filtered or unexported fields
}

Mux is a simple fastHTTP route multiplexer that parses a request path, records any URL params, and searched for the appropriate web.Handler. It implements the web.Handler interface and is friendly with the standard library.

func NewMux added in v0.7.1

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) AddEndpoint added in v0.7.1

func (mx *Mux) AddEndpoint(method, pattern string, handler Handler) error

AddEndpoint adds the route `pattern` that matches `method` http method to execute the `handler` web.Handler.

func (*Mux) Find added in v0.7.1

func (mx *Mux) Find(rctx *Context, method, path string) Handler

func (*Mux) Routes added in v0.7.1

func (mx *Mux) Routes() []Route

Routes returns a slice of routing information from the tree, useful for traversing available routes of a router.

type Route

type Route struct {
	SubRoutes Routes
	Handlers  map[string]Handler
	Pattern   string
}

Route describes the details of a routing handler. Handlers map key is an HTTP method

type RouteParams added in v0.7.1

type RouteParams struct {
	Keys, Values []string
}

RouteParams is a structure to track URL routing parameters efficiently.

func (*RouteParams) Add added in v0.7.1

func (s *RouteParams) Add(key, value string)

Add will append a URL parameter to the end of the route param

type Router

type Router interface {
	Routes

	// AddEndpoint adds routes for `pattern` that matches
	// the `method` HTTP method.
	AddEndpoint(method, pattern string, handler Handler) error
}

Router consisting of the core routing methods used by chi's Mux, using only the standard net/http.

type Routes added in v0.7.1

type Routes interface {
	// Routes returns the routing tree in an easily traversable structure.
	Routes() []Route

	// Find searches the routing tree for a handler that matches
	// the method/path - similar to routing a http request, but without
	// executing the handler thereafter.
	Find(rctx *Context, method, path string) Handler
}

Routes interface adds two methods for router traversal, which is also used by the `docgen` subpackage to generation documentation for Routers.

type WalkFunc added in v0.7.1

type WalkFunc func(method string, route string, handler Handler, middlewares ...func(Handler) Handler) error

WalkFunc is the type of the function called for each method and route visited by Walk.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL