This commit is contained in:
Zeev Diukman 2025-03-05 09:31:30 +00:00
commit 650cd1d241
3 changed files with 45 additions and 0 deletions

5
go.mod Normal file
View file

@ -0,0 +1,5 @@
module github.com/zeevdiukman/go-router
go 1.24.0
require github.com/gorilla/mux v1.8.1

2
go.sum Normal file
View file

@ -0,0 +1,2 @@
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=

38
router.go Normal file
View file

@ -0,0 +1,38 @@
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,
}
}