Skip to content

Instantly share code, notes, and snippets.

@camoles
Forked from JalfResi/revprox.go
Last active September 23, 2017 03:41
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camoles/523dac8cc0fe40d52f66 to your computer and use it in GitHub Desktop.
Save camoles/523dac8cc0fe40d52f66 to your computer and use it in GitHub Desktop.
Virtual Hosts in Go
package main
import(
"net/url"
"net/http"
"net/http/httputil"
)
func main() {
vhost1, err := url.Parse("http://127.0.0.1:1980")
if err != nil {
panic(err)
}
proxy1 := httputil.NewSingleHostReverseProxy(vhost1)
http.HandleFunc("publicdomain1/", handler(proxy1))
vhost2, err := url.Parse("http://127.0.0.1:1981")
if err != nil {
panic(err)
}
proxy2 := httputil.NewSingleHostReverseProxy(vhost2)
http.HandleFunc("publicdomain2/", handler(proxy2))
err = http.ListenAndServe(":80", nil)
if err != nil {
panic(err)
}
}
func handler(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
p.ServeHTTP(w, r)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment