aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-04-03 20:22:40 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-04-03 20:22:40 +0200
commit566adfae24bc26a444918df570f90a0ba63c669a (patch)
tree4bb39e21d93c772e8aad1a4018c343dc1cc1031f
parentd548648e11fd5e1a3a43877b53e63325be1bc159 (diff)
Shorten handler
-rw-r--r--server.go17
1 files changed, 13 insertions, 4 deletions
diff --git a/server.go b/server.go
index d10c615..271edfa 100644
--- a/server.go
+++ b/server.go
@@ -4,6 +4,7 @@ import (
"crypto/tls"
"net/http"
"net/http/httputil"
+ "net/url"
)
type Server struct {
@@ -44,6 +45,14 @@ func NewServer(dataFile, listenWWW, listenTLS, listenRPC string) (*Server, error
return server, server.Update()
}
+func NewRedirect(host string) http.Handler {
+ return http.RedirectHandler(host, http.StatusMovedPermanently)
+}
+
+func NewReverseProxy(target *url.URL) *httputil.ReverseProxy {
+ return httputil.NewSingleHostReverseProxy(target)
+}
+
// Update routes from in-memory state
func (s *Server) Update() error {
wwwMux := http.NewServeMux()
@@ -53,14 +62,14 @@ func (s *Server) Update() error {
up := v.Upstream
switch v.ServerName.Scheme {
case "http", "":
- wwwMux.Handle(host, httputil.NewSingleHostReverseProxy(up))
+ wwwMux.Handle(host, NewReverseProxy(up))
case "https":
- wwwMux.Handle(host, http.RedirectHandler("https://"+host, http.StatusMovedPermanently))
- tlsMux.Handle(host, httputil.NewSingleHostReverseProxy(up))
+ wwwMux.Handle(host, NewRedirect("https://"+host))
+ tlsMux.Handle(host, NewReverseProxy(up))
case "ws":
wwwMux.Handle(host, NewWebSocketProxy(up))
case "wss":
- wwwMux.Handle(host, http.RedirectHandler("wss://"+host, http.StatusMovedPermanently))
+ wwwMux.Handle(host, NewRedirect("wss://"+host))
tlsMux.Handle(host, NewWebSocketProxy(up))
}
}