|
| 1 | +package federation |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "net/http" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/ipfs/go-datastore" |
| 9 | + "github.com/ipfs/go-datastore/sync" |
| 10 | + "github.com/ipld/go-ipld-prime" |
| 11 | + cidlink "github.com/ipld/go-ipld-prime/linking/cid" |
| 12 | + "github.com/ipld/go-ipld-prime/storage/dsadapter" |
| 13 | + "github.com/ipni/storetheindex/internal/ingest" |
| 14 | + "github.com/ipni/storetheindex/internal/registry" |
| 15 | + "github.com/libp2p/go-libp2p" |
| 16 | + "github.com/libp2p/go-libp2p/core/host" |
| 17 | + "github.com/libp2p/go-libp2p/core/peer" |
| 18 | +) |
| 19 | + |
| 20 | +type ( |
| 21 | + Option func(*options) error |
| 22 | + |
| 23 | + options struct { |
| 24 | + datastore datastore.Datastore |
| 25 | + host host.Host |
| 26 | + httpListenAddr string |
| 27 | + linkSystem *ipld.LinkSystem |
| 28 | + members []peer.AddrInfo |
| 29 | + registry *registry.Registry |
| 30 | + ingester *ingest.Ingester |
| 31 | + reconciliationHttpClient *http.Client |
| 32 | + reconciliationInterval time.Duration |
| 33 | + snapshotInterval time.Duration |
| 34 | + } |
| 35 | +) |
| 36 | + |
| 37 | +func newOptions(o ...Option) (*options, error) { |
| 38 | + opt := options{ |
| 39 | + httpListenAddr: "0.0.0.0:3004", |
| 40 | + reconciliationHttpClient: http.DefaultClient, |
| 41 | + reconciliationInterval: 1 * time.Hour, |
| 42 | + snapshotInterval: 30 * time.Minute, |
| 43 | + } |
| 44 | + for _, apply := range o { |
| 45 | + if err := apply(&opt); err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + } |
| 49 | + // Check required options |
| 50 | + if opt.ingester == nil { |
| 51 | + return nil, errors.New("ingester must be specified") |
| 52 | + } |
| 53 | + if opt.registry == nil { |
| 54 | + return nil, errors.New("registry must be specified") |
| 55 | + } |
| 56 | + |
| 57 | + // Set defaults |
| 58 | + if opt.datastore == nil { |
| 59 | + opt.datastore = sync.MutexWrap(datastore.NewMapDatastore()) |
| 60 | + } |
| 61 | + if opt.host == nil { |
| 62 | + var err error |
| 63 | + opt.host, err = libp2p.New() |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + } |
| 68 | + if opt.linkSystem == nil { |
| 69 | + ls := cidlink.DefaultLinkSystem() |
| 70 | + storage := dsadapter.Adapter{ |
| 71 | + Wrapped: opt.datastore, |
| 72 | + } |
| 73 | + ls.SetReadStorage(&storage) |
| 74 | + ls.SetWriteStorage(&storage) |
| 75 | + opt.linkSystem = &ls |
| 76 | + } |
| 77 | + |
| 78 | + return &opt, nil |
| 79 | +} |
| 80 | + |
| 81 | +func WithDatastore(v datastore.Datastore) Option { |
| 82 | + return func(o *options) error { |
| 83 | + o.datastore = v |
| 84 | + return nil |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func WithHost(v host.Host) Option { |
| 89 | + return func(o *options) error { |
| 90 | + o.host = v |
| 91 | + return nil |
| 92 | + } |
| 93 | +} |
| 94 | +func WithHttpListenAddr(v string) Option { |
| 95 | + return func(o *options) error { |
| 96 | + o.httpListenAddr = v |
| 97 | + return nil |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func WithMembers(v ...peer.AddrInfo) Option { |
| 102 | + return func(o *options) error { |
| 103 | + o.members = append(o.members, v...) |
| 104 | + return nil |
| 105 | + } |
| 106 | +} |
| 107 | +func WithLinkSystem(v *ipld.LinkSystem) Option { |
| 108 | + return func(o *options) error { |
| 109 | + o.linkSystem = v |
| 110 | + return nil |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func WithIngester(v *ingest.Ingester) Option { |
| 115 | + return func(o *options) error { |
| 116 | + o.ingester = v |
| 117 | + return nil |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func WithRegistry(v *registry.Registry) Option { |
| 122 | + return func(o *options) error { |
| 123 | + o.registry = v |
| 124 | + return nil |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func WithReconciliationInterval(v time.Duration) Option { |
| 129 | + return func(o *options) error { |
| 130 | + o.reconciliationInterval = v |
| 131 | + return nil |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func WithSnapshotInterval(v time.Duration) Option { |
| 136 | + return func(o *options) error { |
| 137 | + o.snapshotInterval = v |
| 138 | + return nil |
| 139 | + } |
| 140 | +} |
0 commit comments