use std::time::Duration;
use dynamic_config::{dynamic_config, RemoteWatch};
use dynamic_config_server::client::ConfigServer;
use serde::Deserialize;
#[dynamic_config]
#[derive(Debug, Deserialize)]
struct Billing {
host: String,
port: u16,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let base =
std::env::var("CONFIG_SERVER").unwrap_or_else(|_| "http://localhost:8155".to_owned());
let token = std::env::var("CONFIG_SERVER_TOKEN")
.unwrap_or_else(|_| "dev-token-not-for-production-32ch".to_owned());
let source = ConfigServer::new(&base, "billing", "prod").with_token(&token);
Billing::set_remote(source);
Billing::refresh_remote()?;
Billing::builder("billing").init()?;
let sink = Billing::remote_sink();
println!("{}:{}", Billing::current().host, Billing::current().port);
println!("\nwatching for 60 seconds — install a new document to move it");
let watcher = ConfigServer::new(&base, "billing", "prod").with_token(&token);
let watch = RemoteWatch::new();
let watching = watch.watching();
let thread = std::thread::spawn(move || {
watcher.watch(&watching, Duration::from_secs(5), |document| {
sink.apply(document)
.map_err(|error| {
eprintln!("a document did not apply: {error}");
error
})
.or(Ok(()))
})
});
std::thread::sleep(Duration::from_secs(60));
watch.stop();
thread.join().expect("the watch thread should end")?;
println!(
"\nfinal {}:{}",
Billing::current().host,
Billing::current().port
);
Ok(())
}