use std::collections::HashMap; use serde::Deserialize; #[derive(Debug, Deserialize)] pub struct ServiceNode { pub ServiceAddress: String, pub ServicePort: u16, pub ServiceTags: Vec, pub ServiceMeta: HashMap, } pub async fn get_service_address(consul_url: &str, service_name: &str) -> Result, Box> { let client = reqwest::Client::new(); let consul_service_url = format!("{}/v1/catalog/service/{}", consul_url, service_name); let response = client .get(&consul_service_url) .send() .await?; if !response.status().is_success() { return Err(format!( "Failed to fetch service nodes for '{}': {}", service_name, response.status() ) .into()); } // Deserialize the response into a Vec let nodes: Vec = response.json().await?; if nodes.is_empty() { Err(format!("No nodes found for service '{}'", service_name).into()) } else { Ok(nodes) } } async fn get_services_with_tag( service_name: &str, tag: &str, consul_url: &str, ) -> Result, Box> { let url = format!("{}/v1/catalog/service/{}", consul_url, service_name); let client = reqwest::Client::new(); let response = client.get(&url).send().await?; if !response.status().is_success() { return Err(format!( "Failed to fetch service nodes for '{}': {}", service_name, response.status() ) .into()); } // Deserialize the response into a Vec let nodes: Vec = response.json().await?; // Filter nodes that include the specified tag let filtered_nodes = nodes .into_iter() .filter(|node| node.ServiceTags.contains(&tag.to_string())) .collect(); Ok(filtered_nodes) }