route

package
v0.0.0-...-646a310 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2014 License: MIT Imports: 8 Imported by: 6

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var MethodNotAllowed func(i interface{}, rw http.ResponseWriter, rq *http.Request)
View Source
var NotFound http.Handler = http.HandlerFunc(func(rw http.ResponseWriter, rq *http.Request) {
	rw.Header().Add("Content-Type", "text/plain")
	rw.WriteHeader(int(fweight.StatusNotFound))
	fmt.Fprintf(
		rw,
		`A resource could not be found to match your request.
	Technical Information:
	Request URI: %+q
	Method: %+q
	Protocol: %+q
	Headers: %+v
	ContentLength: %v
	Remote Address: %+q`,
		rq.Host+rq.URL.Path,
		rq.Method,
		rq.Proto,
		rq.Header,
		rq.ContentLength,
		rq.RemoteAddr,
	)
})

NotFound is a convenience http.Handler for RouteHandler's NotFound.

View Source
var OptionsHandler func(i interface{}, rw http.ResponseWriter, rq *http.Request)

Functions

func Ampersand

func Ampersand(url, prefix, terminators string) string

Function Ampersand is used to retrieve the URL part that was swallowed by an ampersand path. To achieve this, `prefix` is trimmed from the beginning of the url, and the resulting string is returned up until an instance of a char in `terminators` or the end of the string.

Example
name := Ampersand("/user/bob/images/", "/user/", "/")
fmt.Println(name)

name = Ampersand("/user/anne.html", "/user/", "./")
fmt.Println(name)

name = Ampersand("/user/anne/images/cat.png", "/user/", "./")
fmt.Println(name)
Output:

bob
anne
anne

func Depth

func Depth(url string) int

Function Depth returns the depth in levels of a URL, separated by '/'.

func PartN

func PartN(url string, n int) string

Function PartN returns the Nth part of a URL, separated by '/'. A leading slash ('/usr') is ignored. If there are too few parts, empty string is returned.

Example
name := PartN("/user/bob/images", 1)
fmt.Println(name)

annepage := PartN("/user/anne.html", 1)
fmt.Println(annepage)

imagename := PartN("user/anne/imags/cat.png", 3)
fmt.Println(imagename)

empty := PartN("/user/anne/images/", 3)
fmt.Println(empty)
Output:

bob
anne.html
cat.png

func TrimPast

func TrimPast(s, terminators string) string

Returns s up until a char in terminators, or the whole string.

func TrimPastRune

func TrimPastRune(s string, r rune) string

Returns s up until r, or the whole string.

Types

type DomainRouter

type DomainRouter interface {
	Subdomain(subpath string) (s Router, remainingDomain string)
	Router
}

Internally, the domain system is based on the DomainRouter interface. A DomainRouter will check an assertion that the Router that is recieved from the Subdomain function is a DomainRouter. If is, the RouteHTTP function continues down the trie until this is not the case.

type Handler

type Handler struct {
	http.Handler
}

A handler represents a termination of the Router B+ tree.

func Handle

func Handle(h http.Handler) Handler

Function HandlerOf returns a Handler of a http.Handler, which can be used to terminatr and handle routes.

func HandleFunc

func HandleFunc(hf http.HandlerFunc) Handler

func (Handler) RouteHTTP

func (h Handler) RouteHTTP(rq *http.Request) Router

type NoExtPath

type NoExtPath Path

NoExtPath is a PathRouter that ignores the extensions of folders and files when matching request URIs.

func (NoExtPath) Child

func (i NoExtPath) Child(subpath string) (Router, string)

Function Child is provided by all types implementing the PathingRouter interface.

func (NoExtPath) RouteHTTP

func (i NoExtPath) RouteHTTP(r *http.Request) Router

type Path

type Path map[string]Router

Type Path is a Router which routes by URL path. Files or directories with empty names are not allowed. The empty name routes to the terminal Router, the one used if the path stops here, and the ampersand ("&") path swallows the next file segment in the path, regardless of its contents.

It should be noted that when RouteHTTP is called the PathRouter is followed to completion from the start to end of the URL, thus using two routers separately (i.e. separated by a different Router in the routing tree) causes two separate and independant operations on the path; a Path that routes a/b followed by one that routes a/b/c does not result in a route of a/b/a/c, instead resulting in a route of just a/b.

func (Path) AddChild

func (p Path) AddChild(pR Router, name string) Path

Function AddChild adds a child PathRouter which is used as the next name in the path.

A non PathRouter child will cause the Router to be returned to the caller of RouteHTTP, causing the path to terminate there if it is a Handler.

func (Path) Child

func (p Path) Child(subpath string) (n Router, remainingSubpath string)

Function Child is provided by all types that implement the PathingRouter interface.

func (Path) ChildProcess

func (p Path) ChildProcess(subpath string, process func(string) string) (n Router, remainingSubpath string)

Function Child returns the next Router associated with the next 'hop' in the path.

func (Path) Handler

func (p Path) Handler(r Router) Path

Sets the Handler that is used when the path terminates here. This is the same as AddChild(r, ""); the empty string routes to here.

