Skip to content

Commit f4de34e

Browse files
committed
Add example implementation using async source
Signed-off-by: Matthias Beyer <[email protected]> Reviewed-by: Matthias Beyer <[email protected]>
1 parent c708d44 commit f4de34e

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ json5_rs = { version = "0.3", optional = true, package = "json5" }
4040
serde_derive = "1.0.8"
4141
float-cmp = "0.8"
4242
chrono = { version = "0.4", features = ["serde"] }
43+
tokio = { version = "1", features = ["rt-multi-thread", "macros", "fs", "io-util", "time"]}
44+
warp = "0.3.1"
45+
futures = "0.3.15"
46+
reqwest = "0.11.3"

examples/async_source/main.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use std::{collections::HashMap, error::Error};
2+
3+
use config::{builder::AsyncState, AsyncSource, ConfigBuilder, ConfigError, FileFormat};
4+
5+
use async_trait::async_trait;
6+
use futures::{select, FutureExt};
7+
use warp::Filter;
8+
9+
// Example below presents sample configuration server and client.
10+
//
11+
// Server serves simple configuration on HTTP endpoint.
12+
// Client consumes it using custom HTTP AsyncSource built on top of reqwest.
13+
14+
#[tokio::main]
15+
async fn main() -> Result<(), Box<dyn Error>> {
16+
select! {
17+
r = run_server().fuse() => r,
18+
r = run_client().fuse() => r
19+
}
20+
}
21+
22+
async fn run_server() -> Result<(), Box<dyn Error>> {
23+
let service = warp::path("configuration").map(|| r#"{ "value" : 123 }"#);
24+
25+
println!("Running server on localhost:5001");
26+
27+
warp::serve(service).bind(([127, 0, 0, 1], 5001)).await;
28+
29+
Ok(())
30+
}
31+
32+
async fn run_client() -> Result<(), Box<dyn Error>> {
33+
// Good enough for an example to allow server to start
34+
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
35+
36+
let config = ConfigBuilder::<AsyncState>::default()
37+
.add_async_source(HttpSource {
38+
uri: "http://localhost:5001/configuration".into(),
39+
format: FileFormat::Json,
40+
})
41+
.build()
42+
.await?;
43+
44+
println!("Config value is {}", config.get::<String>("value")?);
45+
46+
Ok(())
47+
}
48+
49+
// Actual implementation of AsyncSource can be found below
50+
51+
#[derive(Debug)]
52+
struct HttpSource {
53+
uri: String,
54+
format: FileFormat,
55+
}
56+
57+
impl HttpSource {
58+
async fn call(&self) -> Result<String, reqwest::Error> {
59+
reqwest::get(&self.uri).await?.text().await
60+
}
61+
}
62+
63+
#[async_trait]
64+
impl AsyncSource for HttpSource {
65+
async fn collect(&self) -> Result<HashMap<String, config::Value>, ConfigError> {
66+
self.call()
67+
.await
68+
.map_err(|e| ConfigError::Foreign(Box::new(e)))
69+
.and_then(|text| {
70+
self.format
71+
.parse(Some(&self.uri), &text)
72+
.map_err(|e| ConfigError::Foreign(e))
73+
})
74+
}
75+
}

0 commit comments

Comments
 (0)