-
-
Save camoles/523dac8cc0fe40d52f66 to your computer and use it in GitHub Desktop.
Virtual Hosts in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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