-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
63 lines (53 loc) · 1.36 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
52
53
54
55
56
57
58
59
60
61
62
63
package memhttp
import (
"context"
"log"
"time"
)
type config struct {
DisableTLS bool
DisableHTTP2 bool
CleanupContext func() (context.Context, context.CancelFunc)
ErrorLog *log.Logger
}
// An Option configures a Server.
type Option interface {
apply(*config)
}
type optionFunc func(*config)
func (f optionFunc) apply(cfg *config) { f(cfg) }
// WithoutTLS disables TLS on the server and client.
func WithoutTLS() Option {
return optionFunc(func(cfg *config) {
cfg.DisableTLS = true
})
}
// WithoutHTTP2 disables HTTP/2 on the server and client.
func WithoutHTTP2() Option {
return optionFunc(func(cfg *config) {
cfg.DisableHTTP2 = true
})
}
// WithOptions composes multiple Options into one.
func WithOptions(opts ...Option) Option {
return optionFunc(func(cfg *config) {
for _, opt := range opts {
opt.apply(cfg)
}
})
}
// WithCleanupTimeout customizes the default five-second timeout for the
// server's Cleanup method. It's most useful with the memhttptest subpackage.
func WithCleanupTimeout(d time.Duration) Option {
return optionFunc(func(cfg *config) {
cfg.CleanupContext = func() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), d)
}
})
}
// WithErrorLog sets [http.Server.ErrorLog].
func WithErrorLog(l *log.Logger) Option {
return optionFunc(func(cfg *config) {
cfg.ErrorLog = l
})
}