diff --git a/example/main.go b/example/main.go index 8e2a2f4..dcd7962 100644 --- a/example/main.go +++ b/example/main.go @@ -8,6 +8,10 @@ import ( func main() { router := gin.Default() + options := ginzip.DefaultOptions() + options.BrotliLevel = "" + options.GzipLevel = "" + ui := router.Group("/", ginzip.New(ginzip.DefaultOptions())) ui.GET("/", getTestHandler("Hello World (should be compressed)")) diff --git a/ginzip.go b/ginzip.go index 965ece6..a3fa2d7 100644 --- a/ginzip.go +++ b/ginzip.go @@ -33,7 +33,7 @@ type intOptions struct { GzipLevel int BrotliEn bool BrotliLevel int - SkipExtensions map[string]bool + SkipExtensions []string } // New creates a new Ginzip middleware function. @@ -136,7 +136,16 @@ func (b *brotliWriter) Encoding() string { return "br" } -func shouldCompress(req *http.Request, skipExtensions map[string]bool) bool { +func containsString(s []string, e string) bool { + for _, a := range s { + if a == e { + return true + } + } + return false +} + +func shouldCompress(req *http.Request, skipExtensions []string) bool { // Dont compress websocket connections if strings.Contains(req.Header.Get("Connection"), "Upgrade") || strings.Contains(req.Header.Get("Content-Type"), "text/event-stream") { @@ -148,8 +157,7 @@ func shouldCompress(req *http.Request, skipExtensions map[string]bool) bool { return true } - _, doSkip := skipExtensions[extension] - return !doSkip + return !containsString(skipExtensions, extension) } func parseLevel(level string, dfault int, min int, max int) (int, bool) { @@ -185,17 +193,12 @@ func parseOptions(options Options) intOptions { brotli.DefaultCompression, brotli.BestSpeed, brotli.BestCompression, ) - exts := map[string]bool{} - for _, ext := range options.SkipExtensions { - exts[ext] = true - } - return intOptions{ GzipEn: gzipEn, GzipLevel: gzipLvl, BrotliEn: brEn, BrotliLevel: brLvl, - SkipExtensions: exts, + SkipExtensions: options.SkipExtensions, } }