Skip to content

Commit 8601cdd

Browse files
committed
Move common example code into mod
1 parent ed48692 commit 8601cdd

File tree

3 files changed

+42
-67
lines changed

3 files changed

+42
-67
lines changed

examples/common/mod.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use anyhow::Result;
2+
use iroh::SecretKey;
3+
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
4+
5+
/// Gets a secret key from the IROH_SECRET environment variable or generates a new random one.
6+
/// If the environment variable is set, it must be a valid string representation of a secret key.
7+
pub fn get_or_generate_secret_key() -> Result<SecretKey> {
8+
use std::{env, str::FromStr};
9+
10+
use anyhow::Context;
11+
use rand::thread_rng;
12+
if let Ok(secret) = env::var("IROH_SECRET") {
13+
// Parse the secret key from string
14+
SecretKey::from_str(&secret).context("Invalid secret key format")
15+
} else {
16+
// Generate a new random key
17+
let secret_key = SecretKey::generate(&mut thread_rng());
18+
println!(
19+
"Generated new secret key: {}",
20+
hex::encode(secret_key.to_bytes())
21+
);
22+
println!("To reuse this key, set the IROH_SECRET environment variable to this value");
23+
Ok(secret_key)
24+
}
25+
}
26+
27+
// set the RUST_LOG env var to one of {debug,info,warn} to see logging info
28+
pub fn setup_logging() {
29+
tracing_subscriber::registry()
30+
.with(tracing_subscriber::fmt::layer().with_writer(std::io::stderr))
31+
.with(EnvFilter::from_default_env())
32+
.try_init()
33+
.ok();
34+
}

examples/custom-protocol.rs

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,17 @@ use std::{
4040
sync::{Arc, Mutex},
4141
};
4242

43-
use anyhow::{Context, Result};
43+
use anyhow::Result;
4444
use clap::Parser;
4545
use iroh::{
4646
discovery::pkarr::PkarrResolver,
4747
endpoint::Connection,
4848
protocol::{AcceptError, ProtocolHandler, Router},
49-
Endpoint, NodeId, SecretKey,
49+
Endpoint, NodeId,
5050
};
5151
use iroh_blobs::{api::Store, store::mem::MemStore, BlobsProtocol, Hash};
52-
use tracing_subscriber::{prelude::*, EnvFilter};
52+
mod common;
53+
use common::{get_or_generate_secret_key, setup_logging};
5354

5455
#[derive(Debug, Parser)]
5556
pub struct Cli {
@@ -322,33 +323,3 @@ async fn read_and_print(store: &Store, hash: Hash) -> Result<()> {
322323
println!("{}: {message}", hash.fmt_short());
323324
Ok(())
324325
}
325-
326-
/// Set the RUST_LOG env var to one of {debug,info,warn} to see logging.
327-
fn setup_logging() {
328-
tracing_subscriber::registry()
329-
.with(tracing_subscriber::fmt::layer().with_writer(std::io::stderr))
330-
.with(EnvFilter::from_default_env())
331-
.try_init()
332-
.ok();
333-
}
334-
335-
/// Gets a secret key from the IROH_SECRET environment variable or generates a new random one.
336-
/// If the environment variable is set, it must be a valid string representation of a secret key.
337-
pub fn get_or_generate_secret_key() -> Result<SecretKey> {
338-
use std::{env, str::FromStr};
339-
340-
use rand::thread_rng;
341-
if let Ok(secret) = env::var("IROH_SECRET") {
342-
// Parse the secret key from string
343-
SecretKey::from_str(&secret).context("Invalid secret key format")
344-
} else {
345-
// Generate a new random key
346-
let secret_key = SecretKey::generate(&mut thread_rng());
347-
println!(
348-
"Generated new secret key: {}",
349-
hex::encode(secret_key.to_bytes())
350-
);
351-
println!("To reuse this key, set the IROH_SECRET environment variable to this value");
352-
Ok(secret_key)
353-
}
354-
}

examples/mdns-discovery.rs

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,9 @@ use iroh::{
1919
discovery::mdns::MdnsDiscovery, protocol::Router, Endpoint, PublicKey, RelayMode, SecretKey,
2020
};
2121
use iroh_blobs::{store::mem::MemStore, BlobsProtocol, Hash};
22-
use tracing_subscriber::{prelude::*, EnvFilter};
2322

24-
// set the RUST_LOG env var to one of {debug,info,warn} to see logging info
25-
pub fn setup_logging() {
26-
tracing_subscriber::registry()
27-
.with(tracing_subscriber::fmt::layer().with_writer(std::io::stderr))
28-
.with(EnvFilter::from_default_env())
29-
.try_init()
30-
.ok();
31-
}
23+
mod common;
24+
use common::{get_or_generate_secret_key, setup_logging};
3225

3326
#[derive(Debug, Parser)]
3427
#[command(version, about)]
@@ -64,13 +57,12 @@ async fn accept(path: &Path) -> Result<()> {
6457
}
6558

6659
let key = get_or_generate_secret_key()?;
67-
let discovery = MdnsDiscovery::new(key.public())?;
6860

6961
println!("Starting iroh node with mdns discovery...");
7062
// create a new node
7163
let endpoint = Endpoint::builder()
7264
.secret_key(key)
73-
.add_discovery(discovery)
65+
.add_discovery(MdnsDiscovery::builder())
7466
.relay_mode(RelayMode::Disabled)
7567
.bind()
7668
.await?;
@@ -97,7 +89,7 @@ async fn accept(path: &Path) -> Result<()> {
9789
async fn connect(node_id: PublicKey, hash: Hash, out: Option<PathBuf>) -> Result<()> {
9890
let key = SecretKey::generate(rand::rngs::OsRng);
9991
// todo: disable discovery publishing once https://github.com/n0-computer/iroh/issues/3401 is implemented
100-
let discovery = MdnsDiscovery::new(key.public())?;
92+
let discovery = MdnsDiscovery::builder();
10193

10294
println!("Starting iroh node with mdns discovery...");
10395
// create a new node
@@ -150,25 +142,3 @@ async fn main() -> anyhow::Result<()> {
150142
}
151143
Ok(())
152144
}
153-
154-
/// Gets a secret key from the IROH_SECRET environment variable or generates a new random one.
155-
/// If the environment variable is set, it must be a valid string representation of a secret key.
156-
pub fn get_or_generate_secret_key() -> Result<SecretKey> {
157-
use std::{env, str::FromStr};
158-
159-
use anyhow::Context;
160-
use rand::thread_rng;
161-
if let Ok(secret) = env::var("IROH_SECRET") {
162-
// Parse the secret key from string
163-
SecretKey::from_str(&secret).context("Invalid secret key format")
164-
} else {
165-
// Generate a new random key
166-
let secret_key = SecretKey::generate(&mut thread_rng());
167-
println!(
168-
"Generated new secret key: {}",
169-
hex::encode(secret_key.to_bytes())
170-
);
171-
println!("To reuse this key, set the IROH_SECRET environment variable to this value");
172-
Ok(secret_key)
173-
}
174-
}

0 commit comments

Comments
 (0)