-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathoptions.go
140 lines (126 loc) · 2.98 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
package federation
import (
"errors"
"net/http"
"time"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/sync"
"github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/storage/dsadapter"
"github.com/ipni/storetheindex/internal/ingest"
"github.com/ipni/storetheindex/internal/registry"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
)
type (
Option func(*options) error
options struct {
datastore datastore.Datastore
host host.Host
httpListenAddr string
linkSystem *ipld.LinkSystem
members []peer.AddrInfo
registry *registry.Registry
ingester *ingest.Ingester
reconciliationHttpClient *http.Client
reconciliationInterval time.Duration
snapshotInterval time.Duration
}
)
func newOptions(o ...Option) (*options, error) {
opt := options{
httpListenAddr: "0.0.0.0:3004",
reconciliationHttpClient: http.DefaultClient,
reconciliationInterval: 1 * time.Hour,
snapshotInterval: 30 * time.Minute,
}
for _, apply := range o {
if err := apply(&opt); err != nil {
return nil, err
}
}
// Check required options
if opt.ingester == nil {
return nil, errors.New("ingester must be specified")
}
if opt.registry == nil {
return nil, errors.New("registry must be specified")
}
// Set defaults
if opt.datastore == nil {
opt.datastore = sync.MutexWrap(datastore.NewMapDatastore())
}
if opt.host == nil {
var err error
opt.host, err = libp2p.New()
if err != nil {
return nil, err
}
}
if opt.linkSystem == nil {
ls := cidlink.DefaultLinkSystem()
storage := dsadapter.Adapter{
Wrapped: opt.datastore,
}
ls.SetReadStorage(&storage)
ls.SetWriteStorage(&storage)
opt.linkSystem = &ls
}
return &opt, nil
}
func WithDatastore(v datastore.Datastore) Option {
return func(o *options) error {
o.datastore = v
return nil
}
}
func WithHost(v host.Host) Option {
return func(o *options) error {
o.host = v
return nil
}
}
func WithHttpListenAddr(v string) Option {
return func(o *options) error {
o.httpListenAddr = v
return nil
}
}
func WithMembers(v ...peer.AddrInfo) Option {
return func(o *options) error {
o.members = append(o.members, v...)
return nil
}
}
func WithLinkSystem(v *ipld.LinkSystem) Option {
return func(o *options) error {
o.linkSystem = v
return nil
}
}
func WithIngester(v *ingest.Ingester) Option {
return func(o *options) error {
o.ingester = v
return nil
}
}
func WithRegistry(v *registry.Registry) Option {
return func(o *options) error {
o.registry = v
return nil
}
}
func WithReconciliationInterval(v time.Duration) Option {
return func(o *options) error {
o.reconciliationInterval = v
return nil
}
}
func WithSnapshotInterval(v time.Duration) Option {
return func(o *options) error {
o.snapshotInterval = v
return nil
}
}