|
| 1 | +# Getting started |
| 2 | + |
| 3 | +The `iroh-blobs` protocol is meant to be use in conjunction with `iroh`. [Iroh](https://docs.rs/iroh/latest/iroh/index.html) is a networking library for making direct connections, these connections are what power the data transfers in `iroh-blobs`. |
| 4 | + |
| 5 | +Iroh provides a [`Router`](https://docs.rs/iroh/latest/iroh/protocol/struct.Router.html) that takes an [`Endpoint`](https://docs.rs/iroh/latest/iroh/endpoint/struct.Endpoint.html) and any protocols needed for the application. Similar to a router in webserver library, it runs a loop accepting incoming connections and routes them to the specific protocol handler, based on `ALPN`. |
| 6 | + |
| 7 | +Here is a basic example of how to set up `iroh-blobs` with `iroh`: |
| 8 | + |
| 9 | +```rust |
| 10 | +use iroh::{protocol::Router, Endpoint}; |
| 11 | +use iroh_blobs::{net_protocol::Blobs, util::local_pool::LocalPool}; |
| 12 | + |
| 13 | +#[tokio::main] |
| 14 | +async fn main() -> Result<(), std::fmt::Error> { |
| 15 | + // create an iroh endpoint that includes the standard discovery mechanisms |
| 16 | + // we've built at number0 |
| 17 | + let endpoint = Endpoint::builder().discovery_n0().bind().await.unwrap(); |
| 18 | + |
| 19 | + // spawn a local pool with one thread per CPU |
| 20 | + // for a single threaded pool use `LocalPool::single` |
| 21 | + let local_pool = LocalPool::default(); |
| 22 | + |
| 23 | + // create an in-memory blob store |
| 24 | + // use `iroh_blobs::net_protocol::Blobs::persistent` to load or create a |
| 25 | + // persistent blob store from a path |
| 26 | + let blobs = Blobs::memory().build(local_pool.handle(), &endpoint); |
| 27 | + |
| 28 | + // turn on the "rpc" feature if you need to create blobs and tags clients |
| 29 | + let blobs_client = blobs.clone().client(); |
| 30 | + let tags_client = blobs_client.tags(); |
| 31 | + |
| 32 | + // build the router |
| 33 | + let router = Router::builder(endpoint) |
| 34 | + .accept(iroh_blobs::ALPN, blobs) |
| 35 | + .spawn(); |
| 36 | + |
| 37 | + // do fun stuff with the blobs protocol! |
| 38 | + // make sure not to drop the local_pool before you are finished |
| 39 | + Ok(()) |
| 40 | +} |
| 41 | +``` |
0 commit comments