-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
162 lines (145 loc) · 3.81 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package caskadht
import (
"time"
"github.com/libp2p/go-libp2p"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
)
type (
Option func(*options) error
options struct {
h host.Host
httpListenAddr string
httpAllowOrigin string
httpResponsePreferJson bool
metricsHttpListenAddr string
metricsEnablePprofDebug bool
bootstrapPeers []peer.AddrInfo
useAccDHT bool
ipniCascadeLabel string
ipniRequireCascadeQueryParam bool
addrFilterDisabled bool
findProvidersLimit int
prAttemptCacheMaxSize int
prAttemptCacheMaxAge time.Duration
}
)
func newOptions(o ...Option) (*options, error) {
opts := options{
httpListenAddr: "0.0.0.0:40080",
metricsHttpListenAddr: "0.0.0.0:40081",
metricsEnablePprofDebug: true,
useAccDHT: false,
ipniCascadeLabel: "ipfs-dht",
httpAllowOrigin: "*",
prAttemptCacheMaxSize: 1024,
prAttemptCacheMaxAge: 20 * time.Minute,
}
for _, apply := range o {
if err := apply(&opts); err != nil {
return nil, err
}
}
var err error
if opts.h == nil {
opts.h, err = libp2p.New()
if err != nil {
return nil, err
}
}
if len(opts.bootstrapPeers) == 0 {
opts.bootstrapPeers = make([]peer.AddrInfo, 0, len(dht.DefaultBootstrapPeers))
for _, p := range dht.DefaultBootstrapPeers {
pa, err := peer.AddrInfoFromP2pAddr(p)
if err != nil {
return nil, err
}
opts.bootstrapPeers = append(opts.bootstrapPeers, *pa)
}
}
return &opts, nil
}
func WithHost(h host.Host) Option {
return func(o *options) error {
o.h = h
return nil
}
}
func WithHttpListenAddr(a string) Option {
return func(o *options) error {
o.httpListenAddr = a
return nil
}
}
func WithBootstrapPeers(p ...peer.AddrInfo) Option {
return func(o *options) error {
o.bootstrapPeers = p
return nil
}
}
func WithUseAcceleratedDHT(b bool) Option {
return func(o *options) error {
o.useAccDHT = b
return nil
}
}
func WithIpniCascadeLabel(l string) Option {
return func(o *options) error {
o.ipniCascadeLabel = l
return nil
}
}
func WithHttpAllowOrigin(ao string) Option {
return func(o *options) error {
o.httpAllowOrigin = ao
return nil
}
}
// WithIpniRequireCascadeQueryParam sets whether the server should require IPNI cascade query
// parameter with the matching label in order to respond to HTTP lookup requests.
// See: WithIpniCascadeLabel
func WithIpniRequireCascadeQueryParam(p bool) Option {
return func(o *options) error {
o.ipniRequireCascadeQueryParam = p
return nil
}
}
// WithHttpResponsePreferJson sets whether to prefer non-streaming json response over streaming
// ndjosn when the Accept header uses `*/*` wildcard. By default, in such case ndjson streaming
// response is preferred.
func WithHttpResponsePreferJson(b bool) Option {
return func(o *options) error {
o.httpResponsePreferJson = b
return nil
}
}
// WithAddrFilterDisabled sets whether to filter out addresses that are not publicly dialable.
// By default such address are excluded from results.
// See: IsPubliclyDialableAddr.
func WithAddrFilterDisabled(b bool) Option {
return func(o *options) error {
o.addrFilterDisabled = b
return nil
}
}
// WithFindProvidersLimit sets the limit on number of providers to find.
// Defaults to zero, i.e. no limit.
func WithFindProvidersLimit(l int) Option {
return func(o *options) error {
o.findProvidersLimit = l
return nil
}
}
func WithMetricsEnablePprofDebug(b bool) Option {
return func(o *options) error {
o.metricsEnablePprofDebug = b
return nil
}
}
func WithMetricsListenAddr(a string) Option {
return func(o *options) error {
o.metricsHttpListenAddr = a
return nil
}
}