Skip to content

Commit

Permalink
fix major bug - the log handler was taking over all requests
Browse files Browse the repository at this point in the history
  • Loading branch information
tombowditch committed Feb 21, 2018
1 parent 2c42c44 commit 5c69fd4
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func log(level string, msg string) {
fmt.Println("[telly] [" + level + "] " + msg)
}

func logRequestHandler() http.Handler {
func logRequestHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if *logRequests {
log("request", r.RemoteAddr+" -> "+r.Method+" "+r.RequestURI)
Expand All @@ -79,6 +79,9 @@ func logRequestHandler() http.Handler {
log("request", "POST body: "+r.Form.Encode())
}
}

next.ServeHTTP(w, r)

})
}

Expand Down Expand Up @@ -269,38 +272,40 @@ func main() {

log("info", "creating webserver routes")

http.HandleFunc("/discover.json", func(w http.ResponseWriter, r *http.Request) {
h := http.NewServeMux()

h.HandleFunc("/discover.json", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(discoveryData)
})

http.HandleFunc("/lineup_status.json", func(w http.ResponseWriter, r *http.Request) {
h.HandleFunc("/lineup_status.json", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(lineupStatus)
})

http.HandleFunc("/lineup.post", func(w http.ResponseWriter, r *http.Request) {
h.HandleFunc("/lineup.post", func(w http.ResponseWriter, r *http.Request) {
// empty
})

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
h.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/xml")
w.Write([]byte(deviceXml))
})

http.HandleFunc("/device.xml", func(w http.ResponseWriter, r *http.Request) {
h.HandleFunc("/device.xml", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/xml")
w.Write([]byte(deviceXml))
})

log("info", "Building lineup")
lineupItems := buildChannels(usedTracks, showNameRegex)

http.HandleFunc("/lineup.json", func(w http.ResponseWriter, r *http.Request) {
h.HandleFunc("/lineup.json", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(lineupItems)

})

log("info", "listening on "+*listenAddress)
if err := http.ListenAndServe(*listenAddress, logRequestHandler()); err != nil {
if err := http.ListenAndServe(*listenAddress, logRequestHandler(h)); err != nil {
log("error", err.Error())
os.Exit(1)
}
Expand Down

0 comments on commit 5c69fd4

Please sign in to comment.