Documentation
¶
Overview ¶
Package mux implements a high performance and powerful trie based url path router for Go.
Index ¶
- Constants
- Variables
- func Delete(pattern string, handler HandlerFunc)
- func Get(pattern string, handler HandlerFunc)
- func HandleError(err error) (events.APIGatewayProxyResponse, error)
- func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error)
- func Head(pattern string, handler HandlerFunc)
- func MarshalResponse(status int, headers map[string]string, data interface{}) (events.APIGatewayProxyResponse, error)
- func Options(pattern string, handler HandlerFunc)
- func Param(r *http.Request, key string) string
- func Params(r *http.Request) map[string]string
- func Patch(pattern string, handler HandlerFunc)
- func Post(pattern string, handler HandlerFunc)
- func Put(pattern string, handler HandlerFunc)
- type Group
- func (g *Group) Delete(pattern string, handler HandlerFunc)
- func (g *Group) Get(pattern string, handler HandlerFunc)
- func (g *Group) Head(pattern string, handler HandlerFunc)
- func (g *Group) Options(pattern string, handler HandlerFunc)
- func (g *Group) Patch(pattern string, handler HandlerFunc)
- func (g *Group) Post(pattern string, handler HandlerFunc)
- func (g *Group) Put(pattern string, handler HandlerFunc)
- type HandlerFunc
- type Matched
- type Node
- type Router
- func (r *Router) DefaultHandler(handler HandlerFunc)
- func (r *Router) Delete(pattern string, handler HandlerFunc)
- func (r *Router) Get(pattern string, handler HandlerFunc)
- func (r *Router) Handle(method, pattern string, handler HandlerFunc)
- func (r *Router) Handler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error)
- func (r *Router) Head(pattern string, handler HandlerFunc)
- func (r *Router) Options(pattern string, handler HandlerFunc)
- func (r *Router) Patch(pattern string, handler HandlerFunc)
- func (r *Router) Post(pattern string, handler HandlerFunc)
- func (r *Router) Put(pattern string, handler HandlerFunc)
- type RouterError
- type TOptions
- type Trie
Constants ¶
const Version = "0.0.1"
Version holds the current mux version
Variables ¶
var ExposeServerErrors = true
Functions ¶
func Delete ¶
func Delete(pattern string, handler HandlerFunc)
func Get ¶
func Get(pattern string, handler HandlerFunc)
func HandleError ¶
func HandleError(err error) (events.APIGatewayProxyResponse, error)
func Handler ¶
func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error)
func Head ¶
func Head(pattern string, handler HandlerFunc)
func MarshalResponse ¶
func Options ¶
func Options(pattern string, handler HandlerFunc)
func Patch ¶
func Patch(pattern string, handler HandlerFunc)
func Post ¶
func Post(pattern string, handler HandlerFunc)
func Put ¶
func Put(pattern string, handler HandlerFunc)
Types ¶
type Group ¶
type Group struct {
Name string
// contains filtered or unexported fields
}
func (*Group) Delete ¶
func (g *Group) Delete(pattern string, handler HandlerFunc)
func (*Group) Get ¶
func (g *Group) Get(pattern string, handler HandlerFunc)
func (*Group) Head ¶
func (g *Group) Head(pattern string, handler HandlerFunc)
func (*Group) Options ¶
func (g *Group) Options(pattern string, handler HandlerFunc)
func (*Group) Patch ¶
func (g *Group) Patch(pattern string, handler HandlerFunc)
func (*Group) Post ¶
func (g *Group) Post(pattern string, handler HandlerFunc)
func (*Group) Put ¶
func (g *Group) Put(pattern string, handler HandlerFunc)
type HandlerFunc ¶
type HandlerFunc func(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error)
type Matched ¶
type Matched struct {
// Either a Node pointer when matched or nil
Node *Node
// Either a map contained matched values or empty map.
Params map[string]string
// Matched path to access
// If Node is nil then redirect to this PATH
Path string
}
Matched is a result returned by Trie.Match.
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node represents a node on defined patterns that can be matched.
func (*Node) GetAllow ¶
GetAllow returns allow methods defined on the node
trie := New()
trie.Parse("/").Handle("GET", handler1)
trie.Parse("/").Handle("PUT", handler2)
// trie.Match("/").Node.GetAllow() == []string{"GET", "PUT"}
func (*Node) GetHandler ¶
GetHandler ... GetHandler returns handler by method that defined on the node
trie := New()
trie.Parse("/api").Handle("GET", func handler1() {})
trie.Parse("/api").Handle("PUT", func handler2() {})
trie.Match("/api").Node.GetHandler("GET").(func()) == handler1
trie.Match("/api").Node.GetHandler("PUT").(func()) == handler2
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Mux is a tire base HTTP request router which can be used to dispatch requests to different handler functions.
func (*Router) DefaultHandler ¶
func (r *Router) DefaultHandler(handler HandlerFunc)
DefaultHandler registers a new handler in the Mux that will run if there is no other handler matching.
func (*Router) Delete ¶
func (r *Router) Delete(pattern string, handler HandlerFunc)
Delete registers a new DELETE route for a path with matching handler in the Mux.
func (*Router) Get ¶
func (r *Router) Get(pattern string, handler HandlerFunc)
Get registers a new GET route for a path with matching handler in the Mux.
func (*Router) Handle ¶
func (r *Router) Handle(method, pattern string, handler HandlerFunc)
Handle registers a new handler with method and path in the Mux. For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used.
func (*Router) Handler ¶
func (r *Router) Handler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error)
ServeHTTP implemented http.Handler interface
func (*Router) Head ¶
func (r *Router) Head(pattern string, handler HandlerFunc)
Head registers a new HEAD route for a path with matching handler in the Mux.
func (*Router) Options ¶
func (r *Router) Options(pattern string, handler HandlerFunc)
Options registers a new OPTIONS route for a path with matching handler in the Mux.
func (*Router) Patch ¶
func (r *Router) Patch(pattern string, handler HandlerFunc)
Patch registers a new PATCH route for a path with matching handler in the Mux.
func (*Router) Post ¶
func (r *Router) Post(pattern string, handler HandlerFunc)
Post registers a new POST route for a path with matching handler in the Mux.
func (*Router) Put ¶
func (r *Router) Put(pattern string, handler HandlerFunc)
Put registers a new PUT route for a path with matching handler in the Mux.
type RouterError ¶
func (RouterError) Error ¶
func (err RouterError) Error() string
type TOptions ¶
type TOptions struct {
// CaseSensitive when matching URL path.
CaseSensitive bool
// PathClean defines the path cleaning behavior for new routes. The default value is false.
// Users should be careful about which routes are not cleaned
// When true, the path will be cleaned, if the route path is "/path//to", it will return "/path/to"
// When false, if the route path is "/path//to", it will remain with the double slash
PathClean bool
// StrictSlash defines the trailing slash behavior for new routes.
// The initial value is false.
// When true, if the route path is "/path/", accessing "/path" will
// redirect to the former and vice versa. In other words,
// your application will always see the path as specified in the route.
// When false, if the route path is "/path", accessing "/path/" will
// not match this route and vice versa.
StrictSlash bool
// UseEncodedPath tells the router to match the encoded original path to the routes.
// For eg. "/path/foo%2Fbar/to" will match the path "/path/{var}/to".
// This behavior has the drawback of needing to match routes against r.RequestURI instead of r.URL.Path.
// Any modifications (such as http.StripPrefix) to r.URL.Path will not affect routing when this flag is
// on and thus may induce unintended behavior.
// If not called, the router will match the unencoded path to the routes.
// For eg. "/path/foo%2Fbar/to" will match the path "/path/foo/bar/to"
UseEncodedPath bool
}
Options describes options for Trie.
type Trie ¶
type Trie struct {
// contains filtered or unexported fields
}
Trie represents a trie that defining patterns and matching URL.
func NewTrie ¶
NewTrie returns a trie
trie := New()
// disable CaseSensitive, PathClean and StrictSlash
trie := New(Options{})