0
Fork 0
mirror of https://github.com/willnorris/imageproxy.git synced 2024-12-30 22:34:18 -05:00

switch from glog to standard log library

add "-verbose" flag for more logging.

fixes #105
This commit is contained in:
Will Norris 2017-08-30 05:21:56 +00:00
parent 9654679655
commit 7338ef68ef
2 changed files with 30 additions and 11 deletions

View file

@ -44,6 +44,7 @@ var cache = flag.String("cache", "", "location to cache images (see https://gith
var signatureKey = flag.String("signatureKey", "", "HMAC key used in calculating request signatures") var signatureKey = flag.String("signatureKey", "", "HMAC key used in calculating request signatures")
var scaleUp = flag.Bool("scaleUp", false, "allow images to scale beyond their original dimensions") var scaleUp = flag.Bool("scaleUp", false, "allow images to scale beyond their original dimensions")
var timeout = flag.Duration("timeout", 0, "time limit for requests served by this proxy") var timeout = flag.Duration("timeout", 0, "time limit for requests served by this proxy")
var verbose = flag.Bool("verbose", false, "print verbose logging messages")
var version = flag.Bool("version", false, "Deprecated: this flag does nothing") var version = flag.Bool("version", false, "Deprecated: this flag does nothing")
func main() { func main() {
@ -83,6 +84,7 @@ func main() {
p.Timeout = *timeout p.Timeout = *timeout
p.ScaleUp = *scaleUp p.ScaleUp = *scaleUp
p.Verbose = *verbose
server := &http.Server{ server := &http.Server{
Addr: *addr, Addr: *addr,

View file

@ -25,12 +25,12 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
"time" "time"
"github.com/golang/glog"
"github.com/gregjones/httpcache" "github.com/gregjones/httpcache"
tphttp "willnorris.com/go/imageproxy/third_party/http" tphttp "willnorris.com/go/imageproxy/third_party/http"
) )
@ -64,6 +64,9 @@ type Proxy struct {
// If a call runs for longer than its time limit, a 504 Gateway Timeout // If a call runs for longer than its time limit, a 504 Gateway Timeout
// response is returned. A Timeout of zero means no timeout. // response is returned. A Timeout of zero means no timeout.
Timeout time.Duration Timeout time.Duration
// If true, log additional debug messages
Verbose bool
} }
// NewProxy constructs a new proxy. The provided http RoundTripper will be // NewProxy constructs a new proxy. The provided http RoundTripper will be
@ -77,20 +80,28 @@ func NewProxy(transport http.RoundTripper, cache Cache) *Proxy {
cache = NopCache cache = NopCache
} }
proxy := Proxy{ proxy := &Proxy{
Cache: cache, Cache: cache,
} }
client := new(http.Client) client := new(http.Client)
client.Transport = &httpcache.Transport{ client.Transport = &httpcache.Transport{
Transport: &TransformingTransport{transport, client}, Transport: &TransformingTransport{
Transport: transport,
CachingClient: client,
log: func(format string, v ...interface{}) {
if proxy.Verbose {
log.Printf(format, v...)
}
},
},
Cache: cache, Cache: cache,
MarkCachedResponses: true, MarkCachedResponses: true,
} }
proxy.Client = client proxy.Client = client
return &proxy return proxy
} }
// ServeHTTP handles incoming requests. // ServeHTTP handles incoming requests.
@ -116,7 +127,7 @@ func (p *Proxy) serveImage(w http.ResponseWriter, r *http.Request) {
req, err := NewRequest(r, p.DefaultBaseURL) req, err := NewRequest(r, p.DefaultBaseURL)
if err != nil { if err != nil {
msg := fmt.Sprintf("invalid request URL: %v", err) msg := fmt.Sprintf("invalid request URL: %v", err)
glog.Error(msg) log.Print(msg)
http.Error(w, msg, http.StatusBadRequest) http.Error(w, msg, http.StatusBadRequest)
return return
} }
@ -125,7 +136,7 @@ func (p *Proxy) serveImage(w http.ResponseWriter, r *http.Request) {
req.Options.ScaleUp = p.ScaleUp req.Options.ScaleUp = p.ScaleUp
if err := p.allowed(req); err != nil { if err := p.allowed(req); err != nil {
glog.Error(err) log.Print(err)
http.Error(w, err.Error(), http.StatusForbidden) http.Error(w, err.Error(), http.StatusForbidden)
return return
} }
@ -133,14 +144,16 @@ func (p *Proxy) serveImage(w http.ResponseWriter, r *http.Request) {
resp, err := p.Client.Get(req.String()) resp, err := p.Client.Get(req.String())
if err != nil { if err != nil {
msg := fmt.Sprintf("error fetching remote image: %v", err) msg := fmt.Sprintf("error fetching remote image: %v", err)
glog.Error(msg) log.Print(msg)
http.Error(w, msg, http.StatusInternalServerError) http.Error(w, msg, http.StatusInternalServerError)
return return
} }
defer resp.Body.Close() defer resp.Body.Close()
cached := resp.Header.Get(httpcache.XFromCache) cached := resp.Header.Get(httpcache.XFromCache)
glog.Infof("request: %v (served from cache: %v)", *req, cached == "1") if p.Verbose {
log.Printf("request: %v (served from cache: %v)", *req, cached == "1")
}
copyHeader(w.Header(), resp.Header, "Cache-Control", "Last-Modified", "Expires", "Etag", "Link") copyHeader(w.Header(), resp.Header, "Cache-Control", "Last-Modified", "Expires", "Etag", "Link")
@ -231,7 +244,7 @@ func validSignature(key []byte, r *Request) bool {
got, err := base64.URLEncoding.DecodeString(sig) got, err := base64.URLEncoding.DecodeString(sig)
if err != nil { if err != nil {
glog.Errorf("error base64 decoding signature %q", r.Options.Signature) log.Printf("error base64 decoding signature %q", r.Options.Signature)
return false return false
} }
@ -281,13 +294,17 @@ type TransformingTransport struct {
// used rather than Transport directly in order to ensure that // used rather than Transport directly in order to ensure that
// responses are properly cached. // responses are properly cached.
CachingClient *http.Client CachingClient *http.Client
log func(format string, v ...interface{})
} }
// RoundTrip implements the http.RoundTripper interface. // RoundTrip implements the http.RoundTripper interface.
func (t *TransformingTransport) RoundTrip(req *http.Request) (*http.Response, error) { func (t *TransformingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if req.URL.Fragment == "" { if req.URL.Fragment == "" {
// normal requests pass through // normal requests pass through
glog.Infof("fetching remote URL: %v", req.URL) if t.log != nil {
t.log("fetching remote URL: %v", req.URL)
}
return t.Transport.RoundTrip(req) return t.Transport.RoundTrip(req)
} }
@ -313,7 +330,7 @@ func (t *TransformingTransport) RoundTrip(req *http.Request) (*http.Response, er
img, err := Transform(b, opt) img, err := Transform(b, opt)
if err != nil { if err != nil {
glog.Errorf("error transforming image: %v", err) log.Printf("error transforming image: %v", err)
img = b img = b
} }