-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
51 lines (42 loc) · 1.03 KB
/
option.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package compress
import (
"compress/gzip"
"context"
"io"
"net/http"
"regexp"
)
type Option func(*config)
func WithEncoder(encoding string, priotity int, factory EncoderFactory) Option {
return func(m *config) {
m.encoders[encoding] = &encoder{priority: priotity, factory: factory}
}
}
func WihtoutEncoder(encoding string) Option {
return func(c *config) {
delete(c.encoders, encoding)
}
}
func WithGzip(priority, level int) Option {
return WithEncoder("gzip", priority, func(ctx context.Context, w io.Writer) (io.WriteCloser, error) {
return gzip.NewWriterLevel(w, level)
})
}
func WithAllowedTypes(list []*regexp.Regexp) Option {
return func(c *config) {
c.allowedType = list
}
}
func WithMinSize(minSize uint64) Option {
return func(c *config) {
c.minSize = minSize
}
}
func WithErrorHandler(handler ErrorHandler) Option {
return func(c *config) {
c.errorHandler = handler
}
}
func DefaultErrorHandler(err error, _ *http.Request, w http.ResponseWriter) {
http.Error(w, err.Error(), http.StatusInternalServerError)
}