diff options
author | Quentin Dufour <quentin@deuxfleurs.fr> | 2023-12-06 20:57:25 +0100 |
---|---|---|
committer | Quentin Dufour <quentin@deuxfleurs.fr> | 2023-12-06 20:57:25 +0100 |
commit | 3ddbce4529225fe004acde3ab2e95261673bafc3 (patch) | |
tree | 799b7fa0e1aa8d173c89a2124ffc2865f3d6ced0 /src/server.rs | |
parent | 2779837a3750c3b2964c3a4d5d43de25218bc605 (diff) | |
download | aerogramme-3ddbce4529225fe004acde3ab2e95261673bafc3.tar.gz aerogramme-3ddbce4529225fe004acde3ab2e95261673bafc3.zip |
WIP refactor
Diffstat (limited to 'src/server.rs')
-rw-r--r-- | src/server.rs | 40 |
1 files changed, 16 insertions, 24 deletions
diff --git a/src/server.rs b/src/server.rs index eca11ad..2321da8 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,6 +1,6 @@ use std::sync::Arc; -use anyhow::{bail, Result}; +use anyhow::Result; use futures::try_join; use log::*; use tokio::sync::watch; @@ -17,19 +17,24 @@ pub struct Server { } impl Server { - pub async fn new(config: Config) -> Result<Self> { - let (login, lmtp_conf, imap_conf) = build(config)?; + pub async fn from_companion_config(config: CompanionConfig) -> Result<Self> { + let login = Arc::new(StaticLoginProvider::new(config.users)?); - let lmtp_server = lmtp_conf.map(|cfg| LmtpServer::new(cfg, login.clone())); - let imap_server = match imap_conf { - Some(cfg) => Some(imap::new(cfg, login.clone()).await?), - None => None, + let lmtp_server = None; + let imap_server = Some(imap::new(config.imap, login).await?); + Ok(Self { lmtp_server, imap_server }) + } + + pub async fn from_provider_config(config: ProviderConfig) -> Result<Self> { + let login: ArcLoginProvider = match config.users { + UserManagement::Static(x) => Arc::new(StaticLoginProvider::new(x)?), + UserManagement::Ldap(x) => Arc::new(LdapLoginProvider::new(x)?), }; - Ok(Self { - lmtp_server, - imap_server, - }) + let lmtp_server = Some(LmtpServer::new(config.lmtp, login.clone())); + let imap_server = Some(imap::new(config.imap, login).await?); + + Ok(Self { lmtp_server, imap_server }) } pub async fn run(self) -> Result<()> { @@ -60,19 +65,6 @@ impl Server { } } -fn build(config: Config) -> Result<(ArcLoginProvider, Option<LmtpConfig>, Option<ImapConfig>)> { - let lp: ArcLoginProvider = match (config.login_static, config.login_ldap) { - (Some(st), None) => Arc::new(StaticLoginProvider::new(st)?), - (None, Some(ld)) => Arc::new(LdapLoginProvider::new(ld)?), - (Some(_), Some(_)) => { - bail!("A single login provider must be set up in config file") - } - (None, None) => bail!("No login provider is set up in config file"), - }; - - Ok((lp, config.lmtp, config.imap)) -} - pub fn watch_ctrl_c() -> (watch::Receiver<bool>, Arc<watch::Sender<bool>>) { let (send_cancel, watch_cancel) = watch::channel(false); let send_cancel = Arc::new(send_cancel); |