Browse Source

Update README.md

pull/141/head
Timothy Cyrus 10 years ago
parent
commit
66181ed337
  1. 241
      README.md

241
README.md

@ -1,211 +1,216 @@
mux mux
=== ===
[![GoDoc](https://godoc.org/github.com/gorilla/mux?status.svg)](https://godoc.org/github.com/gorilla/mux) [![GoDoc](https://godoc.org/github.com/gorilla/mux?status.svg)](https://godoc.org/github.com/gorilla/mux)
[![Build Status](https://travis-ci.org/gorilla/mux.png?branch=master)](https://travis-ci.org/gorilla/mux) [![Build Status](https://travis-ci.org/gorilla/mux.svg?branch=master)](https://travis-ci.org/gorilla/mux)
Package gorilla/mux implements a request router and dispatcher. Package `gorilla/mux` implements a request router and dispatcher.
The name mux stands for "HTTP request multiplexer". Like the standard The name mux stands for "HTTP request multiplexer". Like the standard `http.ServeMux`, `mux.Router` matches incoming requests against a list of registered routes and calls a handler for the route that matches the URL or other conditions. The main features are:
http.ServeMux, mux.Router matches incoming requests against a list of
registered routes and calls a handler for the route that matches the URL * Requests can be matched based on URL host, path, path prefix, schemes, header and query values, HTTP methods or using custom matchers.
or other conditions. The main features are: * URL hosts and paths can have variables with an optional regular expression.
* Registered URLs can be built, or "reversed", which helps maintaining references to resources.
* Requests can be matched based on URL host, path, path prefix, schemes, * Routes can be used as subrouters: nested routes are only tested if the parent route matches. This is useful to define groups of routes that share common conditions like a host, a path prefix or other repeated attributes. As a bonus, this optimizes request matching.
header and query values, HTTP methods or using custom matchers. * It implements the `http.Handler` interface so it is compatible with the standard `http.ServeMux`.
* URL hosts and paths can have variables with an optional regular
expression.
* Registered URLs can be built, or "reversed", which helps maintaining
references to resources.
* Routes can be used as subrouters: nested routes are only tested if the
parent route matches. This is useful to define groups of routes that
share common conditions like a host, a path prefix or other repeated
attributes. As a bonus, this optimizes request matching.
* It implements the http.Handler interface so it is compatible with the
standard http.ServeMux.
Let's start registering a couple of URL paths and handlers: Let's start registering a couple of URL paths and handlers:
func main() { ```go
func main() {
r := mux.NewRouter() r := mux.NewRouter()
r.HandleFunc("/", HomeHandler) r.HandleFunc("/", HomeHandler)
r.HandleFunc("/products", ProductsHandler) r.HandleFunc("/products", ProductsHandler)
r.HandleFunc("/articles", ArticlesHandler) r.HandleFunc("/articles", ArticlesHandler)
http.Handle("/", r) http.Handle("/", r)
} }
```
Here we register three routes mapping URL paths to handlers. This is Here we register three routes mapping URL paths to handlers. This is equivalent to how `http.HandleFunc()` works: if an incoming request URL matches one of the paths, the corresponding handler is called passing (`http.ResponseWriter`, `*http.Request`) as parameters.
equivalent to how http.HandleFunc() works: if an incoming request URL matches
one of the paths, the corresponding handler is called passing
(http.ResponseWriter, *http.Request) as parameters.
Paths can have variables. They are defined using the format {name} or Paths can have variables. They are defined using the format `{name}` or `{name:pattern}`. If a regular expression pattern is not defined, the matched variable will be anything until the next slash. For example:
{name:pattern}. If a regular expression pattern is not defined, the matched
variable will be anything until the next slash. For example:
r := mux.NewRouter() ```go
r.HandleFunc("/products/{key}", ProductHandler) r := mux.NewRouter()
r.HandleFunc("/articles/{category}/", ArticlesCategoryHandler) r.HandleFunc("/products/{key}", ProductHandler)
r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler) r.HandleFunc("/articles/{category}/", ArticlesCategoryHandler)
r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler)
```
The names are used to create a map of route variables which can be retrieved The names are used to create a map of route variables which can be retrieved calling `mux.Vars()`:
calling mux.Vars():
vars := mux.Vars(request) ```go
category := vars["category"] vars := mux.Vars(request)
category := vars["category"]
```
And this is all you need to know about the basic usage. More advanced options And this is all you need to know about the basic usage. More advanced options are explained below.
are explained below.
Routes can also be restricted to a domain or subdomain. Just define a host Routes can also be restricted to a domain or subdomain. Just define a host pattern to be matched. They can also have variables:
pattern to be matched. They can also have variables:
r := mux.NewRouter() ```go
// Only matches if domain is "www.example.com". r := mux.NewRouter()
r.Host("www.example.com") // Only matches if domain is "www.example.com".
// Matches a dynamic subdomain. r.Host("www.example.com")
r.Host("{subdomain:[a-z]+}.domain.com") // Matches a dynamic subdomain.
r.Host("{subdomain:[a-z]+}.domain.com")
```
There are several other matchers that can be added. To match path prefixes: There are several other matchers that can be added. To match path prefixes:
r.PathPrefix("/products/") ```go
r.PathPrefix("/products/")
```
...or HTTP methods: ...or HTTP methods:
r.Methods("GET", "POST") ```go
r.Methods("GET", "POST")
```
...or URL schemes: ...or URL schemes:
r.Schemes("https") ```go
r.Schemes("https")
```
...or header values: ...or header values:
r.Headers("X-Requested-With", "XMLHttpRequest") ```go
r.Headers("X-Requested-With", "XMLHttpRequest")
```
...or query values: ...or query values:
r.Queries("key", "value") ```go
r.Queries("key", "value")
```
...or to use a custom matcher function: ...or to use a custom matcher function:
r.MatcherFunc(func(r *http.Request, rm *RouteMatch) bool { ```go
r.MatcherFunc(func(r *http.Request, rm *RouteMatch) bool {
return r.ProtoMajor == 0 return r.ProtoMajor == 0
}) })
```
...and finally, it is possible to combine several matchers in a single route: ...and finally, it is possible to combine several matchers in a single route:
r.HandleFunc("/products", ProductsHandler). ```go
r.HandleFunc("/products", ProductsHandler).
Host("www.example.com"). Host("www.example.com").
Methods("GET"). Methods("GET").
Schemes("http") Schemes("http")
```
Setting the same matching conditions again and again can be boring, so we have Setting the same matching conditions again and again can be boring, so we have a way to group several routes that share the same requirements. We call it "subrouting".
a way to group several routes that share the same requirements.
We call it "subrouting".
For example, let's say we have several URLs that should only match when the For example, let's say we have several URLs that should only match when the host is `www.example.com`. Create a route for that host and get a "subrouter" from it:
host is `www.example.com`. Create a route for that host and get a "subrouter"
from it:
r := mux.NewRouter() ```go
s := r.Host("www.example.com").Subrouter() r := mux.NewRouter()
s := r.Host("www.example.com").Subrouter()
```
Then register routes in the subrouter: Then register routes in the subrouter:
s.HandleFunc("/products/", ProductsHandler) ```go
s.HandleFunc("/products/{key}", ProductHandler) s.HandleFunc("/products/", ProductsHandler)
s.HandleFunc("/articles/{category}/{id:[0-9]+}"), ArticleHandler) s.HandleFunc("/products/{key}", ProductHandler)
s.HandleFunc("/articles/{category}/{id:[0-9]+}"), ArticleHandler)
```
The three URL paths we registered above will only be tested if the domain is The three URL paths we registered above will only be tested if the domain is `www.example.com`, because the subrouter is tested first. This is not only convenient, but also optimizes request matching. You can create subrouters combining any attribute matchers accepted by a route.
`www.example.com`, because the subrouter is tested first. This is not
only convenient, but also optimizes request matching. You can create
subrouters combining any attribute matchers accepted by a route.
Subrouters can be used to create domain or path "namespaces": you define Subrouters can be used to create domain or path "namespaces": you define subrouters in a central place and then parts of the app can register its paths relatively to a given subrouter.
subrouters in a central place and then parts of the app can register its
paths relatively to a given subrouter.
There's one more thing about subroutes. When a subrouter has a path prefix, There's one more thing about subroutes. When a subrouter has a path prefix, the inner routes use it as base for their paths:
the inner routes use it as base for their paths:
r := mux.NewRouter() ```go
s := r.PathPrefix("/products").Subrouter() r := mux.NewRouter()
// "/products/" s := r.PathPrefix("/products").Subrouter()
s.HandleFunc("/", ProductsHandler) // "/products/"
// "/products/{key}/" s.HandleFunc("/", ProductsHandler)
s.HandleFunc("/{key}/", ProductHandler) // "/products/{key}/"
// "/products/{key}/details" s.HandleFunc("/{key}/", ProductHandler)
s.HandleFunc("/{key}/details", ProductDetailsHandler) // "/products/{key}/details"
s.HandleFunc("/{key}/details", ProductDetailsHandler)
```
Now let's see how to build registered URLs. Now let's see how to build registered URLs.
Routes can be named. All routes that define a name can have their URLs built, Routes can be named. All routes that define a name can have their URLs built, or "reversed". We define a name calling `Name()` on a route. For example:
or "reversed". We define a name calling Name() on a route. For example:
r := mux.NewRouter() ```go
r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler). r := mux.NewRouter()
r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler).
Name("article") Name("article")
```
To build a URL, get the route and call the URL() method, passing a sequence of To build a URL, get the route and call the `URL()` method, passing a sequence of key/value pairs for the route variables. For the previous route, we would do:
key/value pairs for the route variables. For the previous route, we would do:
url, err := r.Get("article").URL("category", "technology", "id", "42") ```go
url, err := r.Get("article").URL("category", "technology", "id", "42")
```
...and the result will be a url.URL with the following path: ...and the result will be a `url.URL` with the following path:
"/articles/technology/42" ```
"/articles/technology/42"
```
This also works for host variables: This also works for host variables:
r := mux.NewRouter() ```go
r.Host("{subdomain}.domain.com"). r := mux.NewRouter()
r.Host("{subdomain}.domain.com").
Path("/articles/{category}/{id:[0-9]+}"). Path("/articles/{category}/{id:[0-9]+}").
HandlerFunc(ArticleHandler). HandlerFunc(ArticleHandler).
Name("article") Name("article")
// url.String() will be "http://news.domain.com/articles/technology/42" // url.String() will be "http://news.domain.com/articles/technology/42"
url, err := r.Get("article").URL("subdomain", "news", url, err := r.Get("article").URL("subdomain", "news",
"category", "technology", "category", "technology",
"id", "42") "id", "42")
```
All variables defined in the route are required, and their values must All variables defined in the route are required, and their values must conform to the corresponding patterns. These requirements guarantee that a generated URL will always match a registered route -- the only exception is for explicitly defined "build-only" routes which never match.
conform to the corresponding patterns. These requirements guarantee that a
generated URL will always match a registered route -- the only exception is
for explicitly defined "build-only" routes which never match.
Regex support also exists for matching Headers within a route. For example, we could do: Regex support also exists for matching Headers within a route. For example, we could do:
r.HeadersRegexp("Content-Type", "application/(text|json)") ```go
r.HeadersRegexp("Content-Type", "application/(text|json)")
```
...and the route will match both requests with a Content-Type of `application/json` as well as ...and the route will match both requests with a Content-Type of `application/json` as well as `application/text`
`application/text`
There's also a way to build only the URL host or path for a route: There's also a way to build only the URL host or path for a route: use the methods `URLHost()` or `URLPath()` instead. For the previous route, we would do:
use the methods URLHost() or URLPath() instead. For the previous route,
we would do:
// "http://news.domain.com/" ```go
host, err := r.Get("article").URLHost("subdomain", "news") // "http://news.domain.com/"
host, err := r.Get("article").URLHost("subdomain", "news")
// "/articles/technology/42" // "/articles/technology/42"
path, err := r.Get("article").URLPath("category", "technology", "id", "42") path, err := r.Get("article").URLPath("category", "technology", "id", "42")
```
And if you use subrouters, host and path defined separately can be built And if you use subrouters, host and path defined separately can be built as well:
as well:
r := mux.NewRouter() ```go
s := r.Host("{subdomain}.domain.com").Subrouter() r := mux.NewRouter()
s.Path("/articles/{category}/{id:[0-9]+}"). s := r.Host("{subdomain}.domain.com").Subrouter()
s.Path("/articles/{category}/{id:[0-9]+}").
HandlerFunc(ArticleHandler). HandlerFunc(ArticleHandler).
Name("article") Name("article")
// "http://news.domain.com/articles/technology/42" // "http://news.domain.com/articles/technology/42"
url, err := r.Get("article").URL("subdomain", "news", url, err := r.Get("article").URL("subdomain", "news",
"category", "technology", "category", "technology",
"id", "42") "id", "42")
```
## Full Example ## Full Example
Here's a complete, runnable example of a small mux based server: Here's a complete, runnable example of a small `mux` based server:
```go ```go
package main package main

Loading…
Cancel
Save