func (Path) RouteHTTP

func (p Path) RouteHTTP(rq *http.Request) Router

RouteHTTP traverses the tree of Paths until the end of the URL path is encountered, returning the terminal router or nil.

type PathingRouter

type PathingRouter interface {
	Child(subpath string) (n Router, remainingSubpath string)
}

A PathingRouter is part of the filepath, and can take part in the descent of the filepath trie.

type RecoverHandler

type RecoverHandler interface {
	//'i' is the data the server paniced with.
	ServeRecover(i interface{}) http.Handler
}
var HandleRecovery RecoverHandler = RecoverHandlerFunc(func(i interface{}) http.Handler {
	return http.HandlerFunc(func(rw http.ResponseWriter, rq *http.Request) {
		rw.Header().Add("Content-Type", "text/plain")
		rw.WriteHeader(int(fweight.StatusInternalServerError))
		fmt.Fprintf(
			rw,
			`An Internal Server Error was encountered while handling your request:
			%+q`,
			fmt.Sprint(i),
		)

		log.Printf(
			"Internal Server Error: %+q\n %s",
			fmt.Sprint(i),
			deb.Stack(),
		)
	})
})

HandleRecovery is a convenience RecoverHandler for development builds.

type RecoverHandlerFunc

type RecoverHandlerFunc func(interface{}) http.Handler

func (RecoverHandlerFunc) ServeRecover

func (r RecoverHandlerFunc) ServeRecover(i interface{}) http.Handler

type RouteHandler

type RouteHandler struct {
	Router
	//NotFound is called when a route for the request could not be found.
	//Recover is called when there is a panic in a Router.
	NotFound http.Handler
	Recover  RecoverHandler
}

A RouteHandler implements an http.Handler for a tree of Routers. When the RouteHandler is unable to find an http.Handler, or encounters an error while handling a response, it uses the NotFound and InternalServerError handlers.

These functions run at the position of this RouteHandler in the pipeline, meaning compression and security middleware that wraps this http.Handler will still be executed.

func (RouteHandler) HandleInternalServerError

func (r RouteHandler) HandleInternalServerError(i interface{}) http.Handler

func (RouteHandler) HandleNotFound

func (r RouteHandler) HandleNotFound(rq *http.Request) http.Handler

func (RouteHandler) ServeHTTP

func (s RouteHandler) ServeHTTP(rw http.ResponseWriter, rq *http.Request)

ServeHTTP traverses this RouteHandler's Router tree. If a failure is encountered, if RouteHandler.Failure is non-nil, it is called for an http.Handler which it uses to handle the response, Handling at this RouteHandler's position in the middleware stack. If it is nil, ServeHTTP instead panics with an ExtendedErr.

type Router

type Router interface {
	RouteHTTP(*http.Request) Router
}

A Router value is used to find the correct http.Handler for this http.Request.

The routing terminates when Router is Handler, or if Router is nil.

func PathRouteHTTP

func PathRouteHTTP(p PathingRouter, rq *http.Request) Router

Performs RouteHTTP on a trie of PathingRouters.

type RouterFunc

type RouterFunc func(*http.Request) Router

func (RouterFunc) RouteHTTP

func (r RouterFunc) RouteHTTP(rq *http.Request) Router

type Subdomain

type Subdomain map[string]Router

Subdomain implements the DomainRouter interface and is used to route requests to subdomain trees and the paths below them.

The empty subdomain ("") is used when the route terminates here.

func (Subdomain) Domain

func (s Subdomain) Domain(name string, r Router) Subdomain

func (Subdomain) Here

func (s Subdomain) Here(r Router)

func (Subdomain) RouteHTTP

func (s Subdomain) RouteHTTP(rq *http.Request) Router

RouteHTTP completes a route down a series of DomainRouters. Once the router is no longer a DomainRouter, it is returned.

func (Subdomain) String

func (s Subdomain) String() (o string)

func (Subdomain) Subdomain

func (s Subdomain) Subdomain(subpath string) (Router, string)

Function Subdomain is provided by all types implementing the SubdomainRouter interface.

type Verb

type Verb map[string]Router

A Router that routes based on verbs and provides

func GetOnly

func GetOnly(r Router) Verb

func (Verb) Delete

func (p Verb) Delete(hf Router) Verb

func (Verb) Get

func (p Verb) Get(hf Router) Verb

func (Verb) Head

func (p Verb) Head(hf Router) Verb

func (Verb) Options

func (p Verb) Options(hf Router) Verb

func (Verb) Patch

func (p Verb) Patch(hf Router) Verb

func (Verb) Post

func (p Verb) Post(hf Router) Verb

func (Verb) Put

func (p Verb) Put(hf Router) Verb

func (Verb) RouteHTTP

func (v Verb) RouteHTTP(rq *http.Request) Router

func (Verb) Verb

func (p Verb) Verb(verb string, hf Router) Verb

func Verb adds an HTTP verb to this VerbRouter.

func (Verb) Verbs

func (v Verb) Verbs() (ops []string)

Jump to

Keyboard shortcuts

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