diff --git a/node/http.go b/node/http.go index f4dc3866655638f467ceaebdcd6ba296c82e1a0b..2eb4908cf445ca035bc01a86de58486f4d3f0031 100644 --- a/node/http.go +++ b/node/http.go @@ -12,6 +12,7 @@ import ( "log" "net" "net/http" + "net/http/httputil" "net/url" "strconv" "strings" @@ -53,6 +54,25 @@ func newHTTPHandler(n *Node, icecastPort int, domain string) http.Handler { mux.Handle("/debug/", h) } + // A very narrow selection of Icecast API methods is served + // below /admin/ and forwarded to the Icecast master via a + // standard httputil.ReverseProxy. + adminHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + leaderAddr := n.leaderAddr() + if leaderAddr == "" { + http.Error(w, "No leader", http.StatusServiceUnavailable) + return + } + + rp := httputil.NewSingleHostReverseProxy(&url.URL{ + Scheme: "http", + Host: leaderAddr, + }) + rp.ServeHTTP(w, r) + }) + mux.Handle("/admin/metadata", adminHandler) + mux.Handle("/admin/killsource", adminHandler) + // Requests for /_stream/ go straight to the local Icecast. proxyHandler := http.StripPrefix(autoradio.IcecastMountPrefix, withMount(n, func(m *pb.Mount, w http.ResponseWriter, r *http.Request) {