router

package
v0.5.20 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2021 License: BSD-3-Clause, BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const MethodWild = "*"

MethodWild wild HTTP method

Variables

View Source
var (

	// MatchedRoutePathParam is the param name under which the path of the matched
	// route is stored, if Router.SaveMatchedRoutePath is set.
	MatchedRoutePathParam = fmt.Sprintf("__matchedRoutePath::%s__", bytes.Rand(make([]byte, 15)))
)

Functions

This section is empty.

Types

type Group

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

Group is a sub-router to group paths

func (*Group) ANY

func (g *Group) ANY(path string, handler gateway.ProxyHandler)

ANY is a shortcut for group.Handle(router.MethodWild, path, handler)

WARNING: Use only for routes where the request method is not important

func (*Group) CONNECT

func (g *Group) CONNECT(path string, handler gateway.ProxyHandler)

OPTIONS is a shortcut for group.Handle(fasthttp.MethodOptions, path, handler)

func (*Group) DELETE

func (g *Group) DELETE(path string, handler gateway.ProxyHandler)

DELETE is a shortcut for group.Handle(fasthttp.MethodDelete, path, handler)

func (*Group) GET

func (g *Group) GET(path string, handler gateway.ProxyHandler)

GET is a shortcut for group.Handle(fasthttp.MethodGet, path, handler)

func (*Group) Group

func (g *Group) Group(path string) *Group

Group returns a new group. Path auto-correction, including trailing slashes, is enabled by default.

func (*Group) HEAD

func (g *Group) HEAD(path string, handler gateway.ProxyHandler)

HEAD is a shortcut for group.Handle(fasthttp.MethodHead, path, handler)

func (*Group) Handle

func (g *Group) Handle(method, path string, handler gateway.ProxyHandler)

Handle registers a new request handler with the given path and method.

For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used.

This function is intended for bulk loading and to allow the usage of less frequently used, non-standardized or custom methods (e.g. for internal communication with a proxy).

func (*Group) OPTIONS

func (g *Group) OPTIONS(path string, handler gateway.ProxyHandler)

OPTIONS is a shortcut for group.Handle(fasthttp.MethodOptions, path, handler)

func (*Group) PATCH

func (g *Group) PATCH(path string, handler gateway.ProxyHandler)

PATCH is a shortcut for group.Handle(fasthttp.MethodPatch, path, handler)

func (*Group) POST

func (g *Group) POST(path string, handler gateway.ProxyHandler)

POST is a shortcut for group.Handle(fasthttp.MethodPost, path, handler)

func (*Group) PUT

func (g *Group) PUT(path string, handler gateway.ProxyHandler)

PUT is a shortcut for group.Handle(fasthttp.MethodPut, path, handler)

func (*Group) TRACE

func (g *Group) TRACE(path string, handler gateway.ProxyHandler)

OPTIONS is a shortcut for group.Handle(fasthttp.MethodOptions, path, handler)

type Router

type Router struct {

	// If enabled, adds the matched route path onto the ctx.UserValue context
	// before invoking the handler.
	// The matched route path is only added to handlers of routes that were
	// registered when this option was enabled.
	SaveMatchedRoutePath bool

	// Enables automatic redirection if the current route can't be matched but a
	// handler for the path with (without) the trailing slash exists.
	// For example if /foo/ is requested but a route only exists for /foo, the
	// client is redirected to /foo with http status code 301 for GET requests
	// and 308 for all other request methods.
	RedirectTrailingSlash bool

	// If enabled, the router tries to fix the current request path, if no
	// handle is registered for it.
	// First superfluous path elements like ../ or // are removed.
	// Afterwards the router does a case-insensitive lookup of the cleaned path.
	// If a handle can be found for this route, the router makes a redirection
	// to the corrected path with status code 301 for GET requests and 308 for
	// all other request methods.
	// For example /FOO and /..//Foo could be redirected to /foo.
	// RedirectTrailingSlash is independent of this option.
	RedirectFixedPath bool

	// 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

	// If enabled, the router automatically replies to OPTIONS requests.
	// Custom OPTIONS handlers take priority over automatic replies.
	HandleOPTIONS bool

	// An optional gateway.ProxyHandler that is called on automatic OPTIONS requests.
	// The handler is only called if HandleOPTIONS is true and no OPTIONS
	// handler for the specific path was set.
	// The "Allowed" header is set before calling the handler.
	GlobalOPTIONS gateway.ProxyHandler

	// Configurable gateway.ProxyHandler which is called when no matching route is
	// found. If it is not set, default NotFound is used.
	NotFound gateway.ProxyHandler

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

	// 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(*fasthttp.RequestCtx, interface{})
	// contains filtered or unexported fields
}

