diff --git a/fe/http.go b/fe/http.go index 17a089bfda585c6f3e6bd4d42ebc107ec101d04d..49a24368a548c5dc8b2e1c10e6c0682449dfc037 100644 --- a/fe/http.go +++ b/fe/http.go @@ -19,14 +19,6 @@ import ( ) // HTTP redirector. -// -// All user-facing traffic reaches the redirector first (this is -// where the first-level, high-ttl redirection points to). -// -// The purpose of the HTTP redirector is two-fold: sources will be -// proxied to the master icecast server, while clients will be served -// a .m3u file directly pointing at the relays. -// type HttpRedirector struct { domain string client *radioai.RadioAPI @@ -40,7 +32,9 @@ func NewHttpRedirector(client *radioai.RadioAPI, domain string) *HttpRedirector } } -// Return an active node, chosen randomly. +// Return an active node, chosen randomly (this is currently our load +// balancing policy, since there is no status information about the +// nodes yet). func (h *HttpRedirector) pickActiveNode() string { nodes, _ := h.client.GetNodes() if nodes != nil && len(nodes) > 0 { @@ -126,12 +120,20 @@ func (h *HttpRedirector) serveStatusPage(w http.ResponseWriter, r *http.Request) http.Error(w, err.Error(), http.StatusInternalServerError) return } + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.Header().Set("Content-Length", strconv.Itoa(buf.Len())) w.Write(buf.Bytes()) } func (h *HttpRedirector) ServeHTTP(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "" || r.URL.Path == "/" { - h.serveStatusPage(w, r) + // Serve the status page through a GZIPHandler. Binds + // to h using function closure. + handler := GZIPHandler( + http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h.serveStatusPage(w, r) + }), nil) + handler.ServeHTTP(w, r) } else if r.Method == "SOURCE" { h.serveSource(w, r) } else { @@ -139,11 +141,14 @@ func (h *HttpRedirector) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } +// Run starts the HTTP server on the given addr. Does not return. func (h *HttpRedirector) Run(addr, staticDir, templateDir string) { h.template = template.Must( template.ParseGlob( filepath.Join(templateDir, "*.html"))) + // The purpose of the odd usage of GZIPHandler is to bypass it + // on SOURCE and m3u requests. May not be necessary though. mux := http.NewServeMux() mux.HandleFunc( "/static/",