]>
Commit | Line | Data |
---|---|---|
c1a3505e LW |
1 | use std::fmt::Debug; |
2 | use std::sync::Mutex; | |
3 | ||
4 | #[cfg(any(feature = "pve-context", feature = "pbs-context"))] | |
5 | pub mod common; | |
6 | #[cfg(feature = "pbs-context")] | |
7 | pub mod pbs; | |
8 | #[cfg(feature = "pve-context")] | |
9 | pub mod pve; | |
f0bf95f5 LW |
10 | #[cfg(test)] |
11 | mod test; | |
c1a3505e LW |
12 | |
13 | /// Product-specific context | |
14 | pub trait Context: Send + Sync + Debug { | |
15 | /// Look up a user's email address from users.cfg | |
16 | fn lookup_email_for_user(&self, user: &str) -> Option<String>; | |
17 | /// Default mail author for mail-based targets | |
18 | fn default_sendmail_author(&self) -> String; | |
19 | /// Default from address for sendmail-based targets | |
20 | fn default_sendmail_from(&self) -> String; | |
21 | /// Proxy configuration for the current node | |
22 | fn http_proxy_config(&self) -> Option<String>; | |
9bea76c6 LW |
23 | // Return default config for built-in targets/matchers. |
24 | fn default_config(&self) -> &'static str; | |
c1a3505e LW |
25 | } |
26 | ||
f0bf95f5 | 27 | #[cfg(not(test))] |
c1a3505e | 28 | static CONTEXT: Mutex<Option<&'static dyn Context>> = Mutex::new(None); |
f0bf95f5 LW |
29 | #[cfg(test)] |
30 | static CONTEXT: Mutex<Option<&'static dyn Context>> = Mutex::new(Some(&test::TestContext)); | |
c1a3505e LW |
31 | |
32 | /// Set the product-specific context | |
33 | pub fn set_context(context: &'static dyn Context) { | |
34 | *CONTEXT.lock().unwrap() = Some(context); | |
35 | } | |
36 | ||
37 | /// Get product-specific context. | |
38 | /// | |
39 | /// Panics if the context has not been set yet. | |
40 | #[allow(unused)] // context is not used if all endpoint features are disabled | |
41 | pub(crate) fn context() -> &'static dyn Context { | |
42 | (*CONTEXT.lock().unwrap()).expect("context for proxmox-notify has not been set yet") | |
43 | } |