README

Router

Build Status Coverage Status Go Report Card GoDev GitHub release

Router is a lightweight high performance HTTP request router (also called multiplexer or just mux for short) for fasthttp.

This router is optimized for high performance and a small memory footprint. It scales well even with very long paths and a large number of routes. A compressing dynamic trie (radix tree) structure is used for efficient matching.

Based on julienschmidt/httprouter.

Features

Best Performance: Router is one of the fastest go web frameworks in the go-web-framework-benchmark. Even faster than httprouter itself.

  • Basic Test: The first test case is to mock 0 ms, 10 ms, 100 ms, 500 ms processing time in handlers.

  • Concurrency Test (allocations): In 30 ms processing time, the test result for 100, 1000, 5000 clients is:

* Smaller is better

See below for technical details of the implementation.

Only explicit matches: With other routers, like http.ServeMux, a requested URL path could match multiple patterns. Therefore they have some awkward pattern priority rules, like longest match or first registered, first matched. By design of this router, a request can only match exactly one or no route. As a result, there are also no unintended matches, which makes it great for SEO and improves the user experience.

Stop caring about trailing slashes: Choose the URL style you like, the router automatically redirects the client if a trailing slash is missing or if there is one extra. Of course it only does so, if the new path has a handler. If you don't like it, you can turn off this behavior.

