1st
This commit is contained in:
commit
650cd1d241
3 changed files with 45 additions and 0 deletions
5
go.mod
Normal file
5
go.mod
Normal 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
2
go.sum
Normal 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
38
router.go
Normal 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,
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue