operaton_task_worker/
settings.rs1use config::Config;
2
3pub fn load_config_from_env(env_prefix: &str) -> ConfigParams {
6 let settings = Config::builder()
7 .add_source(config::Environment::with_prefix(env_prefix))
8 .build()
9 .unwrap();
10
11 settings.try_deserialize().unwrap()
12}
13
14use serde::{Deserialize, Serialize};
15
16use url::Url;
17
18#[derive(Serialize, Deserialize, Clone, Debug)]
20pub struct ConfigParams {
21 #[serde(default = "default_url")]
23 url: Url,
24
25 #[serde(default = "String::new")]
28 username: String,
29
30 #[serde(default = "String::new")]
33 password: String,
34
35 #[serde(default = "default_poll_interval")]
37 poll_interval: usize,
38
39 #[serde(default = "default_task_worker_id")]
40 id: String,
42
43 #[serde(default = "default_lock_duration")]
45 lock_duration: u64,
46}
47
48impl ConfigParams {
49 pub fn url(&self) -> &Url {
50 &self.url
51 }
52
53 pub fn username(&self) -> &str {
54 &self.username
55 }
56
57 pub fn password(&self) -> &str {
58 &self.password
59 }
60
61 pub fn poll_interval(&self) -> usize {
62 self.poll_interval
63 }
64
65 pub fn id(&self) -> &str { &self.id }
66
67 pub fn lock_duration(&self) -> u64 { self.lock_duration }
68
69 pub fn with_url(self, url: Url) -> Self {
70 let mut cloned_self = self.clone();
71 cloned_self.url = url;
72 cloned_self
73 }
74
75 pub fn with_auth(self, username: String, password: String) -> Self {
76 let mut cloned_self = self.clone();
77 cloned_self.username = username;
78 cloned_self.password = password;
79 cloned_self
80 }
81
82 pub fn with_poll_interval(self, poll_interval: usize) -> Self {
83 let mut cloned_self = self.clone();
84 cloned_self.poll_interval = poll_interval;
85 cloned_self
86 }
87
88 pub fn with_worker_id(self, id: String) -> Self {
89 let mut cloned_self = self.clone();
90 cloned_self.id = id;
91 cloned_self
92 }
93
94 pub fn with_lock_duration(self, lock_duration: u64) -> Self {
95 let mut cloned_self = self.clone();
96 cloned_self.lock_duration = lock_duration;
97 cloned_self
98 }
99}
100
101impl Default for ConfigParams {
102 fn default() -> Self {
103 Self {
104 url: default_url(),
105 username: String::new(),
106 password: String::new(),
107 poll_interval: default_poll_interval(),
108 id: default_task_worker_id(),
109 lock_duration: default_lock_duration(),
110 }
111 }
112}
113
114fn default_url() -> Url {
115 Url::parse("http://localhost:8080").unwrap()
116}
117
118fn default_poll_interval() -> usize { 500 }
120
121fn default_task_worker_id() -> String { "operaton_task_worker".to_string() }
122
123fn default_lock_duration() -> u64 { 60_000 }
124
125#[cfg(test)]
126mod test {
127 use super::*;
128
129 #[test]
130 fn test_config_params_builder_pattern() {
131 let config = ConfigParams::default()
132 .with_url(Url::parse("http://localhost:8080").unwrap())
133 .with_auth("user".to_string(), "pass".to_string())
134 .with_poll_interval(1000)
135 .with_worker_id("operaton_task_worker".to_string())
136 .with_lock_duration(12_345);
137
138 assert_eq!(config.url(), &Url::parse("http://localhost:8080").unwrap());
139 assert_eq!(config.username(), "user");
140 assert_eq!(config.password(), "pass");
141 assert_eq!(config.poll_interval(), 1000);
142 assert_eq!(config.id(), "operaton_task_worker");
143 assert_eq!(config.lock_duration(), 12_345);
144 }
145
146 #[test]
147 fn test_default_lock_duration() {
148 let cfg = ConfigParams::default();
149 assert_eq!(cfg.lock_duration(), default_lock_duration());
150 }
151}