router

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2025 License: MIT 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__", bytesRand(make([]byte, 15)))
)

Functions

This section is empty.

Types

type Group

type Group[Req any, Resp any] struct {
	// contains filtered or unexported fields
}

func (*Group[Req, Resp]) ANY

func (g *Group[Req, Resp]) ANY(path string, handler hall.Handler[Req, Resp])

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[Req, Resp]) ANYFunc added in v0.0.11

func (g *Group[Req, Resp]) ANYFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})

func (*Group[Req, Resp]) CONNECT

func (g *Group[Req, Resp]) CONNECT(path string, handler hall.Handler[Req, Resp])

CONNECT is a shortcut for group.Handle(hall.MethodOptions, path, handler)

func (*Group[Req, Resp]) DELETE

func (g *Group[Req, Resp]) DELETE(path string, handler hall.Handler[Req, Resp])

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

func (*Group[Req, Resp]) DELETEFunc added in v0.0.7

func (g *Group[Req, Resp]) DELETEFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})

func (*Group[Req, Resp]) GET

func (g *Group[Req, Resp]) GET(path string, handler hall.Handler[Req, Resp])

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

func (*Group[Req, Resp]) GETFunc added in v0.0.7

func (g *Group[Req, Resp]) GETFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})

func (*Group[Req, Resp]) Group

func (g *Group[Req, Resp]) Group(path string) *Group[Req, Resp]

Group returns a new group. Path autocorrection, including trailing slashes, is enabled by default.

func (*Group[Req, Resp]) HEAD

func (g *Group[Req, Resp]) HEAD(path string, handler hall.Handler[Req, Resp])

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

func (*Group[Req, Resp]) HEADFunc added in v0.0.7

func (g *Group[Req, Resp]) HEADFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})

func (*Group[Req, Resp]) Handle

func (g *Group[Req, Resp]) Handle(method, path string, handler hall.Handler[Req, Resp])

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[Req, Resp]) OPTIONS

func (g *Group[Req, Resp]) OPTIONS(path string, handler hall.Handler[Req, Resp])

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

func (*Group[Req, Resp]) OPTIONSFunc added in v0.0.7

func (g *Group[Req, Resp]) OPTIONSFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})

func (*Group[Req, Resp]) PATCH

func (g *Group[Req, Resp]) PATCH(path string, handler hall.Handler[Req, Resp])

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

func (*Group[Req, Resp]) PATCHFunc added in v0.0.7

func (g *Group[Req, Resp]) PATCHFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})

func (*Group[Req, Resp]) POST

func (g *Group[Req, Resp]) POST(path string, handler hall.Handler[Req, Resp])

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

func (*Group[Req, Resp]) POSTFunc added in v0.0.7

func (g *Group[Req, Resp]) POSTFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})

func (*Group[Req, Resp]) PUT

func (g *Group[Req, Resp]) PUT(path string, handler hall.Handler[Req, Resp])

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

func (*Group[Req, Resp]) PUTFunc added in v0.0.7

func (g *Group[Req, Resp]) PUTFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})

func (*Group[Req, Resp]) ServeFS

func (g *Group[Req, Resp]) ServeFS(path string, filesystem fs.FS)

ServeFS serves files from the given file system. The path must end with "/{filepath:*}", files are then served from the local path /defined/root/dir/{filepath:*}. For example if root is "/etc" and {filepath:*} is "passwd", the local file "/etc/passwd" would be served. Internally a httpal.FSHandler is used, therefore http.NotFound is used instead Use:

router.ServeFS("/src/{filepath:*}", myFilesystem)

func (*Group[Req, Resp]) ServeFiles

func (g *Group[Req, Resp]) ServeFiles(path string, rootPath string)

ServeFiles serves files from the given file system root path. The path must end with "/{filepath:*}", files are then served from the local path /defined/root/dir/{filepath:*}. For example if root is "/etc" and {filepath:*} is "passwd", the local file "/etc/passwd" would be served. Internally a httpal.FSHandler is used, therefore http.NotFound is used instead Use:

