From 62a904093952c4a6765f28f8556153bb5c30c401 Mon Sep 17 00:00:00 2001 From: ale <ale@incal.net> Date: Sat, 16 Nov 2013 12:07:18 +0000 Subject: [PATCH] run the gzip handler on the status page --- fe/http.go | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/fe/http.go b/fe/http.go index 17a089bf..49a24368 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/", -- GitLab