commit 650cd1d241f55873b562f9da9642bc6f567adea1 Author: Zeev Diukman Date: Wed Mar 5 09:31:30 2025 +0000 1st diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..430bda0 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/zeevdiukman/go-router + +go 1.24.0 + +require github.com/gorilla/mux v1.8.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7128337 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= diff --git a/router.go b/router.go new file mode 100644 index 0000000..8cd913a --- /dev/null +++ b/router.go @@ -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, + } +}