From 566adfae24bc26a444918df570f90a0ba63c669a Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 3 Apr 2016 20:22:40 +0200 Subject: Shorten handler --- server.go | 17 +++++++++++++---- 1 file 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)) } } -- cgit v1.2.3