Browse Source

Updated README

pull/212/merge
Bulat Gaifullin 9 years ago committed by Kamil Kisiel
parent
commit
751308a60a
  1. 19
      README.md

19
README.md

@ -179,6 +179,7 @@ package main
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"strings"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
@ -190,15 +191,25 @@ func handler(w http.ResponseWriter, r *http.Request) {
func main() { func main() {
r := mux.NewRouter() r := mux.NewRouter()
r.HandleFunc("/", handler) r.HandleFunc("/", handler)
r.HandleFunc("/products", handler) r.Methods("POST").HandleFunc("/products", handler)
r.HandleFunc("/articles", handler) r.Methods("GET").HandleFunc("/articles", handler)
r.HandleFunc("/articles/{id}", handler) r.Methods("GET", "PUT").HandleFunc("/articles/{id}", handler)
r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error { r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
t, err := route.GetPathTemplate() t, err := route.GetPathTemplate()
if err != nil { if err != nil {
return err return err
} }
fmt.Println(t) // 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
}
m, err := route.GetMethods()
if err != nil {
return err
}
fmt.Println(strings.Join(m, ","), t, p)
return nil return nil
}) })
http.Handle("/", r) http.Handle("/", r)

Loading…
Cancel
Save