Add support for websocket proxying

This commit is contained in:
Jonathan Leibiusky @xetorthio
2017-02-14 15:41:03 -03:00
parent 640d64c3aa
commit 9c4df837a8
2 changed files with 66 additions and 2 deletions

14
api.go
View File

@@ -16,6 +16,7 @@ import (
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/negroni"
"github.com/yhat/wsutil"
)
func main() {
@@ -37,10 +38,19 @@ func main() {
// Reverse proxy (needs to be the first route, to make sure it is the first thing we check)
proxyHandler := handlers.NewMultipleHostReverseProxy()
websocketProxyHandler := handlers.NewMultipleHostWebsocketReverseProxy()
proxyMultiplexer := func(rw http.ResponseWriter, r *http.Request) {
if wsutil.IsWebSocketRequest(r) {
websocketProxyHandler.ServeHTTP(rw, r)
} else {
proxyHandler.ServeHTTP(rw, r)
}
}
// Specific routes
r.Host(`{node:ip[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}}-{port:[0-9]*}.{tld:.*}`).Handler(proxyHandler)
r.Host(`{node:ip[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}}.{tld:.*}`).Handler(proxyHandler)
r.Host(`{node:ip[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}}-{port:[0-9]*}.{tld:.*}`).HandlerFunc(proxyMultiplexer)
r.Host(`{node:ip[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}}.{tld:.*}`).HandlerFunc(proxyMultiplexer)
r.HandleFunc("/ping", handlers.Ping).Methods("GET")
r.HandleFunc("/sessions/{sessionId}", handlers.GetSession).Methods("GET")
r.Handle("/sessions/{sessionId}/instances", http.HandlerFunc(handlers.NewInstance)).Methods("POST")