Make URLPrefixer use the chronograf.Logger

We have a unified structured logging package in Chronograf, and this
should use it.
pull/814/head
Tim Raymond 2017-01-27 19:24:51 -05:00
parent a0ba920046
commit 31621b460d
2 changed files with 15 additions and 8 deletions

View File

@ -39,7 +39,7 @@ func NewMux(opts MuxOpts, service Service) http.Handler {
}) })
// Prefix any URLs found in the React assets with any configured basepath // Prefix any URLs found in the React assets with any configured basepath
prefixedAssets := NewDefaultURLPrefixer(basepath, assets) prefixedAssets := NewDefaultURLPrefixer(basepath, assets, opts.Logger)
// The react application handles all the routing if the server does not // The react application handles all the routing if the server does not
// know about the route. This means that we never have unknown // know about the route. This means that we never have unknown

View File

@ -4,15 +4,17 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"io" "io"
"log"
"net/http" "net/http"
"github.com/influxdata/chronograf"
) )
// URLPrefixer is a wrapper for an http.Handler that will prefix all occurrences of a relative URL with the configured Prefix // URLPrefixer is a wrapper for an http.Handler that will prefix all occurrences of a relative URL with the configured Prefix
type URLPrefixer struct { type URLPrefixer struct {
Prefix string // the prefix to be appended after any detected Attrs Prefix string // the prefix to be appended after any detected Attrs
Next http.Handler // the http.Handler which will generate the content to be modified by this handler Next http.Handler // the http.Handler which will generate the content to be modified by this handler
Attrs [][]byte // a list of attrs that should have their URLs prefixed. For example `src="` or `href="` would be valid Attrs [][]byte // a list of attrs that should have their URLs prefixed. For example `src="` or `href="` would be valid
Logger chronograf.Logger // The logger where prefixing errors will be dispatched to
} }
type wrapResponseWriter struct { type wrapResponseWriter struct {
@ -65,7 +67,9 @@ func (up *URLPrefixer) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
// extract the flusher for flushing chunks // extract the flusher for flushing chunks
flusher, ok := rw.(http.Flusher) flusher, ok := rw.(http.Flusher)
if !ok { if !ok {
log.Fatalln("Exected http.ResponseWriter to be an http.Flusher, but wasn't") up.Logger.
WithField("component", "prefixer").
Fatal("Exected http.ResponseWriter to be an http.Flusher, but wasn't")
} }
nextRead, nextWrite := io.Pipe() nextRead, nextWrite := io.Pipe()
@ -99,7 +103,9 @@ func (up *URLPrefixer) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
} }
} else { } else {
if err := src.Err(); err != nil { if err := src.Err(); err != nil {
log.Println("Error encountered while scanning: err:", err) up.Logger.
WithField("component", "prefixer").
Error("Error encountered while scanning: err:", err)
} }
rw.Write(window) rw.Write(window)
flusher.Flush() flusher.Flush()
@ -147,10 +153,11 @@ func (up *URLPrefixer) maxlen(targets ...[]byte) int {
// with the provided prefix. Additionally, it will prefix any `data-basepath` // with the provided prefix. Additionally, it will prefix any `data-basepath`
// attributes as well for informing front end logic about any prefixes. `next` // attributes as well for informing front end logic about any prefixes. `next`
// is the next http.Handler that will have its output prefixed // is the next http.Handler that will have its output prefixed
func NewDefaultURLPrefixer(prefix string, next http.Handler) *URLPrefixer { func NewDefaultURLPrefixer(prefix string, next http.Handler, lg chronograf.Logger) *URLPrefixer {
return &URLPrefixer{ return &URLPrefixer{
Prefix: prefix, Prefix: prefix,
Next: next, Next: next,
Logger: lg,
Attrs: [][]byte{ Attrs: [][]byte{
[]byte(`src="`), []byte(`src="`),
[]byte(`href="`), []byte(`href="`),