Browse Source

feat: support customized excluded paths

Pei-Ming Wu 6 years ago
parent
commit
b3ea4babeb
4 changed files with 76 additions and 1 deletions
  1. 25 0
      README.md
  2. 20 0
      gzip_test.go
  3. 5 1
      handler.go
  4. 26 0
      options.go

+ 25 - 0
README.md

@@ -74,3 +74,28 @@ func main() {
 	r.Run(":8080")
 }
 ```
+
+### Customized Excluded Paths
+
+```go
+package main
+
+import (
+	"fmt"
+	"time"
+
+	"github.com/gin-contrib/gzip"
+	"github.com/gin-gonic/gin"
+)
+
+func main() {
+	r := gin.Default()
+	r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPaths([]string{"/api/"})))
+	r.GET("/ping", func(c *gin.Context) {
+		c.String(200, "pong "+fmt.Sprint(time.Now().Unix()))
+	})
+
+	// Listen and Server in 0.0.0.0:8080
+	r.Run(":8080")
+}
+```

+ 20 - 0
gzip_test.go

@@ -130,6 +130,26 @@ func TestExcludedExtensions(t *testing.T) {
 	assert.Equal(t, "", w.Header().Get("Content-Length"))
 }
 
+func TestExcludedPaths(t *testing.T) {
+	req, _ := http.NewRequest("GET", "/api/books", nil)
+	req.Header.Add("Accept-Encoding", "gzip")
+
+	router := gin.New()
+	router.Use(Gzip(DefaultCompression, WithExcludedPaths([]string{"/api/"})))
+	router.GET("/api/books", func(c *gin.Context) {
+		c.String(200, "this is books!")
+	})
+
+	w := httptest.NewRecorder()
+	router.ServeHTTP(w, req)
+
+	assert.Equal(t, http.StatusOK, w.Code)
+	assert.Equal(t, "", w.Header().Get("Content-Encoding"))
+	assert.Equal(t, "", w.Header().Get("Vary"))
+	assert.Equal(t, "this is books!", w.Body.String())
+	assert.Equal(t, "", w.Header().Get("Content-Length"))
+}
+
 func TestNoGzip(t *testing.T) {
 	req, _ := http.NewRequest("GET", "/", nil)
 

+ 5 - 1
handler.go

@@ -65,5 +65,9 @@ func (g *gzipHandler) shouldCompress(req *http.Request) bool {
 	}
 
 	extension := filepath.Ext(req.URL.Path)
-	return !g.ExcludedExtensions.Contains(extension)
+	if g.ExcludedExtensions.Contains(extension) {
+		return false
+	}
+
+	return !g.ExcludedPaths.Contains(req.URL.Path)
 }

+ 26 - 0
options.go

@@ -1,5 +1,9 @@
 package gzip
 
+import (
+	"strings"
+)
+
 var (
 	DefaultExcludedExtentions = NewExcludedExtensions([]string{
 		".png", ".gif", ".jpeg", ".jpg",
@@ -11,6 +15,7 @@ var (
 
 type Options struct {
 	ExcludedExtensions ExcludedExtensions
+	ExcludedPaths      ExcludedPaths
 }
 
 type Option func(*Options)
@@ -21,6 +26,12 @@ func WithExcludedExtensions(args []string) Option {
 	}
 }
 
+func WithExcludedPaths(args []string) Option {
+	return func(o *Options) {
+		o.ExcludedPaths = NewExcludedPaths(args)
+	}
+}
+
 // Using map for better lookup performance
 type ExcludedExtensions map[string]bool
 
@@ -36,3 +47,18 @@ func (e ExcludedExtensions) Contains(target string) bool {
 	_, ok := e[target]
 	return ok
 }
+
+type ExcludedPaths []string
+
+func NewExcludedPaths(paths []string) ExcludedPaths {
+	return ExcludedPaths(paths)
+}
+
+func (e ExcludedPaths) Contains(requestURI string) bool {
+	for _, path := range e {
+		if strings.HasPrefix(requestURI, path) {
+			return true
+		}
+	}
+	return false
+}