generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples/rapido: add example on how to use rapide
- Loading branch information
Showing
4 changed files
with
97 additions
and
3 deletions.
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,11 @@ | ||
# RAPIDO | ||
|
||
RAPIDO is an example CLI which download an unordered `.car` for some unixfs data from multiple gateways using [`rapide`](../../rapide). | ||
|
||
This code is not maintained up to "production ready" standars, this is an example demonstrating how to use RAPIDE. | ||
|
||
## Usage | ||
|
||
```console | ||
$ rapido -gw https://ipfs.io/ipfs/ -gw https://strn.pl/ipfs/ QmT2EHPdRvUDxiuZBbYg5ZHy1f8L6MY1HxD45zHycLobMJ | pv > 2023-02-03.ipfs.io.car | ||
``` |
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,83 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/ipfs/go-cid" | ||
"github.com/ipfs/go-libipfs/ipsl/unixfs" | ||
"github.com/ipfs/go-libipfs/rapide" | ||
"github.com/ipfs/go-libipfs/rapide/gateway" | ||
"github.com/ipld/go-car" | ||
"github.com/ipld/go-car/util" | ||
) | ||
|
||
type stringFlagSlice []string | ||
|
||
func (sfs *stringFlagSlice) String() string { | ||
return strings.Join(*sfs, ",") | ||
} | ||
|
||
func (sfs *stringFlagSlice) Set(s string) error { | ||
*sfs = append(*sfs, s) | ||
return nil | ||
} | ||
|
||
func main() { | ||
err := mainRet() | ||
if err != nil { | ||
os.Stderr.WriteString(err.Error()) | ||
os.Exit(1) | ||
} | ||
os.Exit(0) | ||
} | ||
|
||
func mainRet() error { | ||
// Argument Parsing | ||
var gateways stringFlagSlice | ||
flag.Var(&gateways, "gw", `Set once to add a gateway, setable multiple times to use multiple gateways. Format expected is "-gw https://ipfs.io/ipfs/" for example.`) | ||
flag.Parse() | ||
|
||
cidStrs := flag.Args() | ||
if len(cidStrs) != 1 { | ||
return fmt.Errorf("expected one CID as positional argument; got %d", len(cidStrs)) | ||
} | ||
|
||
root, err := cid.Decode(cidStrs[0]) | ||
if err != nil { | ||
return fmt.Errorf("decoding CID: %w", err) | ||
} | ||
|
||
// Setup header for the output car | ||
err = car.WriteHeader(&car.CarHeader{ | ||
Roots: []cid.Cid{root}, | ||
Version: 1, | ||
}, os.Stdout) | ||
if err != nil { | ||
return fmt.Errorf("writing car header: %w", err) | ||
} | ||
|
||
// configure rapide | ||
downloaders := make([]rapide.ServerDrivenDownloader, len(gateways)) // create a slice holding our multiple gateways | ||
for i, g := range gateways { | ||
downloaders[i] = gateway.Gateway{PathName: g} // create a gateway protocol implementation | ||
} | ||
client := rapide.Client{ServerDrivenDownloaders: downloaders} | ||
|
||
// do request and iterate over the resulting blocks, rapide.(*Client).Get returns a channel | ||
for maybeBlock := range client.Get(context.Background(), root, unixfs.Everything()) { | ||
block, err := maybeBlock.Get() // block or error ? | ||
if err != nil { | ||
return fmt.Errorf("downloading: %w", err) | ||
} | ||
err = util.LdWrite(os.Stdout, block.Cid().Bytes(), block.RawData()) // write to the output car | ||
if err != nil { | ||
return fmt.Errorf("writing to output car: %w", err) | ||
} | ||
} | ||
|
||
return nil | ||
} |