I have a bunch of websites running on one server (single IP) and they all run on high ports so I don't need root to run them.
When someone visits from one web address, lets say, http://address001.com/, I want to seamlessly pipe the data from port 4444 to the person who made this request, and if someone visits http://address002.com/ I want to pipe the data from port 5555.
How would I do this in Go?
So far I have a handler function that looks like this:
func home(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.Host, "address001") {
// ???
}
}
you can use httputil's ReverseProxy
here's an example code
package main
import (
"log"
"net/http"
"net/http/httputil"
)
func main() {
director := func(req *http.Request) {
switch req.Host {
case "address001.com":
req.URL.Host = "localhost:4444"
req.URL.Scheme = "http"
case "address002.com":
req.URL.Host = "localhost:5555"
req.URL.Scheme = "http"
default:
log.Println("error")
}
}
proxy := &httputil.ReverseProxy{Director: director}
log.Fatalln(http.ListenAndServe(":8080", proxy))
}