Documentation ¶
Overview ¶
Package router is a trie based high performance HTTP request router.
A trivial example is:
package main import ( "fmt" "log" "github.com/fasthttp/router" ) func Index(ctx *fasthttp.RequestCtx) { fmt.Fprint(w, "Welcome!\n") } func Hello(ctx *fasthttp.RequestCtx) { fmt.Fprintf(w, "hello, %s!\n", ctx.UserValue("name")) } func main() { r := router.New() r.GET("/", Index) r.GET("/hello/{name}", Hello) log.Fatal(fasthttp.ListenAndServe(":8080", r.Handler)) }
The router matches incoming requests by the request method and the path. If a handler is registered for this path and method, the router delegates the request to that function. For the methods GET, POST, PUT, PATCH, DELETE and OPTIONS shortcut functions exist to register handles, for all other methods router.Handle can be used.
The registered path, against which the router matches incoming requests, can contain two types of parameters:
Syntax Type {name} named parameter {name:*} catch-all parameter
Named parameters are dynamic path segments. They match anything until the next '/' or the path end:
Path: /blog/{category}/{post} Requests: /blog/go/request-routers match: category="go", post="request-routers" /blog/go/request-routers/ no match, but the router would redirect /blog/go/ no match /blog/go/request-routers/comments no match
Catch-all parameters match anything until the path end, including the directory index (the '/' before the catch-all). Since they match anything until the end, catch-all parameters must always be the final path element.
Path: /files/{filepath:*} Requests: /files/ match: filepath="/" /files/LICENSE match: filepath="/LICENSE" /files/templates/article.html match: filepath="/templates/article.html" /files no match, but the router would redirect
The value of parameters is saved in ctx.UserValue(<key>), consisting each of a key and a value. The slice is passed to the Handle func as a third parameter. To retrieve the value of a parameter,gets by the name of the parameter
user := ctx.UserValue("user") // defined by {user} or {user:*}
Index ¶
- Constants
- Variables
- type Group
- func (g *Group) ANY(path string, handler fasthttp.RequestHandler)
- func (g *Group) CONNECT(path string, handler fasthttp.RequestHandler)
- func (g *Group) DELETE(path string, handler fasthttp.RequestHandler)
- func (g *Group) GET(path string, handler fasthttp.RequestHandler)
- func (g *Group) Group(path string) *Group
- func (g *Group) HEAD(path string, handler fasthttp.RequestHandler)
- func (g *Group) Handle(method, path string, handler fasthttp.RequestHandler)
- func (g *Group) OPTIONS(path string, handler fasthttp.RequestHandler)
- func (g *Group) PATCH(path string, handler fasthttp.RequestHandler)
- func (g *Group) POST(path string, handler fasthttp.RequestHandler)
- func (g *Group) PUT(path string, handler fasthttp.RequestHandler)
- func (g *Group) ServeFiles(path string, rootPath string)
- func (g *Group) ServeFilesCustom(path string, fs *fasthttp.FS)
- func (g *Group) TRACE(path string, handler fasthttp.RequestHandler)
- type Router
- func (r *Router) ANY(path string, handler fasthttp.RequestHandler)
- func (r *Router) CONNECT(path string, handler fasthttp.RequestHandler)
- func (r *Router) DELETE(path string, handler fasthttp.RequestHandler)
- func (r *Router) GET(path string, handler fasthttp.RequestHandler)
- func (r *Router) Group(path string) *Group
- func (r *Router) HEAD(path string, handler fasthttp.RequestHandler)
- func (r *Router) Handle(method, path string, handler fasthttp.RequestHandler)
- func (r *Router) Handler(ctx *fasthttp.RequestCtx)
- func (r *Router) List() map[string][]string
- func (r *Router) Lookup(method, path string, ctx *fasthttp.RequestCtx) (fasthttp.RequestHandler, bool)
- func (r *Router) Mutable(v bool)
- func (r *Router) OPTIONS(path string, handler fasthttp.RequestHandler)
- func (r *Router) PATCH(path string, handler fasthttp.RequestHandler)
- func (r *Router) POST(path string, handler fasthttp.RequestHandler)
- func (r *Router) PUT(path string, handler fasthttp.RequestHandler)
- func (r *Router) ServeFiles(path string, rootPath string)
- func (r *Router) ServeFilesCustom(path string, fs *fasthttp.FS)
- func (r *Router) TRACE(path string, handler fasthttp.RequestHandler)
Constants ¶
const MethodWild = "*"
MethodWild wild HTTP method
Variables ¶
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 fasthttp.RequestHandler)
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 fasthttp.RequestHandler)
OPTIONS is a shortcut for group.Handle(fasthttp.MethodOptions, path, handler)
func (*Group) DELETE ¶
func (g *Group) DELETE(path string, handler fasthttp.RequestHandler)
DELETE is a shortcut for group.Handle(fasthttp.MethodDelete, path, handler)
func (*Group) GET ¶
func (g *Group) GET(path string, handler fasthttp.RequestHandler)
GET is a shortcut for group.Handle(fasthttp.MethodGet, path, handler)
func (*Group) 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 fasthttp.RequestHandler)
HEAD is a shortcut for group.Handle(fasthttp.MethodHead, path, handler)
func (*Group) Handle ¶
func (g *Group) Handle(method, path string, handler fasthttp.RequestHandler)
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 fasthttp.RequestHandler)
OPTIONS is a shortcut for group.Handle(fasthttp.MethodOptions, path, handler)
func (*Group) PATCH ¶
func (g *Group) PATCH(path string, handler fasthttp.RequestHandler)
PATCH is a shortcut for group.Handle(fasthttp.MethodPatch, path, handler)
func (*Group) POST ¶
func (g *Group) POST(path string, handler fasthttp.RequestHandler)
POST is a shortcut for group.Handle(fasthttp.MethodPost, path, handler)
func (*Group) PUT ¶
func (g *Group) PUT(path string, handler fasthttp.RequestHandler)
PUT is a shortcut for group.Handle(fasthttp.MethodPut, path, handler)
func (*Group) ServeFiles ¶
ServeFiles serves files from the given file system root. 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 Use:
router.ServeFiles("/src/{filepath:*}", "./")
func (*Group) 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)
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 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 fasthttp.RequestHandler // Configurable fasthttp.RequestHandler which is called when no matching route is // found. If it is not set, default NotFound is used. NotFound fasthttp.RequestHandler DefaultFlow string TracingFlow string // Configurable fasthttp.RequestHandler 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 fasthttp.RequestHandler // 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{}) TraceHandler fasthttp.RequestHandler // contains filtered or unexported fields }
Router is a fasthttp.RequestHandler 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 fasthttp.RequestHandler)
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 fasthttp.RequestHandler)
CONNECT is a shortcut for router.Handle(fasthttp.MethodConnect, path, handler)
func (*Router) DELETE ¶
func (r *Router) DELETE(path string, handler fasthttp.RequestHandler)
DELETE is a shortcut for router.Handle(fasthttp.MethodDelete, path, handler)
func (*Router) GET ¶
func (r *Router) GET(path string, handler fasthttp.RequestHandler)
GET is a shortcut for router.Handle(fasthttp.MethodGet, path, handler)
func (*Router) 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 fasthttp.RequestHandler)
HEAD is a shortcut for router.Handle(fasthttp.MethodHead, path, handler)
func (*Router) Handle ¶
func (r *Router) Handle(method, path string, handler fasthttp.RequestHandler)
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)
Handler makes the router implement the http.Handler interface.
func (*Router) Lookup ¶
func (r *Router) Lookup(method, path string, ctx *fasthttp.RequestCtx) (fasthttp.RequestHandler, 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 ¶
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 fasthttp.RequestHandler)
OPTIONS is a shortcut for router.Handle(fasthttp.MethodOptions, path, handler)
func (*Router) PATCH ¶
func (r *Router) PATCH(path string, handler fasthttp.RequestHandler)
PATCH is a shortcut for router.Handle(fasthttp.MethodPatch, path, handler)
func (*Router) POST ¶
func (r *Router) POST(path string, handler fasthttp.RequestHandler)
POST is a shortcut for router.Handle(fasthttp.MethodPost, path, handler)
func (*Router) PUT ¶
func (r *Router) PUT(path string, handler fasthttp.RequestHandler)
PUT is a shortcut for router.Handle(fasthttp.MethodPut, path, handler)
func (*Router) ServeFiles ¶
ServeFiles serves files from the given file system root. 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) 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)