49 lines
1.1 KiB
Rust
49 lines
1.1 KiB
Rust
use serde::Deserialize;
|
|
|
|
#[derive(Deserialize)]
|
|
struct Address {
|
|
Address: String,
|
|
Port: u16,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct TaggedAddresses {
|
|
lan_ipv4: Address,
|
|
wan_ipv4: Address,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct Weights {
|
|
Passing: u8,
|
|
Warning: u8,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
pub struct Service {
|
|
pub(crate) ID: String,
|
|
pub(crate) Service: String,
|
|
pub(crate) Tags: Vec<String>,
|
|
pub(crate) Port: u16,
|
|
pub(crate) Address: String,
|
|
pub(crate) TaggedAddresses: TaggedAddresses,
|
|
pub(crate) Weights: Weights,
|
|
pub(crate) EnableTagOverride: bool,
|
|
pub(crate) ContentHash: String,
|
|
pub(crate) Datacenter: String,
|
|
}
|
|
|
|
pub async fn get_service_address(consul_url: &str, service_name: &str) -> Result<(Service), Box<dyn std::error::Error>> {
|
|
let client = reqwest::Client::new();
|
|
let consul_service_url = format!("{}/v1/agent/service/{}", consul_url, service_name);
|
|
|
|
let response = client
|
|
.get(&consul_service_url)
|
|
.send()
|
|
.await?
|
|
.error_for_status()?
|
|
.json::<Service>()
|
|
.await?; // Ensure response is successful
|
|
|
|
Ok(response)
|
|
}
|