diff --git a/README.md b/README.md index 91cc6a61..1fd8582c 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,10 @@ their respective handler. 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: -* It implements the `http.Handler` interface so it is compatible with the standard `http.ServeMux`. +* It implements the `http.Handler` interface so, it is compatible with the standard `http.ServeMux`. * Requests can be matched based on URL host, path, path prefix, schemes, header and query values, HTTP methods or using custom matchers. * URL hosts, paths and query values can have variables with an optional regular expression. -* Registered URLs can be built, or "reversed", which helps maintaining references to resources. +* Registered URLs can be built, or "reversed", which helps to maintain 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. --- @@ -307,7 +307,7 @@ func main() { Now let's see how to build registered URLs. -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: +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: ```go r := mux.NewRouter() @@ -381,8 +381,8 @@ url, err := r.Get("article").URL("subdomain", "news", ### Walking Routes -The `Walk` function on `mux.Router` can be used to visit all of the routes that are registered on a router. For example, -the following prints all of the registered routes: +The `Walk` function on `mux.Router` can be used to visit all the routes that are registered on a router. For example, +the following prints all the registered routes: ```go package main @@ -515,7 +515,7 @@ Mux middlewares are defined using the de facto standard type: type MiddlewareFunc func(http.Handler) http.Handler ``` -Typically, the returned handler is a closure which does something with the http.ResponseWriter and http.Request passed to it, and then calls the handler passed as parameter to the MiddlewareFunc. This takes advantage of closures being able access variables from the context where they are created, while retaining the signature enforced by the receivers. +Typically, the returned handler is a closure which does something with the http.ResponseWriter and http.Request passed to it, and then calls the handler passed as parameter to the MiddlewareFunc. This takes advantage of closures being able to access variables from the context where they are created, while retaining the signature enforced by the receivers. A very basic middleware which logs the URI of the request being handled could be written as: @@ -623,7 +623,7 @@ func fooHandler(w http.ResponseWriter, r *http.Request) { } ``` -And an request to `/foo` using something like: +And a request to `/foo` using something like: ```bash curl localhost:8080/foo -v diff --git a/doc.go b/doc.go index bd5a38b5..a93ea5e1 100644 --- a/doc.go +++ b/doc.go @@ -10,18 +10,18 @@ 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: - * Requests can be matched based on URL host, path, path prefix, schemes, - header and query values, HTTP methods or using custom matchers. - * URL hosts, paths and query values 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. + - Requests can be matched based on URL host, path, path prefix, schemes, + header and query values, HTTP methods or using custom matchers. + - URL hosts, paths and query values can have variables with an optional + regular expression. + - Registered URLs can be built, or "reversed", which helps to 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: @@ -173,7 +173,7 @@ request that matches "/static/*". This makes it easy to serve static files with Now let's see how to build registered URLs. 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() r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler). @@ -184,7 +184,7 @@ key/value pairs for the route variables. For the previous route, we would do: 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 an url.URL with the following path: "/articles/technology/42" @@ -212,7 +212,7 @@ Regex support also exists for matching Headers within a route. For example, we c 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` and `application/text` There's also a way to build only the URL host or path for a route: @@ -301,6 +301,5 @@ A more complex authentication middleware, which maps session token to users, cou r.Use(amw.Middleware) Note: The handler chain will be stopped if your middleware doesn't call `next.ServeHTTP()` with the corresponding parameters. This can be used to abort a request if the middleware writer wants to. - */ package mux diff --git a/middleware.go b/middleware.go index cb51c565..0fbc5f24 100644 --- a/middleware.go +++ b/middleware.go @@ -5,7 +5,7 @@ import ( "strings" ) -// MiddlewareFunc is a function which receives an http.Handler and returns another http.Handler. +// MiddlewareFunc is a function which receives a http.Handler and returns another http.Handler. // Typically, the returned handler is a closure which does something with the http.ResponseWriter and http.Request passed // to it, and then calls the handler passed as parameter to the MiddlewareFunc. type MiddlewareFunc func(http.Handler) http.Handler diff --git a/mux.go b/mux.go index f126a602..b00b021e 100644 --- a/mux.go +++ b/mux.go @@ -31,17 +31,17 @@ func NewRouter() *Router { // It implements the http.Handler interface, so it can be registered to serve // requests: // -// var router = mux.NewRouter() +// var router = mux.NewRouter() // -// func main() { -// http.Handle("/", router) -// } +// func main() { +// http.Handle("/", router) +// } // -// Or, for Google App Engine, register it in a init() function: +// Or, for Google App Engine, register it in an init() function: // -// func init() { -// http.Handle("/", router) -// } +// func init() { +// http.Handle("/", router) +// } // // This will send all incoming requests to the router. type Router struct { @@ -127,7 +127,7 @@ func copyRouteRegexp(r *routeRegexp) *routeRegexp { // Match attempts to match the given request against the router's registered routes. // // If the request matches a route of this router or one of its subrouters the Route, -// Handler, and Vars fields of the the match argument are filled and this function +// Handler, and Vars fields of the match argument are filled and this function // returns true. // // If the request does not match any of this router's or its subrouters' routes @@ -233,7 +233,7 @@ func (r *Router) GetRoute(name string) *Route { // When false, if the route path is "/path", accessing "/path/" will not match // this route and vice versa. // -// The re-direct is a HTTP 301 (Moved Permanently). Note that when this is set for +// The re-direct is an HTTP 301 (Moved Permanently). Note that when this is set for // routes with a non-idempotent method (e.g. POST, PUT), the subsequent re-directed // request will be made as a GET by most clients. Use middleware or client settings // to modify this behaviour as needed. @@ -364,7 +364,7 @@ func (r *Router) Walk(walkFn WalkFunc) error { } // SkipRouter is used as a return value from WalkFuncs to indicate that the -// router that walk is about to descend down to should be skipped. +// router that walk is about to descend to should be skipped. var SkipRouter = errors.New("skip this router") // WalkFunc is the type of the function called for each route visited by Walk. @@ -554,7 +554,7 @@ func matchMapWithString(toCheck map[string]string, toMatch map[string][]string, return false } else if v != "" { // If value was defined as an empty string we only check that the - // key exists. Otherwise we also check for equality. + // key exists. Otherwise, we also check for equality. valueExists := false for _, value := range values { if v == value { @@ -582,7 +582,7 @@ func matchMapWithRegex(toCheck map[string]*regexp.Regexp, toMatch map[string][]s return false } else if v != nil { // If value was defined as an empty string we only check that the - // key exists. Otherwise we also check for equality. + // key exists. Otherwise, we also check for equality. valueExists := false for _, value := range values { if v.MatchString(value) { diff --git a/mux_test.go b/mux_test.go index 2d8d2b3e..99fa05c8 100644 --- a/mux_test.go +++ b/mux_test.go @@ -2089,7 +2089,7 @@ func TestErrMatchNotFound(t *testing.T) { t.Errorf("Expected ErrNotFound MatchErr, but was %v", match.MatchErr) } - // Now lets add a 404 handler to subrouter + // Now let's add a 404 handler to subrouter s.NotFoundHandler = http.NotFoundHandler() req, _ = http.NewRequest("GET", "/sub/whatever", nil) diff --git a/route.go b/route.go index 750afe57..364f9d3a 100644 --- a/route.go +++ b/route.go @@ -230,9 +230,9 @@ func (m headerMatcher) Match(r *http.Request, match *RouteMatch) bool { // Headers adds a matcher for request header values. // It accepts a sequence of key/value pairs to be matched. For example: // -// r := mux.NewRouter() -// r.Headers("Content-Type", "application/json", -// "X-Requested-With", "XMLHttpRequest") +// r := mux.NewRouter() +// r.Headers("Content-Type", "application/json", +// "X-Requested-With", "XMLHttpRequest") // // The above route will only match if both request header values match. // If the value is an empty string, it will match any value if the key is set. @@ -255,9 +255,9 @@ func (m headerRegexMatcher) Match(r *http.Request, match *RouteMatch) bool { // HeadersRegexp accepts a sequence of key/value pairs, where the value has regex // support. For example: // -// r := mux.NewRouter() -// r.HeadersRegexp("Content-Type", "application/(text|json)", -// "X-Requested-With", "XMLHttpRequest") +// r := mux.NewRouter() +// r.HeadersRegexp("Content-Type", "application/(text|json)", +// "X-Requested-With", "XMLHttpRequest") // // The above route will only match if both the request header matches both regular expressions. // If the value is an empty string, it will match any value if the key is set. @@ -283,10 +283,10 @@ func (r *Route) HeadersRegexp(pairs ...string) *Route { // // For example: // -// r := mux.NewRouter() -// r.Host("www.example.com") -// r.Host("{subdomain}.domain.com") -// r.Host("{subdomain:[a-z]+}.domain.com") +// r := mux.NewRouter() +// r.Host("www.example.com") +// r.Host("{subdomain}.domain.com") +// r.Host("{subdomain:[a-z]+}.domain.com") // // Variable names must be unique in a given route. They can be retrieved // calling mux.Vars(request). @@ -342,11 +342,11 @@ func (r *Route) Methods(methods ...string) *Route { // // For example: // -// r := mux.NewRouter() -// r.Path("/products/").Handler(ProductsHandler) -// r.Path("/products/{key}").Handler(ProductsHandler) -// r.Path("/articles/{category}/{id:[0-9]+}"). -// Handler(ArticleHandler) +// r := mux.NewRouter() +// r.Path("/products/").Handler(ProductsHandler) +// r.Path("/products/{key}").Handler(ProductsHandler) +// r.Path("/articles/{category}/{id:[0-9]+}"). +// Handler(ArticleHandler) // // Variable names must be unique in a given route. They can be retrieved // calling mux.Vars(request). @@ -377,8 +377,8 @@ func (r *Route) PathPrefix(tpl string) *Route { // It accepts a sequence of key/value pairs. Values may define variables. // For example: // -// r := mux.NewRouter() -// r.Queries("foo", "bar", "id", "{id:[0-9]+}") +// r := mux.NewRouter() +// r.Queries("foo", "bar", "id", "{id:[0-9]+}") // // The above route will only match if the URL contains the defined queries // values, e.g.: ?foo=bar&id=42. @@ -416,7 +416,7 @@ func (m schemeMatcher) Match(r *http.Request, match *RouteMatch) bool { // https://golang.org/pkg/net/http/#Request // "For [most] server requests, fields other than Path and RawQuery will be // empty." - // Since we're an http muxer, the scheme is either going to be http or https + // Since we're a http muxer, the scheme is either going to be http or https // though, so we can just set it based on the tls termination state. if scheme == "" { if r.TLS == nil { @@ -473,11 +473,11 @@ func (r *Route) BuildVarsFunc(f BuildVarsFunc) *Route { // // It will test the inner routes only if the parent route matched. For example: // -// r := mux.NewRouter() -// s := r.Host("www.example.com").Subrouter() -// s.HandleFunc("/products/", ProductsHandler) -// s.HandleFunc("/products/{key}", ProductHandler) -// s.HandleFunc("/articles/{category}/{id:[0-9]+}"), ArticleHandler) +// r := mux.NewRouter() +// s := r.Host("www.example.com").Subrouter() +// s.HandleFunc("/products/", ProductsHandler) +// s.HandleFunc("/products/{key}", ProductHandler) +// s.HandleFunc("/articles/{category}/{id:[0-9]+}"), ArticleHandler) // // Here, the routes registered in the subrouter won't be tested if the host // doesn't match. @@ -497,36 +497,36 @@ func (r *Route) Subrouter() *Router { // It accepts a sequence of key/value pairs for the route variables. For // example, given this route: // -// r := mux.NewRouter() -// r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler). -// Name("article") +// r := mux.NewRouter() +// r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler). +// Name("article") // // ...a URL for it can be built using: // -// url, err := r.Get("article").URL("category", "technology", "id", "42") +// url, err := r.Get("article").URL("category", "technology", "id", "42") // // ...which will return an url.URL with the following path: // -// "/articles/technology/42" +// "/articles/technology/42" // // This also works for host variables: // -// r := mux.NewRouter() -// r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler). -// Host("{subdomain}.domain.com"). -// Name("article") +// r := mux.NewRouter() +// r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler). +// Host("{subdomain}.domain.com"). +// Name("article") // -// // url.String() will be "http://news.domain.com/articles/technology/42" -// url, err := r.Get("article").URL("subdomain", "news", -// "category", "technology", -// "id", "42") +// // url.String() will be "http://news.domain.com/articles/technology/42" +// url, err := r.Get("article").URL("subdomain", "news", +// "category", "technology", +// "id", "42") // // The scheme of the resulting url will be the first argument that was passed to Schemes: // -// // url.String() will be "https://example.com" -// r := mux.NewRouter() -// url, err := r.Host("example.com") -// .Schemes("https", "http").URL() +// // url.String() will be "https://example.com" +// r := mux.NewRouter() +// url, err := r.Host("example.com") +// .Schemes("https", "http").URL() // // All variables defined in the route are required, and their values must // conform to the corresponding patterns. @@ -697,7 +697,7 @@ func (r *Route) GetMethods() ([]string, error) { } for _, m := range r.matchers { if methods, ok := m.(methodMatcher); ok { - return []string(methods), nil + return methods, nil } } return nil, errors.New("mux: route doesn't have methods")