38 lines
801 B
Go
38 lines
801 B
Go
package router
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
// Router wraps the mux.Router to provide additional functionality.
|
|
type Router struct {
|
|
*mux.Router
|
|
}
|
|
|
|
// HostRouter associates a host with a mux.Router.
|
|
type HostRouter struct {
|
|
Host string
|
|
*mux.Router
|
|
}
|
|
|
|
// NewRouter creates and returns a new Router with strict slash behavior.
|
|
func NewRouter() *Router {
|
|
r := &Router{}
|
|
r.Router = mux.NewRouter().StrictSlash(true)
|
|
return r
|
|
}
|
|
|
|
func (r *HostRouter) Handler(handler http.Handler) *mux.Route {
|
|
return r.Router.NewRoute().Handler(handler)
|
|
}
|
|
|
|
// NewHostRouter creates a new HostRouter for a specific host.
|
|
func (r *Router) NewHostRouter(host string) *HostRouter {
|
|
hostRouter := r.NewRoute().Host(host).Subrouter()
|
|
return &HostRouter{
|
|
Host: host,
|
|
Router: hostRouter,
|
|
}
|
|
}
|