Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrRouteNotFound = errors.New("route not found")
Functions ¶
This section is empty.
Types ¶
type Resolver ¶
type Resolver struct {
// contains filtered or unexported fields
}
Resolver resolves a request or path to a route.
Given a registered set of route strings, this will take an *http.Request or a verb-path string and resolve it to the best match.
For example, given the registered path "GET /foo/*", the request "GET /foo/bar" will resolve to "GET /foo/*", while "GET /foo/bar/baz" will not.
The path matching syntax follows the one in the `path` package of Go.
HTTP Verbs: This resolver also allows you to specify verbs at the beginning of a path:
- "GET /foo" and "POST /foo" are separate (but legal) paths. "* /foo" will allow any verb.
- There are no constraints on verb name. Thus, verbs like WebDAV's PROPSET are fine, too. Or you can make up your own.
IMPORTANT! When it comes to matching route patterns against paths, ORDER IS IMPORTANT. Routes are evaluated in order. So if two rules (GET /a/b* and GET /a/bc) are both defined, the incomming request GET /a/bc will match whichever route is defined first. See the unit tests for examples.
The `**` and `/**` Wildcards: =============================
In addition to the paths described in the `path` package of Go's core, two extra wildcard sequences are defined:
- `**`: Match everything. - `/**`: a suffix that matches any sub-path.
The `**` wildcard works in ONLY ONE WAY: If the path is declared as `**`, with nothing else, then any path will match.
VALID: `**`, `GET /foo/**`, `GET /**` NOT VALID: `GET **`, `**/foo`, `foo/**/bar`
The `/**` suffix can only be added to the end of a path, and says "Match any subpath under this".
Examples:
- URI paths "POST /foo", "GET /a/b/c", and "hello" all match "**". (The ** rule can be very dangerous for this reason.)
- URI path "POST /assets/images/foo/bar/baz.jpg" matches "POST /assets/**"
The behavior for rules that contain `/**` anywhere other than the end have undefined behavior.
The list of paths is not modifed after the resolver is constructed, so this can safely be used concurrently.
func NewResolver ¶
NewResolver creates a new Resolver.
This creates a resolver that knows about the given paths. Paths are evaluated in order.
Paths may contain HTTP verbs and wildcards as described above.
func (*Resolver) Resolve ¶
Resolve takes an HTTP request and attempts to resolve it.
When successful, it will return the path that it matched.
When no path is found, an ErrRouteNotFound error is returned.
Errors in path format, regexp compilation, and so on may also be returned.
func (*Resolver) ResolvePath ¶
Resolve a path name based using path patterns.
For general usage, this is intended to be used with verbed paths (e.g. "GET /foo"). If the verb is omitted, it will only match paths that do not have a verb. It should be noted that the Resolve() method will ONLY match verbed paths.
The resolver will take a path and attempt to match it against one of the paths the Resolver.
This resolver is designed to match path-like strings to path patterns. For example, the path `GET /foo/bar/baz` may match routes like `* /foo/*/baz` or `GET /foo/bar/*`