GithubHelp home page GithubHelp logo

hostrouter's Introduction

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
}

hostrouter's People

Contributors

bobheadxi avatar pkieltyka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hostrouter's Issues

Allow custom NotFound handler

I would like to use a custom handler for unmatched requests, so we can track metrics and return a custom error message. This is currently not supported, since hostrouter always invokes http.Error:

http.Error(w, http.StatusText(404), 404)

It would be very nice if we could set a custom handler for that case, similar to chi's NotFound hook

Default route does not work.

I created a hostrouter like in the readme.

	r := chi.NewRouter()
	r.Get("/test", func(writer http.ResponseWriter, request *http.Request) {
          //...
	})

	hr := hostrouter.New()
	hr.Map("", r) // default

But always got 404s. Changed it to wildcard (hr.Map("*", r) ) and it worked. This took my quite some time to figure out, don't know if I'm missing something or not. But after inspecting the hostrouter .ServerHttp function, I suspect that is no "default" at all?

Maybe hr.Map("", apiRouter()) // default should be removed from the readme, or if I'm missing something then maybe update the readme to make it clearer?

Cannot use Routes literal

go get -u github.com/go-chi/hostrouter

gives me

go\src\github.com\go-chi\hostrouter\hostrouter.go:12:5: cannot use Routes literal (type Routes) as type chi.Routes in assignment:
        Routes does not implement chi.Routes (missing Match method)

Add Port support?

As is documented in go doc http.Request.Host, the req.Host can consist of the form "host:port", but your code does not handle this scenario properly.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.