1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-07-14 01:57:32 +08:00

Allow to specify port, and remove session id.

So now the way to address a node is in the form of:
`ip10_0_1_9-9200.play-with-docker.com`
This commit is contained in:
Jonathan Leibiusky @xetorthio 2016-11-18 09:54:12 -03:00
parent ec9d34ffda
commit 122118b9df
2 changed files with 8 additions and 5 deletions

3
api.go
View File

@ -60,7 +60,8 @@ func main() {
r.Handle("/sessions/{sessionId}/ws/", server) r.Handle("/sessions/{sessionId}/ws/", server)
// Reverse proxy // Reverse proxy
r.Host(`{node}.{session}.play-with-docker.com`).Handler(handlers.NewMultipleHostReverseProxy()) r.Host(`{node}-{port:[0-9]*}.play-with-docker.com`).Handler(handlers.NewMultipleHostReverseProxy())
r.Host(`{node}.play-with-docker.com`).Handler(handlers.NewMultipleHostReverseProxy())
n := negroni.Classic() n := negroni.Classic()
n.UseHandler(r) n.UseHandler(r)

View File

@ -1,6 +1,7 @@
package handlers package handlers
import ( import (
"fmt"
"net" "net"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
@ -13,6 +14,10 @@ func NewMultipleHostReverseProxy() *httputil.ReverseProxy {
director := func(req *http.Request) { director := func(req *http.Request) {
v := mux.Vars(req) v := mux.Vars(req)
node := v["node"] node := v["node"]
port := v["port"]
if port == "" {
port = "80"
}
if strings.HasPrefix(node, "ip") { if strings.HasPrefix(node, "ip") {
// Node is actually an ip, need to convert underscores by dots. // Node is actually an ip, need to convert underscores by dots.
ip := strings.Replace(strings.TrimPrefix(node, "ip"), "_", ".", -1) ip := strings.Replace(strings.TrimPrefix(node, "ip"), "_", ".", -1)
@ -24,13 +29,10 @@ func NewMultipleHostReverseProxy() *httputil.ReverseProxy {
} }
} }
// Validate that the node actually exists in the network
// TODO:
// Only proxy http for now // Only proxy http for now
req.URL.Scheme = "http" req.URL.Scheme = "http"
req.URL.Host = node req.URL.Host = fmt.Sprintf("%s:%s", node, port)
} }
return &httputil.ReverseProxy{Director: director} return &httputil.ReverseProxy{Director: director}