nytimes / gziphandler

Go middleware to gzip HTTP responses
https://godoc.org/github.com/NYTimes/gziphandler
Apache License 2.0
868 stars 129 forks source link

Enhance handler customization. #50

Closed ldez closed 7 years ago

ldez commented 7 years ago

I would propose to you this PR fixing #43, #40, #34:

This changes:

example ```golang package gziphandler import ( "bufio" "log" "net" "net/http" ) type GzipResponseWriterLogger struct { GzipResponseWriter } func (g *GzipResponseWriterLogger) Write(b []byte) (int, error) { log.Printf("Write: %v\n", b) log.Printf("ResponseWriter Header: %v\n", g.ResponseWriter.Header()) return g.GzipResponseWriter.Write(b) } func (g *GzipResponseWriterLogger) WriteHeader(code int) { log.Printf("WriteHeader: %v\n", code) g.GzipResponseWriter.WriteHeader(code) } func (g *GzipResponseWriterLogger) Hijack() (net.Conn, *bufio.ReadWriter, error) { log.Println("Hijack") return g.GzipResponseWriter.Hijack() } func (g *GzipResponseWriterLogger) Close() error { log.Println("Close") return g.GzipResponseWriter.Close() } func (g *GzipResponseWriterLogger) Header() http.Header { log.Println("Header") return g.GzipResponseWriter.Header() } func GzipHandlerLogger(h http.Handler) http.Handler { wrapper, _ := NewHandler(&GzipResponseWriterLogger{}, CompressionLevel(2), MinSize(1024)) return wrapper(h) } ```

Fixes #43, #40, #34

jprobinson commented 7 years ago

Hi there, @ldez! Sorry for the delay, I was at GopherCon last week.

While we're interested in using functional options for this package, I'm not quite sure the addition of this interface is really needed. Do you think you could implement this without it?

jprobinson commented 7 years ago

Sorry, meant to just comment, not close!

ldez commented 7 years ago

The interface GzipWriter (the name is not the best but GzipResponseWriter is already used) allow to add some custom behaviors without needing to add them to this middleware.

I'm not really happy what I did with this interface (unexported methods) but I don't see a cleaner way to do that, if you have an idea I can implement.

But if you think it's not a interesting idea, I can drop this part.

WDYT?

ldez commented 7 years ago

With the merge of #51, functional options have been added. And without any response I suppose you don't want my PR.