go-router/router.go
2025-03-22 09:00:38 +00:00

50 lines
988 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 DomainRouter struct {
Domain 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 *DomainRouter) Handler(handler http.Handler) *mux.Route {
return r.Router.NewRoute().Handler(handler)
}
// NewDomainRouter creates a new DomainRouter for a specific host.
func (r *Router) NewHostRouter(domain string, rule string) *DomainRouter {
domainRouter := &DomainRouter{
Domain: "",
Router: nil,
}
switch rule {
case "Domain":
{
domainRouter.Router = r.NewRoute().Host(domain).Subrouter()
}
default:
{
domainRouter.Router = r.NewRoute().Host(domain).Subrouter()
}
}
return domainRouter
}