-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_writer_dr.go
75 lines (67 loc) · 1.77 KB
/
response_writer_dr.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
package caskadht
import (
"net/http"
"strings"
"github.com/ipni/go-libipni/apierror"
"github.com/ipni/go-libipni/rwriter"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
"github.com/multiformats/go-multicodec"
)
var drProtocolBitswap = multicodec.TransportBitswap.String()
const drSchemaBitswap = "bitswap"
type (
delegatedRoutingLookupResponseWriter struct {
rwriter.ResponseWriter
result drProviderRecords
}
drProviderRecords struct {
Providers []drProviderRecord
}
drProviderRecord struct {
Protocol string
Schema string
ID peer.ID
Addrs []multiaddr.Multiaddr
}
)
func newDelegatedRoutingLookupResponseWriter(w http.ResponseWriter, r *http.Request, preferJson bool) (*delegatedRoutingLookupResponseWriter, error) {
if !strings.HasPrefix(r.URL.Path, "/routing/v1/providers/") {
return nil, apierror.New(nil, http.StatusNotFound)
}
rspWriter, err := rwriter.New(w, r,
rwriter.WithPreferJson(preferJson),
rwriter.WithMultihashPathType(""),
rwriter.WithCidPathType("providers"),
)
if err != nil {
return nil, err
}
return &delegatedRoutingLookupResponseWriter{
ResponseWriter: *rspWriter,
}, nil
}
func (d *delegatedRoutingLookupResponseWriter) writeDrProviderRecord(provider peer.AddrInfo) error {
rec := drProviderRecord{
Protocol: drProtocolBitswap,
Schema: drSchemaBitswap,
ID: provider.ID,
Addrs: provider.Addrs,
}
if d.IsND() {
if err := d.Encoder().Encode(rec); err != nil {
logger.Errorw("Failed to encode ndjson response", "err", err)
return err
}
d.Flush()
} else {
d.result.Providers = append(d.result.Providers, rec)
}
return nil
}
func (d *delegatedRoutingLookupResponseWriter) close() error {
if d.IsND() {
return nil
}
return d.Encoder().Encode(d.result)
}