Documentation
¶
Index ¶
- Constants
- Variables
- func ContextWithRoute(ctx context.Context, route *Route) context.Context
- func GetHost(r *http.Request) string
- func GetIP(r *http.Request, proxied bool) string
- func SetContextVars(r *http.Request, v Variables) *http.Request
- func SplitPath(path string) []string
- type BindableHandler
- type ContextKey
- type Error
- type FuncHandler
- type HandleFunc
- type Handler
- type Middleware
- type Multiplexer
- type Mux
- func (r *Mux) AddRoute(rt *Route)
- func (r *Mux) Any(path string, handler Handler, name ...string) *Route
- func (r *Mux) Delete(path string, handler Handler, name ...string) *Route
- func (r *Mux) Find(name string) *Route
- func (r *Mux) Get(path string, handler Handler, name ...string) *Route
- func (r *Mux) Handle(method string, path string, handler Handler, name ...string) *Route
- func (r *Mux) HandleFunc(method string, path string, ...) *Route
- func (r *Mux) Head(path string, handler Handler, name ...string) *Route
- func (r *Mux) Match(method string, path string) (*Route, Variables)
- func (r *Mux) Namespace(opts NamespaceOptions) Multiplexer
- func (r *Mux) NotFound(w http.ResponseWriter, req *http.Request)
- func (r *Mux) Options(path string, handler Handler, name ...string) *Route
- func (r *Mux) Patch(path string, handler Handler, name ...string) *Route
- func (r *Mux) Post(path string, handler Handler, name ...string) *Route
- func (r *Mux) Put(path string, handler Handler, name ...string) *Route
- func (r *Mux) RemoveByPath(path string)
- func (r *Mux) RemoveRoute(route *Route)
- func (r *Mux) ResetRoutes()
- func (r *Mux) Resolve(method, path string) (http.Handler, Variables, bool)
- func (r *Mux) Reverse(name string, variables ...interface{}) (string, error)
- func (r *Mux) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *Mux) Use(middleware ...Middleware)
- type NamespaceOptions
- type PathInfo
- type PathPart
- type Resolver
- type Route
- func (r *Route) AddRoute(rt *Route)
- func (r *Route) Any(path string, handler Handler, name ...string) *Route
- func (r *Route) Delete(path string, handler Handler, name ...string) *Route
- func (r *Route) Find(names []string) (*Route, bool)
- func (r *Route) Get(path string, handler Handler, name ...string) *Route
- func (r *Route) Handle(method string, path string, handler Handler, name ...string) *Route
- func (r *Route) HandleFunc(method string, path string, ...) *Route
- func (r *Route) Head(path string, handler Handler, name ...string) *Route
- func (r *Route) ID() int64
- func (r *Route) Match(method string, path []string) (*Route, bool, Variables)
- func (r *Route) Options(path string, handler Handler, name ...string) *Route
- func (r *Route) Patch(path string, handler Handler, name ...string) *Route
- func (r *Route) PathName() []string
- func (r *Route) Post(path string, handler Handler, name ...string) *Route
- func (r *Route) Preprocess(middleware ...Middleware)
- func (r *Route) Put(path string, handler Handler, name ...string) *Route
- func (r *Route) RemoveByPath(path string) bool
- func (r *Route) RemoveChild(child *Route)
- func (r *Route) RunsMiddleware(b bool)
- func (r *Route) String() string
- func (r *Route) Use(middleware ...Middleware)
- type Variables
Constants ¶
const ( ErrRouteNotFound = Error("route not found") ErrTooManyVariables = Error("too many variables provided to replace in path") ErrNotEnoughVariables = Error("not enough variables provided to replace in path") )
const ( // A special method, which will not be returned by any request, but can be set on the route to allow any method to pass. ANY = "*" GET = "GET" // Equivalent to http.MethodGet POST = "POST" // Equivalent to http.MethodPost PUT = "PUT" // Equivalent to http.MethodPut DELETE = "DELETE" // Equivalent to http.MethodDelete HEAD = "HEAD" // Equivalent to http.MethodHead PATCH = "PATCH" // Equivalent to http.MethodPatch OPTIONS = "OPTIONS" // Equivalent to http.MethodOptions NAME_SEPARATOR = ":" )
Variables ¶
var ( VARIABLE_DELIMS = []string{"<<", ">>"} URL_DELIM = "/" GLOB = "*" )
Global variables for use in the package.
These are exported so that they can be changed if needed.
Functions ¶
func ContextWithRoute ¶ added in v1.4.3
func SetContextVars ¶ added in v1.5.7
Types ¶
type BindableHandler ¶ added in v1.5.7
type ContextKey ¶ added in v1.5.7
type ContextKey struct {
K string
}
Exported ContextKey struct allows third party packages to work with the context more freely.
type FuncHandler ¶ added in v1.5.7
type FuncHandler struct {
Func HandleFunc
}
func (*FuncHandler) ServeHTTP ¶ added in v1.5.7
func (h *FuncHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)
type HandleFunc ¶
type HandleFunc func(w http.ResponseWriter, req *http.Request)
type Middleware ¶
Middleware which will run before/after the HandleFunc.
type Multiplexer ¶ added in v1.4.3
type Multiplexer interface {
Use(middleware ...Middleware)
Handle(method string, path string, handler Handler, name ...string) *Route
HandleFunc(method string, path string, handler func(w http.ResponseWriter, r *http.Request), name ...string) *Route
AddRoute(route *Route)
Any(path string, handler Handler, name ...string) *Route
Get(path string, handler Handler, name ...string) *Route
Post(path string, handler Handler, name ...string) *Route
Put(path string, handler Handler, name ...string) *Route
Patch(path string, handler Handler, name ...string) *Route
Delete(path string, handler Handler, name ...string) *Route
}
The interface for our multiplexer
This is a wrapper around the nigel2392/mux.Mux interface
type Mux ¶
type Mux struct {
NotFoundHandler http.HandlerFunc
// contains filtered or unexported fields
}
The muxer.
func (*Mux) HandleFunc ¶ added in v1.2.0
func (*Mux) Namespace ¶ added in v1.4.3
func (r *Mux) Namespace(opts NamespaceOptions) Multiplexer
Namespace allows you to create a new Multiplexer with speficic functions to run on the added routes.
func (*Mux) RemoveByPath ¶ added in v1.0.4
func (*Mux) RemoveRoute ¶ added in v1.0.4
func (*Mux) ResetRoutes ¶ added in v1.0.5
func (r *Mux) ResetRoutes()
func (*Mux) Resolve ¶ added in v1.6.0
Mirrors ServeHTTP but returns the handler, variables, and whether a match was found.
ServeHTTP is more efficient if you just want to serve the request directly, skipping an extra allocation for the returned function (handler).
func (*Mux) Use ¶
func (r *Mux) Use(middleware ...Middleware)
type NamespaceOptions ¶ added in v1.4.3
type PathInfo ¶
PathInfo contains information about a path.
It can be used to match a path, and retrieve variables from it.
func NewPathInfo ¶
NewPathInfo creates a new PathInfo object from a path string.
The path string can contain variables, which are defined by the text between the VARIABLE_DELIMS.
This function will panic if the GLOB is not the last part of the path.
func (*PathInfo) Match ¶
func (p *PathInfo) Match(path []string, from int, variables Variables) (matched bool, nextFrom int, vars Variables)
Match matches a path to this path.
It returns whether the path matched, and the variables in the path.
If the path does not match, the variables will be nil.
func (*PathInfo) Reverse ¶ added in v1.3.0
Reverse returns the path with the variables replaced.
If a variable is not found, this function will error.
func (*PathInfo) WithParent ¶ added in v1.4.8
WithParent returns a new PathInfo with the given parent.
type Route ¶
type Route struct {
Name string
Method string
Middleware []Middleware
PreMiddleware []Middleware
Path *PathInfo
Children []*Route
Handler Handler
Parent *Route
ParentMux *Mux
DisabledMiddleware bool // Is middleware disabled for this route?
// contains filtered or unexported fields
}
func RouteFromContext ¶ added in v1.4.3
func (*Route) Handle ¶
Handle adds a handler to the route.
It returns the route that was added so that it can be used to add children.
func (*Route) HandleFunc ¶
func (*Route) Match ¶
Route.Match tries to match this route (from the beginning of the path) and, on partial match, descends into children carrying the "from" index.
func (*Route) Preprocess ¶ added in v1.3.9
func (r *Route) Preprocess(middleware ...Middleware)
Preprocess adds middleware to the route that will be executed before any other middleware or handler.
func (*Route) RemoveByPath ¶ added in v1.0.4
func (*Route) RemoveChild ¶ added in v1.0.4
func (*Route) RunsMiddleware ¶ added in v1.3.5
DisableMiddleware disables the middleware for the route.
func (*Route) Use ¶ added in v1.2.0
func (r *Route) Use(middleware ...Middleware)
Use adds middleware to the route.