mirror of
https://github.com/bingohuang/docker-labs.git
synced 2025-07-15 02:37:27 +08:00
So now the way to address a node is in the form of: `ip10_0_1_9-9200.play-with-docker.com`
40 lines
770 B
Go
40 lines
770 B
Go
package handlers
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httputil"
|
|
"strings"
|
|
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
func NewMultipleHostReverseProxy() *httputil.ReverseProxy {
|
|
director := func(req *http.Request) {
|
|
v := mux.Vars(req)
|
|
node := v["node"]
|
|
port := v["port"]
|
|
if port == "" {
|
|
port = "80"
|
|
}
|
|
if strings.HasPrefix(node, "ip") {
|
|
// Node is actually an ip, need to convert underscores by dots.
|
|
ip := strings.Replace(strings.TrimPrefix(node, "ip"), "_", ".", -1)
|
|
|
|
if net.ParseIP(ip) == nil {
|
|
// Not a valid IP, so treat this is a hostname.
|
|
} else {
|
|
node = ip
|
|
}
|
|
}
|
|
|
|
// Only proxy http for now
|
|
req.URL.Scheme = "http"
|
|
|
|
req.URL.Host = fmt.Sprintf("%s:%s", node, port)
|
|
}
|
|
|
|
return &httputil.ReverseProxy{Director: director}
|
|
}
|