-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(gateway): expose /routing/v1 server (opt-in) #9877
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d65b8ab
feat: expose routing v1 server via optional setting
hacdias 19af511
feat: re-add deprecated schema bitswap to ensure compatibility
hacdias 5ab1ec7
refactor: restore PUT code, but keep not supporting
hacdias 82e6ae5
chore: apply feedback suggestions
hacdias 833823c
feat: e2e tests, remove .SearchValue override
hacdias c658c6c
refactor: rename Find/ProvideIPNS to Get/PutIPNS
hacdias 6e57511
fix: ensure tests don't "collide" with each other
hacdias 9310e26
chore: bump boxo, do not force bitswap
hacdias 07f3000
chore: revert gateway names
hacdias abeeb59
fix: disable mdns in routing v1 test
hacdias 030ee4b
fix: disable mdns in routing v1 test
hacdias 997a3d3
feat: print routing v1 url
lidel 5fb2d5e
chore: bump boxo
hacdias d2caa67
chore: bump boxo to main
hacdias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package corehttp | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/ipfs/boxo/ipns" | ||
"github.com/ipfs/boxo/routing/http/server" | ||
"github.com/ipfs/boxo/routing/http/types" | ||
"github.com/ipfs/boxo/routing/http/types/iter" | ||
cid "github.com/ipfs/go-cid" | ||
core "github.com/ipfs/kubo/core" | ||
"github.com/libp2p/go-libp2p/core/peer" | ||
"github.com/libp2p/go-libp2p/core/routing" | ||
) | ||
|
||
func RoutingOption() ServeOption { | ||
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) { | ||
handler := server.Handler(&contentRouter{n}) | ||
mux.Handle("/routing/v1/", handler) | ||
return mux, nil | ||
} | ||
} | ||
|
||
type contentRouter struct { | ||
n *core.IpfsNode | ||
} | ||
|
||
func (r *contentRouter) FindProviders(ctx context.Context, key cid.Cid, limit int) (iter.ResultIter[types.Record], error) { | ||
ctx, cancel := context.WithCancel(ctx) | ||
ch := r.n.Routing.FindProvidersAsync(ctx, key, limit) | ||
return iter.ToResultIter[types.Record](&peerChanIter{ | ||
ch: ch, | ||
cancel: cancel, | ||
}), nil | ||
} | ||
|
||
// nolint deprecated | ||
func (r *contentRouter) ProvideBitswap(ctx context.Context, req *server.BitswapWriteProvideRequest) (time.Duration, error) { | ||
return 0, routing.ErrNotSupported | ||
} | ||
|
||
func (r *contentRouter) FindPeers(ctx context.Context, pid peer.ID, limit int) (iter.ResultIter[types.Record], error) { | ||
ctx, cancel := context.WithCancel(ctx) | ||
defer cancel() | ||
|
||
addr, err := r.n.Routing.FindPeer(ctx, pid) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rec := &types.PeerRecord{ | ||
Schema: types.SchemaPeer, | ||
ID: &addr.ID, | ||
} | ||
|
||
for _, addr := range addr.Addrs { | ||
rec.Addrs = append(rec.Addrs, types.Multiaddr{Multiaddr: addr}) | ||
} | ||
|
||
return iter.ToResultIter[types.Record](iter.FromSlice[types.Record]([]types.Record{rec})), nil | ||
} | ||
|
||
func (r *contentRouter) GetIPNS(ctx context.Context, name ipns.Name) (*ipns.Record, error) { | ||
ctx, cancel := context.WithCancel(ctx) | ||
defer cancel() | ||
|
||
raw, err := r.n.Routing.GetValue(ctx, string(name.RoutingKey())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return ipns.UnmarshalRecord(raw) | ||
} | ||
|
||
func (r *contentRouter) PutIPNS(ctx context.Context, name ipns.Name, record *ipns.Record) error { | ||
ctx, cancel := context.WithCancel(ctx) | ||
defer cancel() | ||
|
||
raw, err := ipns.MarshalRecord(record) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// The caller guarantees that name matches the record. This is double checked | ||
// by the internals of PutValue. | ||
return r.n.Routing.PutValue(ctx, string(name.RoutingKey()), raw) | ||
lidel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
type peerChanIter struct { | ||
ch <-chan peer.AddrInfo | ||
cancel context.CancelFunc | ||
next *peer.AddrInfo | ||
} | ||
|
||
func (it *peerChanIter) Next() bool { | ||
addr, ok := <-it.ch | ||
if ok { | ||
it.next = &addr | ||
return true | ||
} else { | ||
it.next = nil | ||
return false | ||
} | ||
} | ||
|
||
func (it *peerChanIter) Val() types.Record { | ||
if it.next == nil { | ||
return nil | ||
} | ||
|
||
rec := &types.PeerRecord{ | ||
Schema: types.SchemaPeer, | ||
ID: &it.next.ID, | ||
} | ||
|
||
for _, addr := range it.next.Addrs { | ||
rec.Addrs = append(rec.Addrs, types.Multiaddr{Multiaddr: addr}) | ||
} | ||
|
||
return rec | ||
} | ||
|
||
func (it *peerChanIter) Close() error { | ||
it.cancel() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ℹ️ we do this for WebUI URL, would not hurt here – makes it easy for user to eyeball if it is enabled, and copy URL for testing.