Path auto-correction: Besides detecting the missing or additional trailing slash at no extra cost, the router can also fix wrong cases and remove superfluous path elements (like ../ or //). Is CAPTAIN CAPS LOCK one of your users? Router can help him by making a case-insensitive look-up and redirecting him to the correct URL.

Parameters in your routing pattern: Stop parsing the requested URL path, just give the path segment a name and the router delivers the dynamic value to you. Because of the design of the router, path parameters are very cheap.

Zero Garbage: The matching and dispatching process generates zero bytes of garbage. In fact, the only heap allocations that are made, is by building the slice of the key-value pairs for path parameters. If the request path contains no parameters, not a single heap allocation is necessary.

No more server crashes: You can set a Panic handler to deal with panics occurring during handling a HTTP request. The router then recovers and lets the PanicHandler log what happened and deliver a nice error page.

Perfect for APIs: The router design encourages to build sensible, hierarchical RESTful APIs. Moreover it has builtin native support for OPTIONS requests and 405 Method Not Allowed replies.

Of course you can also set custom NotFound and MethodNotAllowed handlers and serve static files.

Usage

This is just a quick introduction, view the GoDoc for details:

Let's start with a trivial example:

package main

import (
	"fmt"
	"log"

	"github.com/fasthttp/router"
	"github.com/valyala/fasthttp"
)

func Index(ctx *fasthttp.RequestCtx) {
	ctx.WriteString("Welcome!")
}

func Hello(ctx *fasthttp.RequestCtx) {
	fmt.Fprintf(ctx, "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))
}
Named parameters

As you can see, {name} is a named parameter. The values are accessible via RequestCtx.UserValues. You can get the value of a parameter by using the ctx.UserValue("name").

Named parameters only match a single path segment:

Pattern: /user/{user}

 /user/gordon                     match
 /user/you                        match
 /user/gordon/profile             no match
 /user/                           no match

Pattern with suffix: /user/{user}_admin

 /user/gordon_admin               match
 /user/you_admin                  match
 /user/you                        no match
 /user/gordon/profile             no match
 /user/gordon_admin/profile       no match
 /user/                           no match
Optional parameters

If you need define an optional parameters, add ? at the end of param name. {name?}

Regex validation

If you need define a validation, you could use a custom regex for the paramater value, add :<regex> after the name. For example: {name:[a-zA-Z]{5}}.

Optional paramters and regex validation are compatibles, only add ? between the name and the regex. For example: {name?:[a-zA-Z]{5}}.

Catch-All parameters

The second type are catch-all parameters and have the form {name:*}. Like the name suggests, they match everything. Therefore they must always be at the end of the pattern:

Pattern: /src/{filepath:*}

 /src/                     match
 /src/somefile.go          match
 /src/subdir/somefile.go   match

How does it work?

The router relies on a tree structure which makes heavy use of common prefixes, it is basically a compact prefix tree (or just Radix tree). Nodes with a common prefix also share a common parent. Here is a short example what the routing tree for the GET request method could look like:

Priority   Path             Handle
9          \                 *<1>
3          ├s                nil
2          |├earch\          *<2>
1          |└upport\         *<3>
2          ├blog\            *<4>
1          |    └{post}      nil
1          |          └\     *<5>
2          ├about-us\        *<6>
1          |        └team\   *<7>
1          └contact\         *<8>

Every *<num> represents the memory address of a handler function (a pointer). If you follow a path trough the tree from the root to the leaf, you get the complete route path, e.g \blog\{post}\, where {post} is just a placeholder (parameter) for an actual post name. Unlike hash-maps, a tree structure also allows us to use dynamic parts like the {post} parameter, since we actually match against the routing patterns instead of just comparing hashes. [As benchmarks show][benchmark], this works very well and efficient.

Since URL paths have a hierarchical structure and make use only of a limited set of characters (byte values), it is very likely that there are a lot of common prefixes. This allows us to easily reduce the routing into ever smaller problems. Moreover the router manages a separate tree for every request method. For one thing it is more space efficient than holding a method->handle map in every single node, for another thing is also allows us to greatly reduce the routing problem before even starting the look-up in the prefix-tree.

For even better scalability, the child nodes on each tree level are ordered by priority, where the priority is just the number of handles registered in sub nodes (children, grandchildren, and so on..). This helps in two ways:

  1. Nodes which are part of the most routing paths are evaluated first. This helps to make as much routes as possible to be reachable as fast as possible.
  2. It is some sort of cost compensation. The longest reachable path (highest cost) can always be evaluated first. The following scheme visualizes the tree structure. Nodes are evaluated from top to bottom and from left to right.
├------------
├---------
├-----
├----
├--
├--
└-

Why doesn't this work with http.Handler?

Because fasthttp doesn't provide http.Handler. See this description.

Fasthttp works with RequestHandler functions instead of objects implementing Handler interface. So a Router provides a Handler interface to implement the fasthttp.ListenAndServe interface.

Just try it out for yourself, the usage of Router is very straightforward. The package is compact and minimalistic, but also probably one of the easiest routers to set up.

Where can I find Middleware X?

This package just provides a very efficient request router with a few extra features. The router is just a fasthttp.RequestHandler, you can chain any fasthttp.RequestHandler compatible middleware before the router. Or you could just write your own, it's very easy!

Have a look at these middleware examples:

Chaining with the NotFound handler

NOTE: It might be required to set Router.HandleMethodNotAllowed to false to avoid problems.

You can use another fasthttp.RequestHandler, for example another router, to handle requests which could not be matched by this router by using the Router.NotFound handler. This allows chaining.

Static files

The NotFound handler can for example be used to serve static files from the root path / (like an index.html file along with other assets):

// Serve static files from the ./public directory
r.NotFound = fasthttp.FSHandler("./public", 0)

But this approach sidesteps the strict core rules of this router to avoid routing problems. A cleaner approach is to use a distinct sub-path for serving files, like /static/{filepath:*} or /files/{filepath:*}.

Web Frameworks based on Router

If the Router is a bit too minimalistic for you, you might try one of the following more high-level 3rd-party web frameworks building upon the Router package:

Expand ▾ Collapse ▴

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

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__", gotils.RandBytes(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 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

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

func (g *Group) ServeFiles(path string, rootPath string)

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

func (g *Group) ServeFilesCustom(path string, fs *fasthttp.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 (*Group) TRACE

func (g *Group) TRACE(path string, handler fasthttp.RequestHandler)

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

	// 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{})
	// 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

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 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) List

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

List returns all registered routes grouped by method

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

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

func (r *Router) ServeFiles(path string, rootPath string)

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

func (r *Router) ServeFilesCustom(path string, fs *fasthttp.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) TRACE

func (r *Router) TRACE(path string, handler fasthttp.RequestHandler)

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

Directories

Path Synopsis
radix