You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
hostrouter is a small Go pkg to let you route traffic to different http handlers or routers
based on the request host. This is useful to mount multiple routers on a single server.
Basic usage example
//...funcmain() {
r:=chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
hr:=hostrouter.New()
// Requests to api.domain.comhr.Map("", apiRouter()) // defaulthr.Map("api.domain.com", apiRouter())
// Requests to doma.inhr.Map("doma.in", shortUrlRouter())
// Requests to *.doma.inhr.Map("*.doma.in", shortUrlRouter())
// Requests to host that isn't defined abovehr.Map("*", everythingElseRouter())
// Mount the host routerr.Mount("/", hr)
http.ListenAndServe(":3333", r)
}
// Router for the API servicefuncapiRouter() chi.Router {
r:=chi.NewRouter()
r.Get("/", apiIndexHandler)
// ...returnr
}
// Router for the Short URL servicefuncshortUrlRouter() chi.Router {
r:=chi.NewRouter()
r.Get("/", shortIndexHandler)
// ...returnr
}
About
Little package to map hosts to a variety of http routers for Go API services