Documentation
¶
Index ¶
- Constants
- Variables
- type Group
- func (g *Group[Req, Resp]) ANY(path string, handler hall.Handler[Req, Resp])
- func (g *Group[Req, Resp]) ANYFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})
- func (g *Group[Req, Resp]) CONNECT(path string, handler hall.Handler[Req, Resp])
- func (g *Group[Req, Resp]) DELETE(path string, handler hall.Handler[Req, Resp])
- func (g *Group[Req, Resp]) DELETEFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})
- func (g *Group[Req, Resp]) GET(path string, handler hall.Handler[Req, Resp])
- func (g *Group[Req, Resp]) GETFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})
- func (g *Group[Req, Resp]) Group(path string) *Group[Req, Resp]
- func (g *Group[Req, Resp]) HEAD(path string, handler hall.Handler[Req, Resp])
- func (g *Group[Req, Resp]) HEADFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})
- func (g *Group[Req, Resp]) Handle(method, path string, handler hall.Handler[Req, Resp])
- func (g *Group[Req, Resp]) OPTIONS(path string, handler hall.Handler[Req, Resp])
- func (g *Group[Req, Resp]) OPTIONSFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})
- func (g *Group[Req, Resp]) PATCH(path string, handler hall.Handler[Req, Resp])
- func (g *Group[Req, Resp]) PATCHFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})
- func (g *Group[Req, Resp]) POST(path string, handler hall.Handler[Req, Resp])
- func (g *Group[Req, Resp]) POSTFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})
- func (g *Group[Req, Resp]) PUT(path string, handler hall.Handler[Req, Resp])
- func (g *Group[Req, Resp]) PUTFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})
- func (g *Group[Req, Resp]) ServeFS(path string, filesystem fs.FS)
- func (g *Group[Req, Resp]) ServeFiles(path string, rootPath string)
- func (g *Group[Req, Resp]) ServeFilesCustom(path string, fs fs.FS)
- func (g *Group[Req, Resp]) TRACE(path string, handler hall.Handler[Req, Resp])
- func (g *Group[Req, Resp]) TRACEFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})
- type Router
- func (r *Router[Req, Resp]) ANY(path string, handler hall.Handler[Req, Resp])
- func (r *Router[Req, Resp]) ANYFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})
- func (r *Router[Req, Resp]) CONNECT(path string, handler hall.Handler[Req, Resp])
- func (r *Router[Req, Resp]) DELETE(path string, handler hall.Handler[Req, Resp])
- func (r *Router[Req, Resp]) DELETEFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})
- func (r *Router[Req, Resp]) GET(path string, handler hall.Handler[Req, Resp])
- func (r *Router[Req, Resp]) GETFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})
- func (r *Router[Req, Resp]) Group(path string) *Group[Req, Resp]
- func (r *Router[Req, Resp]) HEAD(path string, handler hall.Handler[Req, Resp])
- func (r *Router[Req, Resp]) HEADFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})
- func (r *Router[Req, Resp]) Handle(method, path string, handler hall.Handler[Req, Resp])
- func (r *Router[Req, Resp]) List() map[string][]string
- func (r *Router[Req, Resp]) Lookup(method, path string, ctx context.Context) (hall.Handler[Req, Resp], bool)
- func (r *Router[Req, Resp]) Mutable(v bool)
- func (r *Router[Req, Resp]) OPTIONS(path string, handler hall.Handler[Req, Resp])
- func (r *Router[Req, Resp]) OPTIONSFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})
- func (r *Router[Req, Resp]) PATCH(path string, handler hall.Handler[Req, Resp])
- func (r *Router[Req, Resp]) PATCHFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})
- func (r *Router[Req, Resp]) POST(path string, handler hall.Handler[Req, Resp])
- func (r *Router[Req, Resp]) POSTFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})
- func (r *Router[Req, Resp]) PUT(path string, handler hall.Handler[Req, Resp])
- func (r *Router[Req, Resp]) PUTFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})
- func (r *Router[Req, Resp]) ServeFS(path string, filesystem fs.FS)
- func (r *Router[Req, Resp]) ServeFiles(path string, rootPath string)
- func (r *Router[Req, Resp]) ServeFilesCustom(path string, fs fs.FS)
- func (r *Router[Req, Resp]) ServeHTTP(writer http.ResponseWriter, request *http.Request)
- func (r *Router[Req, Resp]) SetDecoder(decoder hall.ReqDecoder[Req])
- func (r *Router[Req, Resp]) SetEncoder(encoder hall.RespEncoder[Resp])
- func (r *Router[Req, Resp]) TRACE(path string, handler hall.Handler[Req, Resp])
- func (r *Router[Req, Resp]) TRACEFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})
- func (r *Router[Req, Resp]) Wrap(handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{}) *hall.FuncHandler[Req, Resp]
Constants ¶
const MethodWild = "*"
MethodWild wild HTTP method
Variables ¶
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 ¶
func (*Group[Req, Resp]) ANY ¶
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 ¶
CONNECT is a shortcut for group.Handle(hall.MethodOptions, path, handler)
func (*Group[Req, Resp]) DELETE ¶
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]) 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 ¶
Group returns a new group. Path autocorrection, including trailing slashes, is enabled by default.
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 ¶
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 ¶
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 ¶
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]) 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]) 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 ¶
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 ¶
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 ¶
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)
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 ¶
NewRouter New returns a new router. Path autocorrection, including trailing slashes, is enabled by default.
func (*Router[Req, Resp]) ANY ¶
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 ¶
CONNECT is a shortcut for router.Handle(hall.MethodConnect, path, handler)
func (*Router[Req, Resp]) DELETE ¶
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]) GETFunc ¶
func (r *Router[Req, Resp]) GETFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})
func (*Router[Req, Resp]) Group ¶
Group returns a new group. Path autocorrection, including trailing slashes, is enabled by default.
func (*Router[Req, Resp]) HEAD ¶
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 ¶
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]) 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 ¶
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 ¶
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 ¶
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 ¶
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]) PUTFunc ¶
func (r *Router[Req, Resp]) PUTFunc(uri string, handlerFunc hall.HandlerFunc[Req, Resp], codecs ...interface{})
func (*Router[Req, Resp]) ServeFS ¶
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 ¶
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 ¶
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 ¶
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]