Compare commits
2 commits
Author | SHA1 | Date | |
---|---|---|---|
05a4d62360 | |||
32a105e462 |
2 changed files with 10 additions and 17 deletions
|
@ -8,10 +8,6 @@ import (
|
||||||
func main() {
|
func main() {
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
|
|
||||||
options := ginzip.DefaultOptions()
|
|
||||||
options.BrotliLevel = ""
|
|
||||||
options.GzipLevel = ""
|
|
||||||
|
|
||||||
ui := router.Group("/", ginzip.New(ginzip.DefaultOptions()))
|
ui := router.Group("/", ginzip.New(ginzip.DefaultOptions()))
|
||||||
ui.GET("/", getTestHandler("Hello World (should be compressed)"))
|
ui.GET("/", getTestHandler("Hello World (should be compressed)"))
|
||||||
|
|
||||||
|
|
23
ginzip.go
23
ginzip.go
|
@ -33,7 +33,7 @@ type intOptions struct {
|
||||||
GzipLevel int
|
GzipLevel int
|
||||||
BrotliEn bool
|
BrotliEn bool
|
||||||
BrotliLevel int
|
BrotliLevel int
|
||||||
SkipExtensions []string
|
SkipExtensions map[string]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new Ginzip middleware function.
|
// New creates a new Ginzip middleware function.
|
||||||
|
@ -136,16 +136,7 @@ func (b *brotliWriter) Encoding() string {
|
||||||
return "br"
|
return "br"
|
||||||
}
|
}
|
||||||
|
|
||||||
func containsString(s []string, e string) bool {
|
func shouldCompress(req *http.Request, skipExtensions map[string]bool) bool {
|
||||||
for _, a := range s {
|
|
||||||
if a == e {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func shouldCompress(req *http.Request, skipExtensions []string) bool {
|
|
||||||
// Dont compress websocket connections
|
// Dont compress websocket connections
|
||||||
if strings.Contains(req.Header.Get("Connection"), "Upgrade") ||
|
if strings.Contains(req.Header.Get("Connection"), "Upgrade") ||
|
||||||
strings.Contains(req.Header.Get("Content-Type"), "text/event-stream") {
|
strings.Contains(req.Header.Get("Content-Type"), "text/event-stream") {
|
||||||
|
@ -157,7 +148,8 @@ func shouldCompress(req *http.Request, skipExtensions []string) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
return !containsString(skipExtensions, extension)
|
_, doSkip := skipExtensions[extension]
|
||||||
|
return !doSkip
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseLevel(level string, dfault int, min int, max int) (int, bool) {
|
func parseLevel(level string, dfault int, min int, max int) (int, bool) {
|
||||||
|
@ -193,12 +185,17 @@ func parseOptions(options Options) intOptions {
|
||||||
brotli.DefaultCompression, brotli.BestSpeed, brotli.BestCompression,
|
brotli.DefaultCompression, brotli.BestSpeed, brotli.BestCompression,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
exts := map[string]bool{}
|
||||||
|
for _, ext := range options.SkipExtensions {
|
||||||
|
exts[ext] = true
|
||||||
|
}
|
||||||
|
|
||||||
return intOptions{
|
return intOptions{
|
||||||
GzipEn: gzipEn,
|
GzipEn: gzipEn,
|
||||||
GzipLevel: gzipLvl,
|
GzipLevel: gzipLvl,
|
||||||
BrotliEn: brEn,
|
BrotliEn: brEn,
|
||||||
BrotliLevel: brLvl,
|
BrotliLevel: brLvl,
|
||||||
SkipExtensions: options.SkipExtensions,
|
SkipExtensions: exts,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue