-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptions.go
More file actions
101 lines (84 loc) · 2.25 KB
/
Copy pathoptions.go
File metadata and controls
101 lines (84 loc) · 2.25 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package cloudip
import (
"net/http"
"time"
"github.com/rezmoss/go-cloudip/internal/source"
)
// options holds configuration for the Detector.
type options struct {
// dataDir is the directory for caching data.
dataDir string
// autoUpdate is the interval for automatic updates.
// Zero means no automatic updates.
autoUpdate time.Duration
// offline disables network access.
offline bool
// httpClient is the HTTP client to use for requests.
httpClient *http.Client
// dataURL overrides the default data URL.
dataURL string
// versionURL overrides the default version URL.
versionURL string
}
// defaultOptions returns options with default values.
func defaultOptions() *options {
return &options{
dataDir: source.DefaultCacheDir(),
autoUpdate: 0,
offline: false,
httpClient: nil,
dataURL: source.DefaultDataURL,
versionURL: source.DefaultVersionURL,
}
}
// Option is a functional option for configuring the Detector.
type Option func(*options)
// WithDataDir sets the directory for caching data.
// Set to empty string to disable caching.
func WithDataDir(dir string) Option {
return func(o *options) {
o.dataDir = dir
}
}
// WithAutoUpdate enables automatic background updates at the given interval.
// Minimum interval is 1 hour. Use zero to disable (default).
func WithAutoUpdate(interval time.Duration) Option {
return func(o *options) {
if interval > 0 && interval < time.Hour {
interval = time.Hour
}
o.autoUpdate = interval
}
}
// WithOffline disables all network access.
// The detector will only use embedded data.
func WithOffline() Option {
return func(o *options) {
o.offline = true
}
}
// WithHTTPClient sets a custom HTTP client for network requests.
func WithHTTPClient(client *http.Client) Option {
return func(o *options) {
o.httpClient = client
}
}
// WithDataURL overrides the default data URL.
// Use this if you're hosting your own cloudip-db mirror.
func WithDataURL(url string) Option {
return func(o *options) {
o.dataURL = url
}
}
// WithVersionURL overrides the default version URL.
func WithVersionURL(url string) Option {
return func(o *options) {
o.versionURL = url
}
}
// WithNoCache disables the file cache.
func WithNoCache() Option {
return func(o *options) {
o.dataDir = ""
}
}