router

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2022 License: MIT Imports: 6 Imported by: 12 modules, 22 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Param

type Param struct {
	Key, Value string
}

Param is a url parameter where key is the url segment pattern (without :) and value is the actual segment of a matched url. e.g. url /foo/123 matches /foo/:id, the url param has key "id" and value "123"

func ParamsFromContext

func ParamsFromContext(ctx context.Context) []Param

ParamsFromContext pulls the URL parameters from a request context, or returns nil if none are present.

type Router

type Router struct {

	// If enabled, the router checks if another method is allowed for the
	// current route, if the current request can not be routed.
	// If this is the case, the request is answered with 'Method Not Allowed'
	// and HTTP status code 405.
	// If no other Method is allowed, the request is delegated to the NotFound
	// handler.
	HandleMethodNotAllowed bool

	// Configurable http.Handler which is called when a request
	// cannot be routed and HandleMethodNotAllowed is true.
	// If it is not set, http.Error with http.StatusMethodNotAllowed is used.
	// The "Allow" header with allowed request methods is set before the handler
	// is called.
	MethodNotAllowed http.Handler

	// Configurable http.Handler which is called when no matching route is
	// found. If it is not set, http.NotFound is used.
	NotFound http.Handler

	// Function to handle panics recovered from http handlers.
	// It should be used to generate a error page and return the http error code
	// 500 (Internal Server Error).
	// The handler can be used to keep your server from crashing because of
	// unrecovered panics.
	PanicHandler func(http.ResponseWriter, *http.Request, interface{})

	// Used for special behavior using which different handlers can configured
	// for paths such as /a and /:b in router.
	WhitelistedPaths []string
	// contains filtered or unexported fields
}

Router dispatches http requests to a registered http.Handler. It implements a similar interface to the one in github.com/julienschmidt/httprouter, the main differences are: 1. this router does not treat "/a/:b" and "/a/b/c" as conflicts (https://github.com/julienschmidt/httprouter/issues/175) 2. this router does not treat "/a/:b" and "/a/:c" as different routes and therefore does not allow them to be registered at the same time (https://github.com/julienschmidt/httprouter/issues/6) 3. this router does not treat "/a" and "/a/" as different routes 4. this router treats "/a" and "/:b" as different paths for whitelisted paths Also the `*` pattern is greedy, if a handler is register for `/a/*`, then no handler can be further registered for any path that starts with `/a/`

func (*Router) Handle

func (r *Router) Handle(method, path string, handler http.Handler) error

Handle registers a http.Handler for given method and path.

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP dispatches the request to a register handler to handle.

type Trie

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

Trie is a radix trie to store string value at given url path, a trie node corresponds to an arbitrary path substring.

func NewTrie

func NewTrie() *Trie

NewTrie creates a new trie.

func (*Trie) Get

func (t *Trie) Get(path string, isWhitelisted bool) (http.Handler, []Param, error)

Get returns the http.Handler for given path, returns error if not found. It also returns the url params if given path contains any, e.g. if a handler is registered for "/:foo/bar", then calling Get with path "/xyz/bar" returns a param whose key is "foo" and value is "xyz". isWhitelisted - Used for special behavior using which different handlers can configured for paths such as /a and /:b in router

func (*Trie) Set

func (t *Trie) Set(path string, value http.Handler, isWhitelisted bool) error

Set sets the value for given path, returns error if path already set. When a http.Handler is registered for a given path, a subsequent Get returns the registered handler if the url passed to Get call matches the set path. Match in this context could mean either equality (e.g. url is "/foo" and path is "/foo") or url matches path pattern, which has two forms: - path ends with "/*", e.g. url "/foo" and "/foo/bar" both matches path "/*" - path contains colon wildcard ("/:"), e.g. url "/a/b" and "/a/c" bot matches path "/a/:var" isWhitelisted - Used for special behavior using which different handlers can configured for paths such as /a and /:b in router

Jump to

Keyboard shortcuts

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