router

package
v0.0.0-...-6b840e7 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2022 License: MIT Imports: 10 Imported by: 0

README

api/router

an extremely fast, 0-garbage http router.

Benchmarks
➜ go test -bench=. -cpu 8 -tags httprouter
PASS
BenchmarkHttpRouter5Params-8    10000000               162 ns/op             160 B/op          1 allocs/op
BenchmarkHttpRouterStatic-8     50000000              24.1 ns/op               0 B/op          0 allocs/op
BenchmarkJumpRouter5Params-8     5000000               376 ns/op               0 B/op          0 allocs/op
BenchmarkJumpRouterStatic-8     20000000              62.0 ns/op               0 B/op          0 allocs/op
TODO
  • Add fasthttp benchmarks and tests.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTooManyStars is returned if there are multiple *params in the path
	ErrTooManyStars = errors.New("too many stars")
	// ErrStarNotLast is returned if *param is not the last part of the path.
	ErrStarNotLast = errors.New("star param must be the last part of the path")
)

Functions

func DefaultNotFoundHandler

func DefaultNotFoundHandler(w http.ResponseWriter, req *http.Request, _ Params)

DefaultNotFoundHandler is the default panic handler

func DefaultPanicHandler

func DefaultPanicHandler(w http.ResponseWriter, req *http.Request, v interface{})

DefaultPanicHandler is the default panic handler

Types

type Handler

type Handler func(w http.ResponseWriter, req *http.Request, p Params)

Handler is what handler looks like, duh? *note* `p` is NOT safe to be used outside the handler, call p.Copy() if you need to use it.

type OnRequestDone

type OnRequestDone = func(ctx context.Context, group, method, uri string, duration time.Duration)

type Options

type Options struct {
	OnRequestDone
	NoAutoCleanURL           bool // don't automatically clean URLs, not recommended
	NoDefaultPanicHandler    bool // don't use the default panic handler
	NoPanicOnInvalidAddRoute bool // don't panic on invalid routes, return an error instead
	NoCatchPanics            bool // don't catch panics
	NoAutoHeadToGet          bool // disable automatically handling HEAD requests
	ProfileLabels            bool
}

Options passed to the router

type PanicHandler

type PanicHandler func(w http.ResponseWriter, req *http.Request, v interface{})

PanicHandler is a special handler that gets called if a panic happens

type Param

type Param struct {
	Name  string
	Value string
}

Param is a key/value pair

type Params

type Params []Param

Params handles the named params in your url, it is *NOT* safe to be used outside of your handler.

func (Params) Copy

func (p Params) Copy() Params

Copy returns a copy of p, required if you want to store it somewhere or use it outside of your handler.

func (Params) Get

func (p Params) Get(name string) string

Get returns a param by name

func (Params) GetExt

func (p Params) GetExt(name string) (val, ext string)

GetExt returns the value split at the last extension available, for example:

if :filename == "report.json", GetExt("filename") returns "report", "json"

type Router

type Router struct {
	NotFoundHandler         Handler
	MethodNotAllowedHandler Handler
	PanicHandler            PanicHandler
	// contains filtered or unexported fields
}

Router is an efficient routing library

func New

func New(opts *Options) *Router

New returns a new Router

func (*Router) AddRoute

func (r *Router) AddRoute(group, method, route string, h Handler) error

AddRoute adds a Handler to the specific method and route. Calling AddRoute after starting the http server is racy and not supported.

func (*Router) GetRoutes

func (r *Router) GetRoutes() [][3]string

func (*Router) Match

func (r *Router) Match(method, path string) (group string, handler Handler, params Params)

Match matches a method and path to a handler. if METHOD == HEAD and there isn't a specific handler for it, it returns the GET handler for the path.

func (*Router) ServeHTTP

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

ServeHTTP implements http.Handler

Jump to

Keyboard shortcuts

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