Documentation ¶
Index ¶
- Constants
- Variables
- func Bind(r *http.Request, b Binder, ptrOut interface{}) error
- func Dispatch(w http.ResponseWriter, d Dispatcher, v interface{}) error
- func GetParam(w http.ResponseWriter, key string) string
- func SetParam(w http.ResponseWriter, key, value string) bool
- type Binder
- type Dispatcher
- type InsertOption
- type MethodHandler
- type Mux
- func (m *Mux) Handle(pattern string, handler http.Handler)
- func (m *Mux) HandleFunc(pattern string, handlerFunc func(http.ResponseWriter, *http.Request))
- func (m *Mux) Of(prefix string) SubMux
- func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (m *Mux) Unlink() SubMux
- func (m *Mux) Use(middlewares ...Wrapper)
- type Node
- type NodeKeysSorter
- type ParamEntry
- type ParamsSetter
- type Processor
- type SubMux
- type Trie
- func (t *Trie) Autocomplete(prefix string, sorter NodeKeysSorter) (list []string)
- func (t *Trie) HasPrefix(prefix string) bool
- func (t *Trie) Insert(pattern string, options ...InsertOption)
- func (t *Trie) Parents(prefix string) (parents []*Node)
- func (t *Trie) Search(q string, params ParamsSetter) *Node
- func (t *Trie) SearchPrefix(prefix string) *Node
- type Wrapper
- type Wrappers
Constants ¶
const ( // ParamStart is the character, as a string, which a path pattern starts to define its named parameter. ParamStart = ":" // WildcardParamStart is the character, as a string, which a path pattern starts to define its named parameter for wildcards. // It allows everything else after that path prefix // but the Trie checks for static paths and named parameters before that in order to support everything that other implementations do not, // and if nothing else found then it tries to find the closest wildcard path(super and unique). WildcardParamStart = "*" )
Variables ¶
var ( Charset = "utf-8" JSON = &jsonProcessor{Prefix: nil, Indent: "", UnescapeHTML: false} XML = &xmlProcessor{Indent: ""} )
var DefaultKeysSorter = func(list []string) func(i, j int) bool { return func(i, j int) bool { return len(strings.Split(list[i], pathSep)) < len(strings.Split(list[j], pathSep)) } }
DefaultKeysSorter sorts as: first the "key (the path)" with the lowest number of slashes.
Functions ¶
func Dispatch ¶ added in v1.0.3
func Dispatch(w http.ResponseWriter, d Dispatcher, v interface{}) error
func GetParam ¶
func GetParam(w http.ResponseWriter, key string) string
GetParam returns the path parameter value based on its key, i.e "/hello/:name", the parameter key is the "name". For example if a route with pattern of "/hello/:name" is inserted to the `Trie` or handlded by the `Mux` and the path "/hello/kataras" is requested through the `Mux#ServeHTTP -> Trie#Search` then the `GetParam("name")` will return the value of "kataras". If not associated value with that key is found then it will return an empty string.
The function will do its job only if the given "w" http.ResponseWriter interface is an `paramsWriter`.
func SetParam ¶
func SetParam(w http.ResponseWriter, key, value string) bool
SetParam sets manually a parameter to the "w" http.ResponseWriter which should be a *paramsWriter. This is not commonly used by the end-developers, unless sharing values(string messages only) between handlers is absolutely necessary.
Types ¶
type Dispatcher ¶ added in v1.0.3
type Dispatcher interface { // no io.Writer because we need to set the headers here, // Binder and Processor are only for HTTP. Dispatch(http.ResponseWriter, interface{}) error }
type InsertOption ¶
type InsertOption func(*Node)
InsertOption is just a function which accepts a pointer to a Node which can alt its `Handler`, `Tag` and `Data` fields.
See `WithHandler`, `WithTag` and `WithData`.
func WithData ¶
func WithData(data interface{}) InsertOption
WithData sets the node's optionally `Data` field.
func WithHandler ¶
func WithHandler(handler http.Handler) InsertOption
WithHandler sets the node's `Handler` field (useful for HTTP).
func WithTag ¶
func WithTag(tag string) InsertOption
WithTag sets the node's `Tag` field (may be useful for HTTP).
type MethodHandler ¶ added in v1.0.2
type MethodHandler struct {
// contains filtered or unexported fields
}
MethodHandler implements the `http.Handler` which can be used on `Mux#Handle/HandleFunc` to declare handlers responsible for specific HTTP method(s).
Look `Handle` and `HandleFunc`.
func Methods ¶ added in v1.0.2
func Methods() *MethodHandler
Methods returns a MethodHandler which caller can use to register handler for specific HTTP Methods inside the `Mux#Handle/HandleFunc`. Usage: mux := muxie.NewMux() mux.Handle("/user/:id", muxie.Methods().
Handle("GET", getUserHandler). Handle("POST", saveUserHandler))
func (*MethodHandler) Handle ¶ added in v1.0.2
func (m *MethodHandler) Handle(method string, handler http.Handler) *MethodHandler
Handle adds a handler to be responsible for a specific HTTP Method. Returns this MethodHandler for further calls. Usage: Handle("GET", myGetHandler).HandleFunc("DELETE", func(w http.ResponseWriter, r *http.Request){[...]}) Handle("POST, PUT", saveOrUpdateHandler)
^ can accept many methods for the same handler ^ methods should be separated by comma, comma following by a space or just space
func (*MethodHandler) HandleFunc ¶ added in v1.0.2
func (m *MethodHandler) HandleFunc(method string, handlerFunc func(w http.ResponseWriter, r *http.Request)) *MethodHandler
HandleFunc adds a handler function to be responsible for a specific HTTP Method. Returns this MethodHandler for further calls.
func (*MethodHandler) ServeHTTP ¶ added in v1.0.2
func (m *MethodHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type Mux ¶
Mux is an HTTP request multiplexer. It matches the URL of each incoming request against a list of registered nodes and calls the handler for the pattern that most closely matches the URL.
Patterns name fixed, rooted paths and dynamic like /profile/:name or /profile/:name/friends or even /files/*file when ":name" and "*file" are the named parameters and wildcard parameters respectfully.
Note that since a pattern ending in a slash names a rooted subtree, the pattern "/*myparam" matches all paths not matched by other registered patterns, but not the URL with Path == "/", for that you would need the pattern "/".
See `NewMux`.
func NewMux ¶
func NewMux() *Mux
NewMux returns a new HTTP multiplexer which uses a fast, if not the fastest implementation of the trie data structure that is designed especially for path segments.
func (*Mux) HandleFunc ¶
HandleFunc registers a route handler function for a path pattern.
func (*Mux) Of ¶
Of returns a new Mux which its Handle and HandleFunc will register the path based on given "prefix", i.e: mux := NewMux() v1 := mux.Of("/v1") v1.HandleFunc("/users", myHandler) The above will register the "myHandler" to the "/v1/users" path pattern.
func (*Mux) ServeHTTP ¶
func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP exposes and serves the registered routes.
func (*Mux) Unlink ¶ added in v1.0.1
Unlink will remove any inheritance fields from the parent mux (and its parent) that are inherited with the `Of` function. Returns the current SubMux. Usage:
mux := NewMux() mux.Use(myLoggerMiddleware) v1 := mux.Of("/v1").Unlink() // v1 will no longer have the "myLoggerMiddleware". v1.HandleFunc("/users", myHandler)
func (*Mux) Use ¶ added in v1.0.1
Use adds middleware that should be called before each mux route's main handler. Should be called before `Handle/HandleFunc`. Order matters.
A Wrapper is just a type of `func(http.Handler) http.Handler` which is a common type definition for net/http middlewares.
To add a middleware for a specific route and not in the whole mux use the `Handle/HandleFunc` with the package-level `muxie.Pre` function instead. Functionality of `Use` is pretty self-explained but new gophers should take a look of the examples for further details.
type Node ¶
type Node struct { // insert main data relative to http and a tag for things like route names. Handler http.Handler Tag string // other insert data. Data interface{} // contains filtered or unexported fields }
Node is the trie's node which path patterns with their data like an HTTP handler are saved to. See `Trie` too.
func (*Node) Keys ¶
func (n *Node) Keys(sorter NodeKeysSorter) (list []string)
Keys returns this node's key (if it's a final path segment) and its children's node's key. The "sorter" can be optionally used to sort the result.
type NodeKeysSorter ¶
NodeKeysSorter is the type definition for the sorting logic that caller can pass on `GetKeys` and `Autocomplete`.
type ParamEntry ¶
ParamEntry holds the Key and the Value of a named path parameter.
func GetParams ¶
func GetParams(w http.ResponseWriter) []ParamEntry
GetParams returns all the available parameters based on the "w" http.ResponseWriter which should be a *paramsWriter.
The function will do its job only if the given "w" http.ResponseWriter interface is an `paramsWriter`.
type ParamsSetter ¶
ParamsSetter is the interface which should be implemented by the params writer for `Search` in order to store the found named path parameters, if any.
type Processor ¶ added in v1.0.3
type Processor interface { Binder Dispatcher }
type SubMux ¶
type SubMux interface { Of(prefix string) SubMux Unlink() SubMux Use(middlewares ...Wrapper) Handle(pattern string, handler http.Handler) HandleFunc(pattern string, handlerFunc func(http.ResponseWriter, *http.Request)) }
SubMux is the child of a main Mux.
type Trie ¶
type Trie struct {
// contains filtered or unexported fields
}
Trie contains the main logic for adding and searching nodes for path segments. It supports wildcard and named path parameters. Trie supports very coblex and useful path patterns for routes. The Trie checks for static paths(path without : or *) and named parameters before that in order to support everything that other implementations do not, and if nothing else found then it tries to find the closest wildcard path(super and unique).
func NewTrie ¶
func NewTrie() *Trie
NewTrie returns a new, empty Trie. It is only useful for end-developers that want to design their own mux/router based on my trie implementation.
See `Trie`
func (*Trie) Autocomplete ¶
func (t *Trie) Autocomplete(prefix string, sorter NodeKeysSorter) (list []string)
Autocomplete returns the keys that starts with "prefix", this is useful for custom search-engines built on top of my trie implementation.
func (*Trie) Insert ¶
func (t *Trie) Insert(pattern string, options ...InsertOption)
Insert adds a node to the trie.
func (*Trie) Search ¶
func (t *Trie) Search(q string, params ParamsSetter) *Node
Search is the most important part of the Trie. It will try to find the responsible node for a specific query (or a request path for HTTP endpoints).
Search supports searching for static paths(path without : or *) and paths that contain named parameters or wildcards. Priority as: 1. static paths 2. named parameters with ":" 3. wildcards 4. closest wildcard if not found, if any 5. root wildcard
func (*Trie) SearchPrefix ¶
SearchPrefix returns the last node which holds the key which starts with "prefix".
type Wrapper ¶ added in v1.0.1
Wrapper is just a type of `func(http.Handler) http.Handler` which is a common type definition for net/http middlewares.
type Wrappers ¶ added in v1.0.1
type Wrappers struct {
// contains filtered or unexported fields
}
Wrappers contains `Wrapper`s that can be registered and used by a "main route handler". Look the `Pre` and `For/ForFunc` functions too.
func Pre ¶ added in v1.0.1
Pre starts a chain of handlers for wrapping a "main route handler" the registered "middleware" will run before the main handler(see `Wrappers#For/ForFunc`).
Usage: mux := muxie.NewMux() myMiddlewares := muxie.Pre(myFirstMiddleware, mySecondMiddleware) mux.Handle("/", myMiddlewares.ForFunc(myMainRouteHandler))
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
_benchmarks
|
|
_examples
|
|
6_middleware
Package main will explore the helpers for middleware(s) that Muxie has to offer, but they are totally optional, you can still use your favourite pattern to wrap route handlers.
|
Package main will explore the helpers for middleware(s) that Muxie has to offer, but they are totally optional, you can still use your favourite pattern to wrap route handlers. |