router.ServeFiles("/src/{filepath:*}", "./")

func (*Group[Req, Resp]) ServeFilesCustom

func (g *Group[Req, Resp]) ServeFilesCustom(path string, fs fs.FS)

ServeFilesCustom serves files from the given file system settings. The path must end with "/{filepath:*}", files are then served from the local path /defined/root/dir/{filepath:*}. For example if root is "/etc" and {filepath:*} is "passwd", the local file "/etc/passwd" would be served. Internally a httpal.FSHandler is used, therefore http.NotFound is used instead of the Router's NotFound handler. Use:

router.ServeFilesCustom("/src/{filepath:*}", *customFS)

func (*Group[Req, Resp]) TRACE

func (g *Group[Req, Resp]) TRACE(path string, handler hall.Handler[Req, Resp])

TRACE is a shortcut for group.Handle(hall.MethodOptions, path, handler)

func (*Group[Req, Resp]) TRACEFunc added in v0.0.7

func (g *Group[Req, Resp]) TRACEFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})

type Router

type Router[Req any, Resp any] 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.
	// Afterward 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 fasthttp.RequestHandler 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 hall.Handler[Req, Resp]

	// Configurable hall.Handler[Req,Resp] which is called when no matching route is
	// found. If it is not set, default NotFound is used.
	NotFound hall.Handler[Req, Resp]

	// Configurable hall.Handler[Req,Resp] 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 hall.Handler[Req, Resp]

	// Function to handle panics recovered from http handlers.
	// It should be used to generate an 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(context.Context, interface{})
	// contains filtered or unexported fields
}

func NewRouter

func NewRouter[Req any, Resp any]() *Router[Req, Resp]

NewRouter New returns a new router. Path autocorrection, including trailing slashes, is enabled by default.

func (*Router[Req, Resp]) ANY

func (r *Router[Req, Resp]) ANY(path string, handler hall.Handler[Req, Resp])

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[Req, Resp]) ANYFunc added in v0.0.11

func (r *Router[Req, Resp]) ANYFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})

func (*Router[Req, Resp]) CONNECT

func (r *Router[Req, Resp]) CONNECT(path string, handler hall.Handler[Req, Resp])

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

func (*Router[Req, Resp]) DELETE

func (r *Router[Req, Resp]) DELETE(path string, handler hall.Handler[Req, Resp])

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

func (*Router[Req, Resp]) DELETEFunc

func (r *Router[Req, Resp]) DELETEFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})

func (*Router[Req, Resp]) GET

func (r *Router[Req, Resp]) GET(path string, handler hall.Handler[Req, Resp])

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

func (*Router[Req, Resp]) GETFunc

func (r *Router[Req, Resp]) GETFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})

func (*Router[Req, Resp]) Group

func (r *Router[Req, Resp]) Group(path string) *Group[Req, Resp]

Group returns a new group. Path autocorrection, including trailing slashes, is enabled by default.

func (*Router[Req, Resp]) HEAD

func (r *Router[Req, Resp]) HEAD(path string, handler hall.Handler[Req, Resp])

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

func (*Router[Req, Resp]) HEADFunc

func (r *Router[Req, Resp]) HEADFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})

func (*Router[Req, Resp]) Handle

func (r *Router[Req, Resp]) Handle(method, path string, handler hall.Handler[Req, Resp])

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[Req, Resp]) List

func (r *Router[Req, Resp]) List() map[string][]string

List returns all registered routes grouped by method

func (*Router[Req, Resp]) Lookup

func (r *Router[Req, Resp]) Lookup(method, path string, ctx context.Context) (hall.Handler[Req, Resp], 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. Otherwise, the second return value indicates whether a redirection to the same path with an extra / without the trailing slash should be performed.

func (*Router[Req, Resp]) Mutable

func (r *Router[Req, Resp]) 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[Req, Resp]) OPTIONS

