Reputation: 141
Is there a way to handle listening for multiple Go web applications on a single port (80, for example). I am aware of ServeMux and the ability to listen for different incoming host names, but in this method they must be handled in the same program, and thus the same binary.
Would the best method be to listen for hostnames on :80 in one binary and then send the requests/response writers to another corresponding binary somewhere else? Would I use "os/exec"
for this? How would you pass in the Request
and ResponseWriter
parameters to this external binary? Thanks in advance!
EDIT:
Is it possible for goroutines of different binary origin to access each others channels? That would be a cool way to do it.
Upvotes: 3
Views: 538
Reputation: 43949
The usual method for doing this would be to use a reverse proxy that directs requests to relevant app servers (usually running on a different port or different machine) based on the host name in the request.
A common approach is to use Apache for this, but if you want to do it from Go, the ReverseProxy
type from the net/http/httputil
package should help.
httputil.NewSingleHostReverseProxy(baseurl)
will give you an HTTP handler that proxies requests through to another web site and returns the results. So you could implement your front end via a multiplexing HTTP handler that directs requests to one of a number of ReverseProxy
handlers based on the requested host name.
If you need more complicated routing than NewSingleHostReverseProxy
gives you, you can use a custom Director
function when creating the proxy handler.
Upvotes: 5