Browse Source

Add Mount method to Route.

With Mount method, we can simplify subrouter with tidy code.

```
todos := r.PathPrefix("/todos").Subrouter()
todosResource{}.SubrouterFunc(todos)
```

```
r.Mount("/todos", todosResource{}.SubrouterFunc)
```


```
type todosResource struct{}

func (rs todosResource) SubrouterFunc(r *mux.Router) {
	r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		_, _ = fmt.Fprintln(w, "entry [/todos/].")
	})
	r.HandleFunc("/list", func(w http.ResponseWriter, r *http.Request) {
		_, _ = fmt.Fprintln(w, "entry [/todos/list].")
	})
}
```
pull/612/head
xiaoshuai 5 years ago committed by GitHub
parent
commit
8f58e460f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      route.go

6
route.go

@ -488,6 +488,12 @@ func (r *Route) Subrouter() *Router { @@ -488,6 +488,12 @@ func (r *Route) Subrouter() *Router {
return router
}
// Mount registers a subrouter for the route.
func (r *Router) Mount(prefix string, wrapFunc func(r *Router)) {
sub := r.PathPrefix(prefix).Subrouter()
wrapFunc(sub)
}
// ----------------------------------------------------------------------------
// URL building
// ----------------------------------------------------------------------------

Loading…
Cancel
Save