func (r *Router[Req, Resp]) OPTIONS(path string, handler hall.Handler[Req, Resp])

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

func (*Router[Req, Resp]) OPTIONSFunc

func (r *Router[Req, Resp]) OPTIONSFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})

func (*Router[Req, Resp]) PATCH

func (r *Router[Req, Resp]) PATCH(path string, handler hall.Handler[Req, Resp])

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

func (*Router[Req, Resp]) PATCHFunc

func (r *Router[Req, Resp]) PATCHFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})

func (*Router[Req, Resp]) POST

func (r *Router[Req, Resp]) POST(path string, handler hall.Handler[Req, Resp])

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

func (*Router[Req, Resp]) POSTFunc

func (r *Router[Req, Resp]) POSTFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})

func (*Router[Req, Resp]) PUT

func (r *Router[Req, Resp]) PUT(path string, handler hall.Handler[Req, Resp])

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

func (*Router[Req, Resp]) PUTFunc

func (r *Router[Req, Resp]) PUTFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})

func (*Router[Req, Resp]) ServeFS

func (r *Router[Req, Resp]) ServeFS(path string, filesystem fs.FS)

ServeFS serves files from the given file system. The path must end with "/{filepath:*}", files are then served from the local path /defined/root/dir/{filepath:*}. For example if root is "/etc" and {filepath:*} is "passwd", the local file "/etc/passwd" would be served. Internally a fasthttp.FSHandler is used, therefore fasthttp.NotFound is used instead Use:

router.ServeFS("/src/{filepath:*}", myFilesystem)

func (*Router[Req, Resp]) ServeFiles

func (r *Router[Req, Resp]) ServeFiles(path string, rootPath string)

ServeFiles serves files from the given file system root path. The path must end with "/{filepath:*}", files are then served from the local path /defined/root/dir/{filepath:*}. For example if root is "/etc" and {filepath:*} is "passwd", the local file "/etc/passwd" would be served. Internally a fasthttp.FSHandler is used, therefore fasthttp.NotFound is used instead Use:

router.ServeFiles("/src/{filepath:*}", "./")

func (*Router[Req, Resp]) ServeFilesCustom

func (r *Router[Req, Resp]) ServeFilesCustom(path string, fs fs.FS)

ServeFilesCustom serves files from the given file system settings. The path must end with "/{filepath:*}", files are then served from the local path /defined/root/dir/{filepath:*}. For example if root is "/etc" and {filepath:*} is "passwd", the local file "/etc/passwd" would be served. Internally a fasthttp.FSHandler is used, therefore http.NotFound is used instead of the Router's NotFound handler. Use:

router.ServeFilesCustom("/src/{filepath:*}", *customFS)

func (*Router[Req, Resp]) ServeHTTP

func (r *Router[Req, Resp]) ServeHTTP(writer http.ResponseWriter, request *http.Request)

Handler makes the router implement the http.Handler interface.

func (*Router[Req, Resp]) SetDecoder added in v0.0.8

func (r *Router[Req, Resp]) SetDecoder(decoder hall.ReqDecoder[Req])

func (*Router[Req, Resp]) SetEncoder added in v0.0.8

func (r *Router[Req, Resp]) SetEncoder(encoder hall.RespEncoder[Resp])

func (*Router[Req, Resp]) TRACE

func (r *Router[Req, Resp]) TRACE(path string, handler hall.Handler[Req, Resp])

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

func (*Router[Req, Resp]) TRACEFunc

func (r *Router[Req, Resp]) TRACEFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})

func (*Router[Req, Resp]) Wrap added in v0.0.8

func (r *Router[Req, Resp]) Wrap(handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{}) *hall.FuncHandler[Req, Resp]

Directories

Path Synopsis
util

Jump to

Keyboard shortcuts

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