pathrouter

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2023 License: BSD-3-Clause Imports: 5 Imported by: 0

README

Path Router

GoDoc Widget Go Report Card Widget

Fork of julienschmidt/httprouter to route paths without HTTP semantics.

Introduction

pathrouter is a lightweight high performance path request router.

In contrast to the default mux of Go's `net/http** package, this router supports variables in the routing pattern. It does not use HTTP semantics and is intended for matching paths only.*

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

Features

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? HttpRouter 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. The only heap allocations that are made are building the slice of the key-value pairs for path parameters, and building new context and request objects (the latter only in the standard Handler/HandlerFunc API). In the 3-argument API, if the request path contains no parameters not a single heap allocation is necessary.

Best Performance: Benchmarks speak for themselves. See below for technical details of the implementation.

No more server crashes: You can set a Panic handler to deal with panics. The router then recovers and lets the PanicHandler log what happened.

Usage

This is just a quick introduction, view the Docs for details.

Let's start with a trivial example:

package main

import (
	"context"
	"fmt"

	"github.com/aperturerobotics/pathrouter"
)

// Responder is a custom struct used in place of a response writer.
type Responder struct {
}

func (r *Responder) Respond(ctx context.Context, path, resp string) error {
	fmt.Printf("path=%s responded with: %s", path, resp)
	return nil
}

func Index(ctx context.Context, reqPath string, p pathrouter.Params, rw *Responder) (bool, error) {
	return true, rw.Respond(ctx, reqPath, "Welcome!\n")
}

func Hello(ctx context.Context, reqPath string, p pathrouter.Params, rw *Responder) (bool, error) {
	return true, rw.Respond(ctx, reqPath, fmt.Sprintf("hello, %s!\n", p.ByName("name")))
}

func main() {
	router := pathrouter.New[*Responder]()
	router.AddHandler("/", Index)
	router.AddHandler("/hello/:name", Hello)

	ctx := context.Background()
	resp := &Responder{}

    // path=/ responded with: Welcome!
	router.Serve(ctx, "/", resp)
    // path=/hello/world responded with: hello, world!
	router.Serve(ctx, "/hello/world", resp)
    // path=/hello/reader responded with: hello, reader!
	router.Serve(ctx, "/hello/reader", resp)
}

Some things to note when compared to typical HTTP request handlers:

  • The router uses generics to pass a custom response writer type.
  • The context is passed separately in the arguments to handlers.
  • All handlers can return an error and a bool indicating "not found" if false.
  • The router strictly operates on paths and ignores the rest of the URL.
Named parameters

As you can see, :name is a named parameter. The values are accessible via pathrouter.Params, which is just a slice of pathrouter.Params. You can get the value of a parameter either by its index in the slice, or by using the ByName(name) method: :name can be retrieved by ByName("name").

When using a http.Handler (using router.Handler or http.HandlerFunc) instead of HttpRouter's handle API using a 3rd function parameter, the named parameters are stored in the request.Context. See more below under Why doesn't this work with http.Handler?.

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

Note: Since this router has only explicit matches, you can not register static routes and parameters for the same path segment. For example you can not register the patterns /user/new and /user/:user for the same request method at the same time. The routing of different request methods is independent from each other.

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, 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, it 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?

This router is intended to be used for situations where routing paths are the primary concern. It's not intended to be used as a HTTP handler, although it definitely could be, just pass http.ResponseWriter as the response type.

Attribution

This repository is a fork of julienschmidt/httprouter.

Documentation

Overview

Package pathrouter is a trie based high performance path request router.

The router matches incoming requests by the request path. If a handle is registered for the path, it is called with the path params.

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 as a slice of the Param struct, consisting each of a key and a value. The slice is passed to the Handle func as a third parameter. There are two ways to retrieve the value of a parameter:

// by the name of the parameter
user := ps.ByName("user") // defined by :user or *user

// by the index of the parameter. This way you can also get the name (key)
thirdKey   := ps[2].Key   // the name of the 3rd parameter
thirdValue := ps[2].Value // the value of the 3rd parameter

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanPath

func CleanPath(p string) string

CleanPath is the URL version of path.Clean, it returns a canonical URL path for p, eliminating . and .. elements.

The following rules are applied iteratively until no further processing can be done:

  1. Replace multiple slashes with a single slash.
  2. Eliminate each . path name element (the current directory).
  3. Eliminate each inner .. path name element (the parent directory) along with the non-.. element that precedes it.
  4. Eliminate .. elements that begin a rooted path: that is, replace "/.." by "/" at the beginning of a path.

If the result of this process is an empty string, "/" is returned

Types

type Handle

type Handle[W any] func(ctx context.Context, reqPath string, p Params, rw W) (bool, error)

Handle is a function that can be registered to a route to handle requests. Returns if the request was handled and any error. If false, nil is returned, assumes the function has returned "not found."

type Param

type Param struct {
	Key   string
	Value string
}

Param is a single URL parameter, consisting of a key and a value.

type Params

type Params []Param

Params is a Param-slice, as returned by the router. The slice is ordered, the first URL parameter is also the first slice value. It is therefore safe to read values by the index.

func (Params) ByName

func (ps Params) ByName(name string) string

ByName returns the value of the first Param which key matches the given name. If no matching Param is found, an empty string is returned.

type Router

type Router[W any] struct {
	// contains filtered or unexported fields
}

Router can be used to dispatch URL requests to different handler functions via configurable routes. The request and response types are generic.

R is the request type. W is the response writer type.

func New

func New[W any]() *Router[W]

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

func NewWithConfig

func NewWithConfig[W any](conf RouterConfig[W]) *Router[W]

NewWithConfig constructs a new Router with the given config.

All configuration values are defaulted to false.

func (*Router[W]) AddHandler

func (r *Router[W]) AddHandler(path string, handle Handle[W])

AddHandler registers a new request handle with the given path and method.

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

NOTE: this function is NOT concurrency safe.

func (*Router[W]) LookupPath

func (r *Router[W]) LookupPath(path string) (Handle[W], Params, bool)

LookupPath allows the manual lookup of a handler with a path. This is e.g. useful to build a framework around this router. If the path was found, it returns the handle 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[W]) Serve

func (r *Router[W]) Serve(ctx context.Context, reqPath string, wr W) (bool, error)

Serve serves a request with the router. Returns if the request was handled and any error. Note: if the error handler is set, may return true even if not found.

type RouterConfig

type RouterConfig[W any] struct {
	// 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

	// RedirectFixedPath configures the router 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.
	// For example /FOO and /..//Foo could be redirected to /foo.
	// RedirectTrailingSlash is independent of this option.
	RedirectFixedPath bool

	// NotFound is called when no matching route is found.
	NotFound Handle[W]

	// Function to handle panics recovered from http handlers.
	// If nil, no recover() will be called (panics will throw).
	// The fourth parameter is the error from recover().
	PanicHandler func(ctx context.Context, reqPath string, rw W, panicErr interface{})
}

RouterConfig are optional configuration parameters for the Router.

func DefaultConfig

func DefaultConfig[W any]() RouterConfig[W]

DefaultConfig returns the default configuration if none is specified. Path auto-correction, including trailing slashes, is enabled by default.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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