circles/research/launcher/examples/circle_launcher_example.rs
2025-07-08 22:49:47 +02:00

146 lines
5.1 KiB
Rust

use circles_launcher::{Circle, Launcher};
use rand::rngs::OsRng;
use sal_service_manager::create_service_manager;
use secp256k1::{PublicKey, Secp256k1, SecretKey};
const WORKER_BINARY: &str = "../target/release/worker";
const SERVER_BINARY: &str = "../target/release/server";
const REDIS_URL: &str = "redis://127.0.0.1/";
const PORT: u16 = 8080;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
run_examples().await
}
async fn run_examples() -> Result<(), Box<dyn std::error::Error>> {
// Generate valid secp256k1 keypairs for testing
let secp = Secp256k1::new();
let mut rng = OsRng;
let secret_key1 = SecretKey::new(&mut rng);
let public_key1 = PublicKey::from_secret_key(&secp, &secret_key1);
let pk1_hex = hex::encode(public_key1.serialize());
let secret_key2 = SecretKey::new(&mut rng);
let public_key2 = PublicKey::from_secret_key(&secp, &secret_key2);
let pk2_hex = hex::encode(public_key2.serialize());
let secret_key3 = SecretKey::new(&mut rng);
let public_key3 = PublicKey::from_secret_key(&secp, &secret_key3);
let pk3_hex = hex::encode(public_key3.serialize());
println!("Generated test public keys:");
println!(" PK1: {}", pk1_hex);
println!(" PK2: {}", pk2_hex);
println!(" PK3: {}", pk3_hex);
// Example 1: Simple launcher with single circle
println!("\n=== Example 1: Simple Launcher ===");
let launcher1 = Launcher {
service_manager: tokio::task::spawn_blocking(|| create_service_manager(None))
.await??,
circles: vec![Circle {
public_key: pk1_hex.clone(),
init_script: None,
}],
worker_binary: WORKER_BINARY.to_string(),
server_binary: SERVER_BINARY.to_string(),
redis_url: REDIS_URL.to_string(),
port: PORT,
};
match launcher1.launch().await {
Ok(_) => println!("Circle launched successfully!"),
Err(e) => println!("Failed to launch: {}", e),
}
launcher1.clean().await?;
// Example 2: Launcher with multiple circles
println!("\n=== Example 2: Multiple Circles ===");
let launcher2 = Launcher {
service_manager: tokio::task::spawn_blocking(|| create_service_manager(None))
.await??,
circles: vec![
Circle {
public_key: pk1_hex.clone(),
init_script: None,
},
Circle {
public_key: pk2_hex.clone(),
init_script: None,
},
],
worker_binary: WORKER_BINARY.to_string(),
server_binary: SERVER_BINARY.to_string(),
redis_url: REDIS_URL.to_string(),
port: PORT,
};
match launcher2.launch().await {
Ok(_) => println!("Multiple circles launched successfully!"),
Err(e) => println!("Failed to launch multiple circles: {}", e),
}
launcher2.clean().await?;
// Example 3: Multiple circles with initialization scripts
println!("\n=== Example 3: Multiple Circles with Init Scripts ===");
let launcher3 = Launcher {
service_manager: tokio::task::spawn_blocking(|| create_service_manager(None))
.await??,
circles: vec![
Circle {
public_key: pk1_hex.clone(),
init_script: Some("test_script.rhai".to_string()),
},
Circle {
public_key: pk2_hex.clone(),
init_script: Some("test_script.rhai".to_string()),
},
Circle {
public_key: pk3_hex.clone(),
init_script: Some("test_script.rhai".to_string()),
},
],
worker_binary: WORKER_BINARY.to_string(),
server_binary: SERVER_BINARY.to_string(),
redis_url: REDIS_URL.to_string(),
port: PORT,
};
match launcher3.launch().await {
Ok(_) => println!("Multiple circles with init scripts launched successfully!"),
Err(e) => println!("Failed to launch multiple circles with init scripts: {}", e),
}
launcher3.clean().await?;
// Example 4: Mixed configuration (some with scripts, some without)
println!("\n=== Example 4: Mixed Configuration ===");
let launcher4 = Launcher {
service_manager: tokio::task::spawn_blocking(|| create_service_manager(None))
.await??,
circles: vec![
Circle {
public_key: pk1_hex.clone(),
init_script: Some("test_script.rhai".to_string()),
},
Circle {
public_key: pk2_hex.clone(),
init_script: None,
},
Circle {
public_key: pk3_hex.clone(),
init_script: Some("test_script.rhai".to_string()),
},
],
worker_binary: WORKER_BINARY.to_string(),
server_binary: SERVER_BINARY.to_string(),
redis_url: REDIS_URL.to_string(),
port: PORT,
};
match launcher4.launch().await {
Ok(_) => println!("Mixed configuration launched successfully!"),
Err(e) => println!("Failed to launch mixed configuration: {}", e),
}
launcher4.clean().await?;
println!("\nAll examples completed.");
Ok(())
}