We are now able to watch

This commit is contained in:
Quentin 2020-05-22 10:33:09 +02:00
parent 2a6b440270
commit d583c17782
2 changed files with 22 additions and 8 deletions

View File

@ -1,6 +1,6 @@
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use std::collections::HashMap; use std::collections::HashMap;
use anyhow::Result; use anyhow::{Result, anyhow};
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct ServiceEntry { pub struct ServiceEntry {
@ -14,20 +14,32 @@ pub struct CatalogNode {
pub struct Consul { pub struct Consul {
client: reqwest::Client, client: reqwest::Client,
url: String url: String,
idx: Option<u64>
} }
impl Consul { impl Consul {
pub fn new(url: &str) -> Self { pub fn new(url: &str) -> Self {
return Self { return Self {
client: reqwest::Client::new(), client: reqwest::Client::new(),
url: url.to_string() url: url.to_string(),
idx: None
}; };
} }
pub async fn catalog_node(&self, host: &str) -> Result<CatalogNode> { pub async fn watch_node(&mut self, host: &str) -> Result<CatalogNode> {
let url = format!("{}/v1/catalog/node/{}", self.url, host); let url = match self.idx {
let resp: CatalogNode = self.client.get(&url).send().await?.json().await?; Some(i) => format!("{}/v1/catalog/node/{}?index={}", self.url, host, i),
None => format!("{}/v1/catalog/node/{}", self.url, host)
};
let http = self.client.get(&url).send().await?;
self.idx = match http.headers().get("X-Consul-Index") {
Some(v) => Some(v.to_str()?.parse::<u64>()?),
None => return Err(anyhow!("X-Consul-Index header not found"))
};
let resp: CatalogNode = http.json().await?;
return Ok(resp) return Ok(resp)
} }
} }

View File

@ -14,9 +14,11 @@ async fn main() {
let diplo = Diplonat::new().await.expect("Setup failed"); let diplo = Diplonat::new().await.expect("Setup failed");
diplo.listen().await.expect("A runtime error occured"); diplo.listen().await.expect("A runtime error occured");
*/ */
let c = Consul::new("http://127.0.0.1:8500"); let mut c = Consul::new("http://127.0.0.1:8500");
let cn = c.catalog_node("lheureduthe").await.expect("Failed to fetch API"); let cn = c.watch_node("lheureduthe").await.expect("Failed to fetch API");
println!("{:#?}", cn); println!("{:#?}", cn);
let cn2 = c.watch_node("lheureduthe").await.expect("Failed to fetch API");
println!("{:#?}", cn2);
/* /*
let gateway = match search_gateway(Default::default()).await { let gateway = match search_gateway(Default::default()).await {