Skip to content

Commit b667224

Browse files
committed
add example from readme as separate example
1 parent 7643ec9 commit b667224

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ required-features = ["example-iroh"]
154154
name = "custom-protocol"
155155
required-features = ["example-iroh"]
156156

157+
[[example]]
158+
name = "setup"
159+
required-features = ["example-iroh"]
160+
157161
[lints.rust]
158162
missing_debug_implementations = "warn"
159163

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ use iroh::{protocol::Router, Endpoint};
3535
use iroh_blobs::{net_protocol::Blobs, util::local_pool::LocalPool};
3636

3737
#[tokio::main]
38-
async fn main() -> Result<(), std::fmt::Error> {
38+
async fn main() -> anyhow::Result<()> {
3939
// create an iroh endpoint that includes the standard discovery mechanisms
4040
// we've built at number0
41-
let endpoint = Endpoint::builder().discovery_n0().bind().await.unwrap();
41+
let endpoint = Endpoint::builder().discovery_n0().bind().await?;
4242

4343
// spawn a local pool with one thread per CPU
4444
// for a single threaded pool use `LocalPool::single`
@@ -56,10 +56,14 @@ async fn main() -> Result<(), std::fmt::Error> {
5656
// build the router
5757
let router = Router::builder(endpoint)
5858
.accept(iroh_blobs::ALPN, blobs.clone())
59-
.spawn();
59+
.spawn()
60+
.await?;
6061

6162
// do fun stuff with the blobs protocol!
6263
// make sure not to drop the local_pool before you are finished
64+
router.shutdown().await?;
65+
drop(local_pool);
66+
drop(tags_client);
6367
Ok(())
6468
}
6569
```

examples/setup.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use iroh::{protocol::Router, Endpoint};
2+
use iroh_blobs::{net_protocol::Blobs, util::local_pool::LocalPool};
3+
4+
#[tokio::main]
5+
async fn main() -> anyhow::Result<()> {
6+
// create an iroh endpoint that includes the standard discovery mechanisms
7+
// we've built at number0
8+
let endpoint = Endpoint::builder().discovery_n0().bind().await?;
9+
10+
// spawn a local pool with one thread per CPU
11+
// for a single threaded pool use `LocalPool::single`
12+
let local_pool = LocalPool::default();
13+
14+
// create an in-memory blob store
15+
// use `iroh_blobs::net_protocol::Blobs::persistent` to load or create a
16+
// persistent blob store from a path
17+
let blobs = Blobs::memory().build(local_pool.handle(), &endpoint);
18+
19+
// turn on the "rpc" feature if you need to create blobs and tags clients
20+
let blobs_client = blobs.client();
21+
let tags_client = blobs_client.tags();
22+
23+
// build the router
24+
let router = Router::builder(endpoint)
25+
.accept(iroh_blobs::ALPN, blobs.clone())
26+
.spawn()
27+
.await?;
28+
29+
// do fun stuff with the blobs protocol!
30+
// make sure not to drop the local_pool before you are finished
31+
router.shutdown().await?;
32+
drop(local_pool);
33+
drop(tags_client);
34+
Ok(())
35+
}

0 commit comments

Comments
 (0)