-
Notifications
You must be signed in to change notification settings - Fork 26
/
options.go
82 lines (71 loc) · 1.7 KB
/
options.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package find
import (
"fmt"
"time"
)
const (
defaultHomepage = "https://web-ipni.cid.contact/"
defaultMaxConns = 8_000
defaultReadTimeout = 30 * time.Second
defaultWriteTimeout = 30 * time.Second
)
// config contains all options for the server.
type config struct {
homepageURL string
maxConns int
readTimeout time.Duration
writeTimeout time.Duration
version string
}
// Option is a function that sets a value in a config.
type Option func(*config) error
// getOpts creates a config and applies Options to it.
func getOpts(opts []Option) (config, error) {
cfg := config{
homepageURL: defaultHomepage,
maxConns: defaultMaxConns,
readTimeout: defaultReadTimeout,
writeTimeout: defaultWriteTimeout,
}
for i, opt := range opts {
if err := opt(&cfg); err != nil {
return config{}, fmt.Errorf("option %d error: %s", i, err)
}
}
return cfg, nil
}
// WithHomepage config for API.
func WithHomepage(URL string) Option {
return func(c *config) error {
c.homepageURL = URL
return nil
}
}
// MaxConnections config allowed by server.
func WithMaxConnections(maxConnections int) Option {
return func(c *config) error {
c.maxConns = maxConnections
return nil
}
}
// WithReadTimeout configures server read timeout.
func WithReadTimeout(t time.Duration) Option {
return func(c *config) error {
c.readTimeout = t
return nil
}
}
// WithWriteTimeout configures server write timeout.
func WithWriteTimeout(t time.Duration) Option {
return func(c *config) error {
c.writeTimeout = t
return nil
}
}
// WithVersion sets the version string used in /health output.
func WithVersion(ver string) Option {
return func(c *config) error {
c.version = ver
return nil
}
}