Skip to content

Commit 6ac096f

Browse files
authored
Merge pull request #36 from standcats/master
feat : excluede paths by regex
2 parents af5a1ba + 44264cb commit 6ac096f

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

README.md

+27
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,30 @@ func main() {
9999
r.Run(":8080")
100100
}
101101
```
102+
103+
104+
Customized Excluded Paths
105+
106+
```go
107+
package main
108+
109+
import (
110+
"fmt"
111+
"net/http"
112+
"time"
113+
114+
"github.com/gin-contrib/gzip"
115+
"github.com/gin-gonic/gin"
116+
)
117+
118+
func main() {
119+
r := gin.Default()
120+
r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPathsRegexs([]string{".*"})))
121+
r.GET("/ping", func(c *gin.Context) {
122+
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
123+
})
124+
125+
// Listen and Server in 0.0.0.0:8080
126+
r.Run(":8080")
127+
}
128+
```

handler.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,12 @@ func (g *gzipHandler) shouldCompress(req *http.Request) bool {
7373
return false
7474
}
7575

76-
return !g.ExcludedPaths.Contains(req.URL.Path)
76+
if g.ExcludedPaths.Contains(req.URL.Path) {
77+
return false
78+
}
79+
if g.ExcludedPathesRegexs.Contains(req.URL.Path) {
80+
return false
81+
}
82+
83+
return true
7784
}

options.go

+30-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gzip
33
import (
44
"compress/gzip"
55
"net/http"
6+
"regexp"
67
"strings"
78

89
"github.com/gin-gonic/gin"
@@ -18,9 +19,10 @@ var (
1819
)
1920

2021
type Options struct {
21-
ExcludedExtensions ExcludedExtensions
22-
ExcludedPaths ExcludedPaths
23-
DecompressFn func(c *gin.Context)
22+
ExcludedExtensions ExcludedExtensions
23+
ExcludedPaths ExcludedPaths
24+
ExcludedPathesRegexs ExcludedPathesRegexs
25+
DecompressFn func(c *gin.Context)
2426
}
2527

2628
type Option func(*Options)
@@ -37,6 +39,12 @@ func WithExcludedPaths(args []string) Option {
3739
}
3840
}
3941

42+
func WithExcludedPathsRegexs(args []string) Option {
43+
return func(o *Options) {
44+
o.ExcludedPathesRegexs = NewExcludedPathesRegexs(args)
45+
}
46+
}
47+
4048
func WithDecompressFn(decompressFn func(c *gin.Context)) Option {
4149
return func(o *Options) {
4250
o.DecompressFn = decompressFn
@@ -74,6 +82,25 @@ func (e ExcludedPaths) Contains(requestURI string) bool {
7482
return false
7583
}
7684

85+
type ExcludedPathesRegexs []*regexp.Regexp
86+
87+
func NewExcludedPathesRegexs(regexs []string) ExcludedPathesRegexs {
88+
result := make([]*regexp.Regexp, len(regexs), len(regexs))
89+
for i, reg := range regexs {
90+
result[i] = regexp.MustCompile(reg)
91+
}
92+
return result
93+
}
94+
95+
func (e ExcludedPathesRegexs) Contains(requestURI string) bool {
96+
for _, reg := range e {
97+
if reg.MatchString(requestURI) {
98+
return true
99+
}
100+
}
101+
return false
102+
}
103+
77104
func DefaultDecompressHandle(c *gin.Context) {
78105
if c.Request.Body == nil {
79106
return

0 commit comments

Comments
 (0)