Skip to content
Snippets Groups Projects
Commit 62a90409 authored by ale's avatar ale
Browse files

run the gzip handler on the status page

parent 2ded47fb
No related branches found
No related tags found
No related merge requests found
...@@ -19,14 +19,6 @@ import ( ...@@ -19,14 +19,6 @@ import (
) )
// HTTP redirector. // 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 { type HttpRedirector struct {
domain string domain string
client *radioai.RadioAPI client *radioai.RadioAPI
...@@ -40,7 +32,9 @@ func NewHttpRedirector(client *radioai.RadioAPI, domain string) *HttpRedirector ...@@ -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 { func (h *HttpRedirector) pickActiveNode() string {
nodes, _ := h.client.GetNodes() nodes, _ := h.client.GetNodes()
if nodes != nil && len(nodes) > 0 { if nodes != nil && len(nodes) > 0 {
...@@ -126,12 +120,20 @@ func (h *HttpRedirector) serveStatusPage(w http.ResponseWriter, r *http.Request) ...@@ -126,12 +120,20 @@ func (h *HttpRedirector) serveStatusPage(w http.ResponseWriter, r *http.Request)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Content-Length", strconv.Itoa(buf.Len()))
w.Write(buf.Bytes()) w.Write(buf.Bytes())
} }
func (h *HttpRedirector) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *HttpRedirector) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "" || r.URL.Path == "/" { 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" { } else if r.Method == "SOURCE" {
h.serveSource(w, r) h.serveSource(w, r)
} else { } else {
...@@ -139,11 +141,14 @@ func (h *HttpRedirector) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -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) { func (h *HttpRedirector) Run(addr, staticDir, templateDir string) {
h.template = template.Must( h.template = template.Must(
template.ParseGlob( template.ParseGlob(
filepath.Join(templateDir, "*.html"))) 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 := http.NewServeMux()
mux.HandleFunc( mux.HandleFunc(
"/static/", "/static/",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment