feature_probe_server_sdk/
config.rs

1use std::time::Duration;
2
3use reqwest::Client;
4use tracing::info;
5use url::Url;
6
7#[derive(Debug, Clone)]
8pub struct FPConfig {
9    pub remote_url: Url,
10    pub toggles_url: Option<Url>,
11    pub events_url: Option<Url>,
12    pub server_sdk_key: String,
13    pub refresh_interval: Duration,
14    pub http_client: Option<Client>,
15    pub start_wait: Option<Duration>,
16
17    #[cfg(feature = "realtime")]
18    pub realtime_url: Option<Url>,
19    #[cfg(feature = "realtime")]
20    pub realtime_path: Option<String>,
21    pub track_events: bool,
22}
23
24#[derive(Debug, Clone)]
25pub(crate) struct Config {
26    pub toggles_url: Url,
27    pub events_url: Url,
28    pub server_sdk_key: String,
29    pub refresh_interval: Duration,
30    pub http_client: Option<Client>,
31    pub start_wait: Option<Duration>,
32    pub track_events: bool,
33
34    #[cfg(feature = "realtime")]
35    pub realtime_url: Url,
36    #[cfg(feature = "realtime")]
37    pub realtime_path: String,
38    pub max_prerequisites_deep: u8,
39}
40
41impl Default for FPConfig {
42    fn default() -> Self {
43        Self {
44            server_sdk_key: "".to_owned(),
45            remote_url: Url::parse("https://featureprobe.io/server").unwrap(),
46            toggles_url: None,
47            events_url: None,
48            refresh_interval: Duration::from_secs(5),
49            start_wait: None,
50            http_client: None,
51
52            #[cfg(feature = "realtime")]
53            realtime_url: None,
54            #[cfg(feature = "realtime")]
55            realtime_path: None,
56            track_events: true,
57        }
58    }
59}
60
61impl Default for Config {
62    fn default() -> Self {
63        Self {
64            server_sdk_key: "".to_owned(),
65            toggles_url: Url::parse("https://featureprobe.io/server/api/server-sdk/toggles")
66                .unwrap(),
67            events_url: Url::parse("https://featureprobe.io/server/api/events").unwrap(),
68            track_events: true,
69            refresh_interval: Duration::from_secs(60),
70            start_wait: None,
71            http_client: None,
72
73            #[cfg(feature = "realtime")]
74            realtime_url: Url::parse("https://featureprobe.io/server/realtime").unwrap(),
75            #[cfg(feature = "realtime")]
76            realtime_path: "/server/realtime".to_owned(),
77            max_prerequisites_deep: 20,
78        }
79    }
80}
81
82impl FPConfig {
83    pub(crate) fn build(&self) -> Config {
84        info!("build_config from {:?}", self);
85        let remote_url = self.remote_url.to_string();
86        let remote_url = match remote_url.ends_with('/') {
87            true => remote_url,
88            false => remote_url + "/",
89        };
90
91        #[cfg(feature = "realtime")]
92        let realtime_url = match &self.realtime_url {
93            None => Url::parse(&(remote_url.clone() + "realtime")).expect("invalid realtime url"),
94            Some(url) => url.to_owned(),
95        };
96
97        #[cfg(feature = "realtime")]
98        let realtime_path = match &self.realtime_path {
99            Some(p) => p.to_owned(),
100            None => realtime_url.path().to_owned(),
101        };
102
103        let toggles_url = match &self.toggles_url {
104            None => Url::parse(&(remote_url.clone() + "api/server-sdk/toggles"))
105                .expect("invalid toggles url"),
106            Some(url) => url.to_owned(),
107        };
108
109        let events_url = match &self.events_url {
110            None => Url::parse(&(remote_url + "api/events")).expect("invalid events url"),
111            Some(url) => url.to_owned(),
112        };
113
114        Config {
115            toggles_url,
116            events_url,
117            server_sdk_key: self.server_sdk_key.clone(),
118            refresh_interval: self.refresh_interval,
119            start_wait: self.start_wait,
120            http_client: self.http_client.clone(),
121            track_events: self.track_events,
122            #[cfg(feature = "realtime")]
123            realtime_url,
124            #[cfg(feature = "realtime")]
125            realtime_path,
126            ..Default::default()
127        }
128    }
129}