-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
223 lines (199 loc) · 5.34 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package naam
import (
"bytes"
"io"
"net/http"
"time"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
"github.com/ipfs/go-datastore/sync"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/linking"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/host"
)
const defaultAnnounceURL = "https://cid.contact"
type (
Option func(*options)
options struct {
ds datastore.Datastore
host host.Host
httpClient *http.Client
findURL string
announceURL string
listenAddr string
linkSys *ipld.LinkSystem
providerAddrs []string
publisherAddrs []string
readPriv bool
}
PublishOption func(*publishOptions)
publishOptions struct {
embedPubKey bool
eol time.Time
ttl time.Duration
}
)
func newOptions(o ...Option) (*options, error) {
opts := options{
httpClient: http.DefaultClient,
announceURL: defaultAnnounceURL,
listenAddr: "0.0.0.0:8080",
readPriv: true,
}
for _, apply := range o {
apply(&opts)
}
var err error
if opts.host == nil {
opts.host, err = libp2p.New()
if err != nil {
return nil, err
}
}
if opts.ds == nil {
opts.ds = sync.MutexWrap(datastore.NewMapDatastore())
}
if opts.linkSys == nil {
opts.linkSys = makeLinkSys(opts.ds)
}
if opts.findURL == "" {
opts.findURL = opts.announceURL
}
return &opts, nil
}
func makeLinkSys(ds datastore.Datastore) *ipld.LinkSystem {
linkSys := cidlink.DefaultLinkSystem()
lsds := namespace.Wrap(ds, datastore.NewKey("ls"))
linkSys.StorageReadOpener = func(ctx linking.LinkContext, l datamodel.Link) (io.Reader, error) {
val, err := lsds.Get(ctx.Ctx, datastore.NewKey(l.Binary()))
if err != nil {
return nil, err
}
return bytes.NewBuffer(val), nil
}
linkSys.StorageWriteOpener = func(ctx ipld.LinkContext) (io.Writer, ipld.BlockWriteCommitter, error) {
buf := bytes.NewBuffer(nil)
return buf, func(l ipld.Link) error {
return lsds.Put(ctx.Ctx, datastore.NewKey(l.Binary()), buf.Bytes())
}, nil
}
return &linkSys
}
// WithDatastore supplies an existing datastore. An in-memory datastore is
// created if one is not supplied.
func WithDatastore(ds datastore.Datastore) Option {
return func(o *options) {
o.ds = ds
}
}
// WithHost supplies a libp2p Host. If not specified a new Host is created.
func WithHost(h host.Host) Option {
return func(o *options) {
o.host = h
}
}
// WithHttpClient supplies an optional HTTP client. If not specified, the
// default client is used.
func WithHttpClient(c *http.Client) Option {
return func(o *options) {
o.httpClient = c
}
}
// WithFindURL configures the URL use for lookups. This is the URL of the
// dhstore that is used for multihash lookup. If not specified the URL
// configured by WithAnnounceURL is used.
func WithFindURL(a string) Option {
return func(o *options) {
if a != "" {
o.findURL = a
}
}
}
// WithAnnounceURL is the IPNI endpoint to send direct HTTP advertisement
// announcement messages to. If no values are specified, then the value of
// defaultAnnounceURL is used.
func WithAnnounceURL(a string) Option {
return func(o *options) {
if a != "" {
o.announceURL = a
}
}
}
// WithListenAddr specifies the address:port that the publisher listens on when
// serving advertisements over HTTP. If not specified, the default
// "0.0.0.0:8080" is used.
func WithListenAddr(a string) Option {
return func(o *options) {
if a != "" {
o.listenAddr = a
}
}
}
func WithLinkSystem(ls *ipld.LinkSystem) Option {
return func(o *options) {
o.linkSys = ls
}
}
// WithPublisherAddrs is the multiaddr that tells IPNI where to fetch
// advertisements from.
//
// This address must be routable from the indexer. If not specified then the
// publisher listen addresses are used.
func WithPublisherAddrs(addrs ...string) Option {
return func(o *options) {
if len(addrs) != 0 {
o.publisherAddrs = append(o.publisherAddrs, addrs...)
}
}
}
// WithReaderPrivacy enables (true) or disables (false) reader-privacy. This is
// enabled by default.
func WithReaderPrivacy(enabled bool) Option {
return func(o *options) {
o.readPriv = enabled
}
}
// WithProviderAddrs are multiaddrs that get put into advertisements as the
// provider addresses. If not specified then the publisher addresses are used.
//
// When publishing to a public indexer, this should always be set to a public
// address so that the advertisement is not rejected.
//
// These addresses to not have a role in resolving IPNS, but may be used to
// tell an IPNS clients where to get the content corresponding to the CIR in
// the IPNS record.
func WithProviderAddrs(addrs ...string) Option {
return func(o *options) {
if len(addrs) != 0 {
o.providerAddrs = append(o.providerAddrs, addrs...)
}
}
}
func newPublishOptions(o ...PublishOption) *publishOptions {
opts := publishOptions{
eol: time.Now().Add(24 * time.Hour),
embedPubKey: true,
}
for _, apply := range o {
apply(&opts)
}
return &opts
}
func WithEmbedPublicKey(epk bool) PublishOption {
return func(o *publishOptions) {
o.embedPubKey = epk
}
}
func WithEOL(eol time.Time) PublishOption {
return func(o *publishOptions) {
o.eol = eol
}
}
func WithTTL(ttl time.Duration) PublishOption {
return func(o *publishOptions) {
o.ttl = ttl
}
}