Skip to content

Commit 9bb40fc

Browse files
committed
refactor: refactor gzipHandler to use config struct instead of Options struct
- Rename `Options` to `config` in `gzipHandler` struct - Initialize `config` in `newGzipHandler` function - Apply options to `config` in `newGzipHandler` function - Remove `Options` initialization and application in `newGzipHandler` - Rename fields in `gzipHandler` to use camelCase - Add comments to explain `DefaultExcludedExtentions` and `Option` interface - Replace `Options` struct with `config` struct - Ensure `optionFunc` implements `Option` interface - Update `WithExcludedExtensions`, `WithExcludedPaths`, `WithExcludedPathsRegexs`, `WithDecompressFn`, and `WithDecompressOnly` functions to use `config` instead of `Options` Signed-off-by: appleboy <[email protected]>
1 parent eae4239 commit 9bb40fc

File tree

2 files changed

+60
-41
lines changed

2 files changed

+60
-41
lines changed

handler.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,22 @@ import (
1313
)
1414

1515
type gzipHandler struct {
16-
*Options
16+
*config
1717
gzPool sync.Pool
1818
}
1919

20-
func newGzipHandler(level int, options ...Option) *gzipHandler {
20+
func newGzipHandler(level int, opts ...Option) *gzipHandler {
21+
cfg := &config{
22+
excludedExtensions: DefaultExcludedExtentions,
23+
}
24+
25+
// Apply each option to the config
26+
for _, o := range opts {
27+
o.apply(cfg)
28+
}
29+
2130
handler := &gzipHandler{
22-
Options: DefaultOptions,
31+
config: cfg,
2332
gzPool: sync.Pool{
2433
New: func() interface{} {
2534
gz, err := gzip.NewWriterLevel(io.Discard, level)
@@ -30,18 +39,15 @@ func newGzipHandler(level int, options ...Option) *gzipHandler {
3039
},
3140
},
3241
}
33-
for _, setter := range options {
34-
setter(handler.Options)
35-
}
3642
return handler
3743
}
3844

3945
func (g *gzipHandler) Handle(c *gin.Context) {
40-
if fn := g.DecompressFn; fn != nil && c.Request.Header.Get("Content-Encoding") == "gzip" {
46+
if fn := g.decompressFn; fn != nil && c.Request.Header.Get("Content-Encoding") == "gzip" {
4147
fn(c)
4248
}
4349

44-
if g.DecompressOnly || !g.shouldCompress(c.Request) {
50+
if g.decompressOnly || !g.shouldCompress(c.Request) {
4551
return
4652
}
4753

@@ -71,14 +77,14 @@ func (g *gzipHandler) shouldCompress(req *http.Request) bool {
7177
}
7278

7379
extension := filepath.Ext(req.URL.Path)
74-
if g.ExcludedExtensions.Contains(extension) {
80+
if g.excludedExtensions.Contains(extension) {
7581
return false
7682
}
7783

78-
if g.ExcludedPaths.Contains(req.URL.Path) {
84+
if g.excludedPaths.Contains(req.URL.Path) {
7985
return false
8086
}
81-
if g.ExcludedPathesRegexs.Contains(req.URL.Path) {
87+
if g.excludedPathesRegexs.Contains(req.URL.Path) {
8288
return false
8389
}
8490

options.go

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,69 +9,82 @@ import (
99
"github.com/gin-gonic/gin"
1010
)
1111

12-
var (
13-
DefaultExcludedExtentions = NewExcludedExtensions([]string{
14-
".png", ".gif", ".jpeg", ".jpg",
15-
})
16-
DefaultOptions = &Options{
17-
ExcludedExtensions: DefaultExcludedExtentions,
18-
}
19-
)
12+
// DefaultExcludedExtentions is a predefined list of file extensions that should be excluded from gzip compression.
13+
// These extensions typically represent image files that are already compressed
14+
// and do not benefit from additional compression.
15+
var DefaultExcludedExtentions = NewExcludedExtensions([]string{
16+
".png", ".gif", ".jpeg", ".jpg",
17+
})
18+
19+
// Option is an interface that defines a method to apply a configuration
20+
// to a given config instance. Implementations of this interface can be
21+
// used to modify the configuration settings of the logger.
22+
type Option interface {
23+
apply(*config)
24+
}
25+
26+
// Ensures that optionFunc implements the Option interface at compile time.
27+
// If optionFunc does not implement Option, a compile-time error will occur.
28+
var _ Option = (*optionFunc)(nil)
2029

21-
type Options struct {
22-
ExcludedExtensions ExcludedExtensions
23-
ExcludedPaths ExcludedPaths
24-
ExcludedPathesRegexs ExcludedPathesRegexs
25-
DecompressFn func(c *gin.Context)
26-
DecompressOnly bool
30+
type optionFunc func(*config)
31+
32+
func (o optionFunc) apply(c *config) {
33+
o(c)
2734
}
2835

29-
type Option func(*Options)
36+
type config struct {
37+
excludedExtensions ExcludedExtensions
38+
excludedPaths ExcludedPaths
39+
excludedPathesRegexs ExcludedPathesRegexs
40+
decompressFn func(c *gin.Context)
41+
decompressOnly bool
42+
}
3043

3144
// WithExcludedExtensions returns an Option that sets the ExcludedExtensions field of the Options struct.
3245
// Parameters:
3346
// - args: []string - A slice of file extensions to exclude from gzip compression.
3447
func WithExcludedExtensions(args []string) Option {
35-
return func(o *Options) {
36-
o.ExcludedExtensions = NewExcludedExtensions(args)
37-
}
48+
return optionFunc(func(o *config) {
49+
o.excludedExtensions = NewExcludedExtensions(args)
50+
})
3851
}
3952

4053
// WithExcludedPaths returns an Option that sets the ExcludedPaths field of the Options struct.
4154
// Parameters:
4255
// - args: []string - A slice of paths to exclude from gzip compression.
4356
func WithExcludedPaths(args []string) Option {
44-
return func(o *Options) {
45-
o.ExcludedPaths = NewExcludedPaths(args)
46-
}
57+
return optionFunc(func(o *config) {
58+
o.excludedPaths = NewExcludedPaths(args)
59+
})
4760
}
4861

4962
// WithExcludedPathsRegexs returns an Option that sets the ExcludedPathesRegexs field of the Options struct.
5063
// Parameters:
5164
// - args: []string - A slice of regex patterns to exclude paths from gzip compression.
5265
func WithExcludedPathsRegexs(args []string) Option {
53-
return func(o *Options) {
54-
o.ExcludedPathesRegexs = NewExcludedPathesRegexs(args)
55-
}
66+
return optionFunc(func(o *config) {
67+
o.excludedPathesRegexs = NewExcludedPathesRegexs(args)
68+
})
5669
}
5770

5871
// WithDecompressFn returns an Option that sets the DecompressFn field of the Options struct.
5972
// Parameters:
6073
// - decompressFn: func(c *gin.Context) - A function to handle decompression of incoming requests.
6174
func WithDecompressFn(decompressFn func(c *gin.Context)) Option {
62-
return func(o *Options) {
63-
o.DecompressFn = decompressFn
64-
}
75+
return optionFunc(func(o *config) {
76+
o.decompressFn = decompressFn
77+
})
6578
}
6679

6780
// WithDecompressOnly is an option that configures the gzip middleware to only
6881
// decompress incoming requests without compressing the responses. When this
6982
// option is enabled, the middleware will set the DecompressOnly field of the
7083
// Options struct to true.
7184
func WithDecompressOnly() Option {
72-
return func(o *Options) {
73-
o.DecompressOnly = true
74-
}
85+
return optionFunc(func(o *config) {
86+
o.decompressOnly = true
87+
})
7588
}
7689

7790
// Using map for better lookup performance

0 commit comments

Comments
 (0)