Router is a gateway.ProxyHandler which can be used to dispatch requests to different handler functions via configurable routes

func New

func New() *Router

New returns a new router. Path auto-correction, including trailing slashes, is enabled by default.

func (*Router) ANY

func (r *Router) ANY(path string, handler gateway.ProxyHandler)

ANY is a shortcut for router.Handle(router.MethodWild, path, handler)

WARNING: Use only for routes where the request method is not important

func (*Router) CONNECT

func (r *Router) CONNECT(path string, handler gateway.ProxyHandler)

CONNECT is a shortcut for router.Handle(fasthttp.MethodConnect, path, handler)

func (*Router) DELETE

func (r *Router) DELETE(path string, handler gateway.ProxyHandler)

DELETE is a shortcut for router.Handle(fasthttp.MethodDelete, path, handler)

func (*Router) GET

func (r *Router) GET(path string, handler gateway.ProxyHandler)

GET is a shortcut for router.Handle(fasthttp.MethodGet, path, handler)

func (*Router) Group

func (r *Router) Group(path string) *Group

Group returns a new group. Path auto-correction, including trailing slashes, is enabled by default.

func (*Router) HEAD

func (r *Router) HEAD(path string, handler gateway.ProxyHandler)

HEAD is a shortcut for router.Handle(fasthttp.MethodHead, path, handler)

func (*Router) Handle

func (r *Router) Handle(method, path string, handler gateway.ProxyHandler)

Handle registers a new request handler with the given path and method.

For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used.

This function is intended for bulk loading and to allow the usage of less frequently used, non-standardized or custom methods (e.g. for internal communication with a proxy).

func (*Router) Handler

func (r *Router) Handler(ctx *fasthttp.RequestCtx, buf *pools.ByteBuffer)

Handler makes the router implement the http.Handler interface.

func (*Router) List

func (r *Router) List() map[string][]string

List returns all registered routes grouped by method

func (*Router) Lookup

func (r *Router) Lookup(ctx *fasthttp.RequestCtx) (gateway.ProxyHandler, bool)

Lookup allows the manual lookup of a method + path combo. This is e.g. useful to build a framework around this router. If the path was found, it returns the handler function and the path parameter values. Otherwise the third return value indicates whether a redirection to the same path with an extra / without the trailing slash should be performed.

func (*Router) Mutable

func (r *Router) Mutable(v bool)

Mutable allows updating the route handler

It's disabled by default

WARNING: Use with care. It could generate unexpected behaviours

func (*Router) OPTIONS

func (r *Router) OPTIONS(path string, handler gateway.ProxyHandler)

OPTIONS is a shortcut for router.Handle(fasthttp.MethodOptions, path, handler)

func (*Router) PATCH

func (r *Router) PATCH(path string, handler gateway.ProxyHandler)

PATCH is a shortcut for router.Handle(fasthttp.MethodPatch, path, handler)

func (*Router) POST

func (r *Router) POST(path string, handler gateway.ProxyHandler)

POST is a shortcut for router.Handle(fasthttp.MethodPost, path, handler)

func (*Router) PUT

func (r *Router) PUT(path string, handler gateway.ProxyHandler)

PUT is a shortcut for router.Handle(fasthttp.MethodPut, path, handler)

func (*Router) TRACE

func (r *Router) TRACE(path string, handler gateway.ProxyHandler)

TRACE is a shortcut for router.Handle(fasthttp.MethodTrace, path, handler)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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