dispatch

package
v0.0.0-...-ac6c325 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2016 License: MIT Imports: 4 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dispatcher

type Dispatcher struct {
	// contains filtered or unexported fields
}
Example (Argument)
dispatcher := new(Dispatcher)
dispatcher.Add("/foo/<arg>", myHandler1)
printMatchingPaths(dispatcher)
Output:

/foo/bar map[arg:bar]
Example (Literal)
dispatcher := new(Dispatcher)
dispatcher.Add("/foo", myHandler1)
printMatchingPaths(dispatcher)
Output:

/foo
/foo/
/foo?arg=1
/foo#arg=1
Example (Multiargument)
dispatcher := new(Dispatcher)
dispatcher.Add("/<first>/<second>", myHandler1)
printMatchingPaths(dispatcher)
Output:

/foo/bar map[second:bar first:foo]
Example (Root)
dispatcher := new(Dispatcher)
dispatcher.Add("/", myHandler1)
printMatchingPaths(dispatcher)
Output:

/
Example (Star)
dispatcher := new(Dispatcher)
dispatcher.Add("/foo/*", myHandler1)
printMatchingPaths(dispatcher)
Output:

/foo/bar
/foo/bar/baz

func (*Dispatcher) Add

func (dispatcher *Dispatcher) Add(pattern string, handler Handler) error

Adds the specified handler with the specified pattern.

The pattern format is described below.

By default, the pattern is an exact-prefix match. Query parameters never matter for the purpose of dispatch.

PATTERN   : /foo
MATCHES   : /foo, /foo?arg=1
MISMATCHES: /foobar, /foo/bar

PATTERN   : /foo/bar
MATCHES   : /foo/bar, /foo/bar?arg=1
MISMATCHES: /foobar, /foo/bar/baz

"/*" Can be used to construct a non-exact-match prefix. Note that paths like "/foo*" or "/foo/*/bar" are invalid. "/*" must terminate the pattern.

PATTERN   : /foo/*
MATCHES   : /foo/bar, /foo/bar/baz
MISMATCHES: /foo, /foobar

"/<key>" Can be used as a wildcard for a path segment. The content of that path segment is forwarded to the dispatcher in the map.

PATTERN   : /foo/<a>
MATCHES   : /foo/1 (a:1), /foo/2 (a:2)
MISMATCHES: /foo, /foo/bar/baz, /foobar

PATTERN   : /foo/<a>/<b>
MATCHES   : /foo/1/2 (a:1,b:2)
MISMATCHES: /foo, /foo/1, /foo/1/2/3

func (*Dispatcher) RootHandler

func (dispatcher *Dispatcher) RootHandler(w http.ResponseWriter, r *http.Request)

Use http.HandleFunc("/", dispatcher.RootHandler) to use this dispatcher for dispatch in an application.

func (*Dispatcher) Select

func (dispatcher *Dispatcher) Select(
	path string) (handler Handler, args map[string]string, err error)

type Handler

type Handler func(http.ResponseWriter, *http.Request, map[string]string)

Jump to

Keyboard shortcuts

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