router

package module
v0.0.0-...-15c4ed7 Latest Latest
Warning

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

Go to latest
Published: May 1, 2017 License: Apache-2.0 Imports: 4 Imported by: 4

README

CloudyKit Router

CloudyKit Router was developed to be a very fast router on matching and retrieving of parameters.

Main characteristics

  1. Use prefix tree with special nodes to match dynamic sentences and catch all.
  2. No allocations, matching and retrieve parameters don't allocates.
  3. Nodes has precedence when matching, text node then single sentence node then wildcard node

Benchmarks

I benchmark CloudyKit router against "github.com/julienschmidt/httprouter" and the results are pretty good, the benchmark consist of test the routes listem below per each iteration.

	/users
	/users/:userId
	/users/:userId/subscriptions
	/users/:userId/subscriptions/:subscription
	/assets/*file

Benchmark source: https://github.com/CloudyKit/benchmarks/router

####### Results

Go 1.6
BenchmarkCloudyKitRouter-4       2000000               618 ns/op               0 B/op          0 allocs/op
BenchmarkHttprouterRouter-4      1000000              1104 ns/op             224 B/op          4 allocs/op

Go tip
BenchmarkCloudyKitRouter-4       3000000               492 ns/op               0 B/op          0 allocs/op
BenchmarkHttprouterRouter-4      2000000              1006 ns/op             224 B/op          4 allocs/op

Precedence example

On the example below the router will test the routes in the following order, /users/list then /users/:userId then /users/*page.

	router.AddRoute("GET","/users/:userId",...)
	router.AddRoute("GET","/users/*page",...)
	router.AddRoute("GET","/users/list",...)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler func(http.ResponseWriter, *http.Request, Parameter)

type Parameter

type Parameter struct {
	// contains filtered or unexported fields
}

Parameter holds the parameters matched in the route

func (*Parameter) ByName

func (vv *Parameter) ByName(name string) string

ByName returns the url parameter by name

func (*Parameter) IndexOf

func (vv *Parameter) IndexOf(name string) int

IndexOf returns the index of the argument by name

func (*Parameter) Len

func (vv *Parameter) Len() int

Len returns number arguments matched in the provided URL

func (Parameter) String

func (node Parameter) String() string

type Router

type Router struct {
	// contains filtered or unexported fields
}

func New

func New() *Router

func (*Router) AddRoute

func (router *Router) AddRoute(method string, path string, fn Handler)

func (*Router) Finalize

func (router *Router) Finalize()

func (*Router) FindRoute

func (router *Router) FindRoute(method string, path string) (Handler, Parameter)

func (*Router) ServeHTTP

func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Router) String

func (router *Router) String() string

Jump to

Keyboard shortcuts

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