Skip to content
Little package to map hosts to a variety of http routers for Go API services
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE Update import path and LICENSE Aug 9, 2017 Fix typos and implement match function Oct 22, 2017
hostrouter.go Fix typos and implement match function Oct 22, 2017


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

func main() {
  r := chi.NewRouter()


  hr := hostrouter.New()

  // Requests to
  hr.Map("", apiRouter()) // default
  hr.Map("", apiRouter())

  // Requests to
  hr.Map("", shortUrlRouter())

  // Requests to host that isn't defined above
  hr.Map("*", everythingElseRouter())

  // Mount the host router
  r.Mount("/", hr)

  http.ListenAndServe(":3333", r)

// Router for the API service
func apiRouter() chi.Router {
  r := chi.NewRouter()
  r.Get("/", apiIndexHandler)
  // ...
  return r

// Router for the Short URL service
func shortUrlRouter() chi.Router {
  r := chi.NewRouter()
  r.Get("/", shortIndexHandler)
  // ...
  return r
You can’t perform that action at this time.