Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/go-chi/hostrouter
Little package to map hosts to a variety of http routers for Go API services
https://github.com/go-chi/hostrouter
Last synced: about 1 month ago
JSON representation
Little package to map hosts to a variety of http routers for Go API services
- Host: GitHub
- URL: https://github.com/go-chi/hostrouter
- Owner: go-chi
- License: mit
- Created: 2016-10-27T17:37:38.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2021-02-28T21:46:12.000Z (almost 4 years ago)
- Last Synced: 2024-11-04T14:45:18.075Z (about 1 month ago)
- Language: Go
- Size: 5.86 KB
- Stars: 69
- Watchers: 8
- Forks: 13
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# HostRouter
`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
```go
//...
func main() {
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.com
hr.Map("", apiRouter()) // default
hr.Map("api.domain.com", apiRouter())// Requests to doma.in
hr.Map("doma.in", shortUrlRouter())// Requests to *.doma.in
hr.Map("*.doma.in", 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
}
```