Fixes #323. Also removed the duplicate "listing routes" example.
This commit is contained in:
committed by
Matt Silverlock
parent
3dbb9ed96e
commit
dc83507598
145
README.md
145
README.md
@@ -180,64 +180,6 @@ s.HandleFunc("/{key}/", ProductHandler)
|
|||||||
s.HandleFunc("/{key}/details", ProductDetailsHandler)
|
s.HandleFunc("/{key}/details", ProductDetailsHandler)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Listing Routes
|
|
||||||
|
|
||||||
Routes on a mux can be listed using the Router.Walk method—useful for generating documentation:
|
|
||||||
|
|
||||||
```go
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
)
|
|
||||||
|
|
||||||
func handler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
r := mux.NewRouter()
|
|
||||||
r.HandleFunc("/", handler)
|
|
||||||
r.HandleFunc("/products", handler).Methods("POST")
|
|
||||||
r.HandleFunc("/articles", handler).Methods("GET")
|
|
||||||
r.HandleFunc("/articles/{id}", handler).Methods("GET", "PUT")
|
|
||||||
r.HandleFunc("/authors", handler).Queries("surname", "{surname}")
|
|
||||||
r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
|
|
||||||
t, err := route.GetPathTemplate()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
qt, err := route.GetQueriesTemplates()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// p will contain regular expression is compatible with regular expression in Perl, Python, and other languages.
|
|
||||||
// for instance the regular expression for path '/articles/{id}' will be '^/articles/(?P<v0>[^/]+)$'
|
|
||||||
p, err := route.GetPathRegexp()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// qr will contain a list of regular expressions with the same semantics as GetPathRegexp,
|
|
||||||
// just applied to the Queries pairs instead, e.g., 'Queries("surname", "{surname}") will return
|
|
||||||
// {"^surname=(?P<v0>.*)$}. Where each combined query pair will have an entry in the list.
|
|
||||||
qr, err := route.GetQueriesRegexp()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
m, err := route.GetMethods()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
fmt.Println(strings.Join(m, ","), strings.Join(qt, ","), strings.Join(qr, ","), t, p)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
http.Handle("/", r)
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Static Files
|
### Static Files
|
||||||
|
|
||||||
@@ -350,41 +292,58 @@ The `Walk` function on `mux.Router` can be used to visit all of the routes that
|
|||||||
the following prints all of the registered routes:
|
the following prints all of the registered routes:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
r := mux.NewRouter()
|
package main
|
||||||
r.HandleFunc("/", handler)
|
|
||||||
r.HandleFunc("/products", handler).Methods("POST")
|
import (
|
||||||
r.HandleFunc("/articles", handler).Methods("GET")
|
"fmt"
|
||||||
r.HandleFunc("/articles/{id}", handler).Methods("GET", "PUT")
|
"net/http"
|
||||||
r.HandleFunc("/authors", handler).Queries("surname", "{surname}")
|
"strings"
|
||||||
r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
|
|
||||||
t, err := route.GetPathTemplate()
|
"github.com/gorilla/mux"
|
||||||
if err != nil {
|
)
|
||||||
return err
|
|
||||||
}
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
qt, err := route.GetQueriesTemplates()
|
return
|
||||||
if err != nil {
|
}
|
||||||
return err
|
|
||||||
}
|
func main() {
|
||||||
// p will contain a regular expression that is compatible with regular expressions in Perl, Python, and other languages.
|
r := mux.NewRouter()
|
||||||
// For example, the regular expression for path '/articles/{id}' will be '^/articles/(?P<v0>[^/]+)$'.
|
r.HandleFunc("/", handler)
|
||||||
p, err := route.GetPathRegexp()
|
r.HandleFunc("/products", handler).Methods("POST")
|
||||||
if err != nil {
|
r.HandleFunc("/articles", handler).Methods("GET")
|
||||||
return err
|
r.HandleFunc("/articles/{id}", handler).Methods("GET", "PUT")
|
||||||
}
|
r.HandleFunc("/authors", handler).Queries("surname", "{surname}")
|
||||||
// qr will contain a list of regular expressions with the same semantics as GetPathRegexp,
|
err := r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
|
||||||
// just applied to the Queries pairs instead, e.g., 'Queries("surname", "{surname}") will return
|
pathTemplate, err := route.GetPathTemplate()
|
||||||
// {"^surname=(?P<v0>.*)$}. Where each combined query pair will have an entry in the list.
|
if err == nil {
|
||||||
qr, err := route.GetQueriesRegexp()
|
fmt.Println("ROUTE:", pathTemplate)
|
||||||
if err != nil {
|
}
|
||||||
return err
|
pathRegexp, err := route.GetPathRegexp()
|
||||||
}
|
if err == nil {
|
||||||
m, err := route.GetMethods()
|
fmt.Println("Path regexp:", pathRegexp)
|
||||||
if err != nil {
|
}
|
||||||
return err
|
queriesTemplates, err := route.GetQueriesTemplates()
|
||||||
}
|
if err == nil {
|
||||||
fmt.Println(strings.Join(m, ","), strings.Join(qt, ","), strings.Join(qr, ","), t, p)
|
fmt.Println("Queries templates:", strings.Join(queriesTemplates, ","))
|
||||||
return nil
|
}
|
||||||
})
|
queriesRegexps, err := route.GetQueriesRegexp()
|
||||||
|
if err == nil {
|
||||||
|
fmt.Println("Queries regexps:", strings.Join(queriesRegexps, ","))
|
||||||
|
}
|
||||||
|
methods, err := route.GetMethods()
|
||||||
|
if err == nil {
|
||||||
|
fmt.Println("Methods:", strings.Join(methods, ","))
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Handle("/", r)
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Graceful Shutdown
|
### Graceful Shutdown
|
||||||
|
|||||||
Reference in New Issue
Block a user