hostrouter

package module
v0.0.0-...-b9bb5df Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 3, 2019 License: MIT Imports: 3 Imported by: 0

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

//...
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
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Routes

type Routes map[string]chi.Router

func New

func New() Routes

func (Routes) Map

func (hr Routes) Map(host string, h chi.Router)

func (Routes) Match

func (hr Routes) Match(rctx *chi.Context, method, path string) bool

func (Routes) Middlewares

func (hr Routes) Middlewares() chi.Middlewares

func (Routes) Routes

func (hr Routes) Routes() []chi.Route

func (Routes) ServeHTTP

func (hr Routes) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (Routes) Unmap

func (hr Routes) Unmap(host string)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL