move repos into monorepo
This commit is contained in:
170
lib/clients/osiris/examples/complete.rs
Normal file
170
lib/clients/osiris/examples/complete.rs
Normal file
@@ -0,0 +1,170 @@
|
||||
//! Complete Osiris Client Example
|
||||
//!
|
||||
//! This example demonstrates the full CQRS pattern with Osiris:
|
||||
//! - Commands (writes) via Rhai scripts through Supervisor
|
||||
//! - Queries (reads) via REST API from Osiris server
|
||||
//!
|
||||
//! Prerequisites:
|
||||
//! - Redis running on localhost:6379
|
||||
//! - Supervisor running on localhost:3030
|
||||
//! - Osiris server running on localhost:8080
|
||||
//! - Osiris runner connected to Redis
|
||||
|
||||
use osiris_client::OsirisClient;
|
||||
use hero_supervisor_openrpc_client::SupervisorClient;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("🚀 Osiris Client - Complete Example\n");
|
||||
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
|
||||
|
||||
// Configuration
|
||||
let admin_secret = "807470fd1e1ccc3fb997a1d4177cceb31a68cb355a4412c8fd6e66e517e902be";
|
||||
let supervisor_url = "http://localhost:3030";
|
||||
let osiris_url = "http://localhost:8080";
|
||||
let runner_name = "osiris-queue";
|
||||
|
||||
// ========== Part 1: Setup Runner ==========
|
||||
println!("📋 Part 1: Runner Setup\n");
|
||||
|
||||
let supervisor = SupervisorClient::builder()
|
||||
.url(supervisor_url)
|
||||
.secret(admin_secret)
|
||||
.build()?;
|
||||
|
||||
// Register the runner
|
||||
println!("1. Registering runner '{}'...", runner_name);
|
||||
match supervisor.register_runner(runner_name).await {
|
||||
Ok(result) => println!(" ✅ Runner registered: {}\n", result),
|
||||
Err(e) => println!(" ⚠️ Registration failed (may already exist): {:?}\n", e),
|
||||
}
|
||||
|
||||
// List all runners
|
||||
println!("2. Listing all runners...");
|
||||
match supervisor.list_runners().await {
|
||||
Ok(runners) => {
|
||||
println!(" ✅ Found {} runner(s):", runners.len());
|
||||
for runner in runners {
|
||||
println!(" - {}", runner);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
Err(e) => println!(" ❌ Failed: {:?}\n", e),
|
||||
}
|
||||
|
||||
// ========== Part 2: Initialize Osiris Client ==========
|
||||
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
|
||||
println!("📋 Part 2: Osiris Client (CQRS Pattern)\n");
|
||||
|
||||
let client = OsirisClient::builder()
|
||||
.osiris_url(osiris_url)
|
||||
.supervisor_url(supervisor_url)
|
||||
.supervisor_secret(admin_secret)
|
||||
.runner_name(runner_name)
|
||||
.build()?;
|
||||
|
||||
println!("✅ Osiris client initialized");
|
||||
println!(" - Osiris URL: {}", osiris_url);
|
||||
println!(" - Supervisor URL: {}", supervisor_url);
|
||||
println!(" - Runner: {}\n", runner_name);
|
||||
|
||||
// ========== Part 3: Execute Simple Script ==========
|
||||
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
|
||||
println!("📋 Part 3: Execute Rhai Script\n");
|
||||
|
||||
let script = r#"
|
||||
print("Hello from Osiris!");
|
||||
let result = 40 + 2;
|
||||
print("The answer is: " + result);
|
||||
result
|
||||
"#;
|
||||
|
||||
println!("Executing script...");
|
||||
match client.execute_script(script).await {
|
||||
Ok(response) => {
|
||||
println!(" ✅ Script executed successfully!");
|
||||
println!(" Job ID: {}", response.job_id);
|
||||
println!(" Status: {}\n", response.status);
|
||||
}
|
||||
Err(e) => {
|
||||
println!(" ❌ Script execution failed: {}", e);
|
||||
println!(" Make sure the runner is connected to Redis!\n");
|
||||
}
|
||||
}
|
||||
|
||||
// ========== Part 4: CQRS Operations (if runner is connected) ==========
|
||||
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
|
||||
println!("📋 Part 4: CQRS Operations (Commands + Queries)\n");
|
||||
|
||||
// Create an API Key (Command via Rhai)
|
||||
println!("1. Creating API Key (Command via Rhai)...");
|
||||
let api_key = format!("test-key-{}", chrono::Utc::now().timestamp());
|
||||
match client.create_api_key(
|
||||
api_key.clone(),
|
||||
"Test Key".to_string(),
|
||||
"admin".to_string()
|
||||
).await {
|
||||
Ok(response) => {
|
||||
println!(" ✅ API Key created!");
|
||||
println!(" Job ID: {}", response.job_id);
|
||||
println!(" Status: {}\n", response.status);
|
||||
}
|
||||
Err(e) => println!(" ⚠️ Failed: {}\n", e),
|
||||
}
|
||||
|
||||
// Query the API Key (Query via REST)
|
||||
println!("2. Querying API Key (Query via REST)...");
|
||||
match client.get_api_key(&api_key).await {
|
||||
Ok(key) => {
|
||||
println!(" ✅ API Key retrieved!");
|
||||
println!(" Key: {:?}\n", key);
|
||||
}
|
||||
Err(e) => println!(" ⚠️ Not found yet: {}\n", e),
|
||||
}
|
||||
|
||||
// List all API Keys (Query via REST)
|
||||
println!("3. Listing all API Keys (Query via REST)...");
|
||||
match client.list_api_keys().await {
|
||||
Ok(keys) => {
|
||||
println!(" ✅ Found {} API key(s)", keys.len());
|
||||
for key in keys.iter().take(3) {
|
||||
println!(" - {:?}", key);
|
||||
}
|
||||
if keys.len() > 3 {
|
||||
println!(" ... and {} more", keys.len() - 3);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
Err(e) => println!(" ⚠️ Failed: {}\n", e),
|
||||
}
|
||||
|
||||
// ========== Part 5: Cleanup ==========
|
||||
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
|
||||
println!("📋 Part 5: Cleanup\n");
|
||||
|
||||
println!("Deleting test API Key (Command via Rhai)...");
|
||||
match client.delete_api_key(api_key.clone()).await {
|
||||
Ok(response) => {
|
||||
println!(" ✅ API Key deleted!");
|
||||
println!(" Job ID: {}", response.job_id);
|
||||
println!(" Status: {}\n", response.status);
|
||||
}
|
||||
Err(e) => println!(" ⚠️ Failed: {}\n", e),
|
||||
}
|
||||
|
||||
// ========== Summary ==========
|
||||
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
|
||||
println!("✅ Example Complete!\n");
|
||||
println!("Summary:");
|
||||
println!(" ✅ Runner registration working");
|
||||
println!(" ✅ Osiris client initialized");
|
||||
println!(" ✅ CQRS pattern demonstrated");
|
||||
println!(" - Commands via Rhai scripts");
|
||||
println!(" - Queries via REST API");
|
||||
println!("\nNext steps:");
|
||||
println!(" - Explore other CQRS methods (runners, jobs, etc.)");
|
||||
println!(" - Use template-based script generation");
|
||||
println!(" - Build your own Osiris-backed applications!");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user