mux

package
v0.0.0-...-d7376be Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2016 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package mux provides a path multiplexer and interfaces for plugin handling and custom path matching.

Index

Constants

View Source
const (
	GROUP    cType = 0
	ENDPOINT cType = 1
)

Variables

View Source
var ErrNotFound = errors.New("mux: handler not found")

ErrNotFound gets returned if a path could not be matched.

View Source
var ErrNotImplemented = errors.New("mux: handler not implemented")

ErrNotImplemented gets returned if a path could be matched but the method could not be found.

View Source
var ErrRedirectSlash = errors.New("mux: redirect trailing slash")

ErrRedirectSlash gets returned if a path could not be matched but a path with (without) a slash exists.

Functions

This section is empty.

Types

type Endpoint

type Endpoint interface {
	// Use adds a PluginHandler onto the end of the chain of plugins
	// for an Endpoint.
	Use(handler PluginHandler) Endpoint

	// UseHandler wraps the handler as a PluginHandler and adds it onto the end
	// of the plugin chain.
	UseHandler(hander http.Handler) Endpoint
}

Endpoint is an interface for endpoints that allows for the addition of per-endpoint plugin handlers.

type Group

type Group interface {
	// Add adds a handler to the passed in path under the group.
	// The full path to the handler will be the group's path concatenated
	// with the passed in path.
	Add(path string, handler http.Handler) Endpoint

	// AddFunc wraps f as an http.Handler and calls Add()
	AddFunc(path string, f func(w http.ResponseWriter, r *http.Request)) Endpoint

	// Group creates a subgroup at the passed in path. The full path for the new
	// subgroup is the parent group's path concatenated with the passed in path.
	// If a group already exists at the passed in path, the existing group is returned.
	// If a subgroup exists with a path that is a prefix of the passed in path, the new
	// subgroup will be created under the subgroup with the shorter prefix path. Any
	// existing paths that contain the passed in path as a prefix are subsumed under
	// the newly created subgroup.
	Group(path string) Group

	// Use appends handler on to the end of the Plugin chain for this group
	Use(handler PluginHandler) Group

	// UseHandler wraps handler as a PluginHandler and calls Use. Handler registered
	// using UseHandler automatically call the next-in-line Plugin.
	UseHandler(handler http.Handler) Group
}

Group is an interface for interacting with route groups

type NotFoundHandler

type NotFoundHandler struct{}

NotFoundHandler is the default http.Handler for Not Found responses. Returns a 404 status with message "Not Found."

func (NotFoundHandler) ServeHTTP

func (handler NotFoundHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type NotImplementedHandler

type NotImplementedHandler struct{}

NotImplementedHandler is the default http.Handler for Not Implemented responses. Returns a 501 status with message "Not Implemented."

func (NotImplementedHandler) ServeHTTP

func (handler NotImplementedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type PathMuxer

type PathMuxer struct {
	NotFound       http.Handler
	NotImplemented http.Handler
	Redirect       http.Handler

	// If strict, Paths with trailing slashes are considered
	// a different path than those without trailing slashes.
	// E.g. '/a/b/' != '/a/b'.
	Strict bool
	// contains filtered or unexported fields
}

PathMuxer matches string paths and methods to endpoint handlers. Paths can contain named parameters which can be restricted by regexes. PathMuxer also allows the use of global and per-route plugins.

func New

func New() *PathMuxer

New returns a pointer to a newly initialized PathMuxer.

func (*PathMuxer) Add

func (mux *PathMuxer) Add(method, path string, handler http.Handler) Endpoint

Add sets the handler for a specific method+path combination and returns the endpoint node.

func (*PathMuxer) AddFunc

func (mux *PathMuxer) AddFunc(method, path string, f func(w http.ResponseWriter, r *http.Request)) Endpoint

AddFunc wraps f as an http.Handler and set is as handler for a specific method+path combination. AddFunc returns the endpoint node.

func (*PathMuxer) Group

func (mux *PathMuxer) Group(method, path string) Group

Group creates a group at the passed in path. Groups and endpoints with paths that are subpaths of the passed in path are automatically subsumed by the newly created group. If there is a super-group that the passed in path falls under, the newly created group will be created under the super-group.

func (*PathMuxer) ServeHTTP

func (mux *PathMuxer) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP dispatches the correct handler for the route.

func (*PathMuxer) Use

func (mux *PathMuxer) Use(handler PluginHandler) *PathMuxer

Use adds a plugin handler onto the end of the chain of global plugins for the muxer.

func (*PathMuxer) UseHandler

func (mux *PathMuxer) UseHandler(handler http.Handler) *PathMuxer

UseHandler wraps the handler as a PluginHandler and adds it onto the ned of the global plugin chain for the muxer.

type PluginFunc

type PluginFunc func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

PluginFunc wraps functions so that they implement the PluginHandler interface.

func (PluginFunc) Handle

func (p PluginFunc) Handle(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

Handle calls the function wrapped as a PluginFunc.

type PluginHandler

type PluginHandler interface {
	Handle(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)
}

PluginHandler is an interface for plugins. If the plugin ran successfully, call next to continue the chain of plugins.

type RedirectHandler

type RedirectHandler struct{}

RedirectHandler is the default http.Handler for Redirect responses. Returns a 301 status and redirects to the URL stored in r. This handler assumes the necessary adjustments to r.URL have been made prior to calling the handler.

func (RedirectHandler) ServeHTTP

func (handler RedirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

Jump to

Keyboard shortcuts

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