aboutsummaryrefslogtreecommitdiff
path: root/src/server.rs
diff options
context:
space:
mode:
authorQuentin <quentin@dufour.io>2023-12-27 16:35:43 +0000
committerQuentin <quentin@dufour.io>2023-12-27 16:35:43 +0000
commit6ff3c6f71efd802da422a371e6168ae528fb2ddc (patch)
tree62b5d7d9bc7fd2bf3defd1a85ae1b3f34cd4b8ee /src/server.rs
parent609dde413972ebeeb8cd658a5ec9f62b34b5c402 (diff)
parentea4cd48bba96027882a637df08e313af92a3db46 (diff)
downloadaerogramme-6ff3c6f71efd802da422a371e6168ae528fb2ddc.tar.gz
aerogramme-6ff3c6f71efd802da422a371e6168ae528fb2ddc.zip
Add storage behind a trait
Reviewed-on: https://git.deuxfleurs.fr/Deuxfleurs/aerogramme/pulls/32
Diffstat (limited to 'src/server.rs')
-rw-r--r--src/server.rs70
1 files changed, 39 insertions, 31 deletions
diff --git a/src/server.rs b/src/server.rs
index f0eb35f..28e0b27 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -1,6 +1,8 @@
+use std::io::Write;
+use std::path::PathBuf;
use std::sync::Arc;
-use anyhow::{bail, Result};
+use anyhow::Result;
use futures::try_join;
use log::*;
use tokio::sync::watch;
@@ -9,31 +11,59 @@ use crate::config::*;
use crate::imap;
use crate::lmtp::*;
use crate::login::ArcLoginProvider;
-use crate::login::{ldap_provider::*, static_provider::*, Region};
+use crate::login::{ldap_provider::*, static_provider::*};
pub struct Server {
lmtp_server: Option<Arc<LmtpServer>>,
imap_server: Option<imap::Server>,
+ pid_file: Option<PathBuf>,
}
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> {
+ tracing::info!("Init as companion");
+ let login = Arc::new(StaticLoginProvider::new(config.users).await?);
- 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.clone()).await?);
+ Ok(Self {
+ lmtp_server,
+ imap_server,
+ pid_file: config.pid,
+ })
+ }
+
+ pub async fn from_provider_config(config: ProviderConfig) -> Result<Self> {
+ tracing::info!("Init as provider");
+ let login: ArcLoginProvider = match config.users {
+ UserManagement::Static(x) => Arc::new(StaticLoginProvider::new(x).await?),
+ UserManagement::Ldap(x) => Arc::new(LdapLoginProvider::new(x)?),
};
+ let lmtp_server = Some(LmtpServer::new(config.lmtp, login.clone()));
+ let imap_server = Some(imap::new(config.imap, login.clone()).await?);
+
Ok(Self {
lmtp_server,
imap_server,
+ pid_file: config.pid,
})
}
pub async fn run(self) -> Result<()> {
- tracing::info!("Starting Aerogramme...");
+ let pid = std::process::id();
+ tracing::info!(pid = pid, "Starting main loops");
+
+ // write the pid file
+ if let Some(pid_file) = self.pid_file {
+ let mut file = std::fs::OpenOptions::new()
+ .write(true)
+ .create(true)
+ .truncate(true)
+ .open(pid_file)?;
+ file.write_all(pid.to_string().as_bytes())?;
+ drop(file);
+ }
let (exit_signal, provoke_exit) = watch_ctrl_c();
let _exit_on_err = move |err: anyhow::Error| {
@@ -60,28 +90,6 @@ impl Server {
}
}
-fn build(config: Config) -> Result<(ArcLoginProvider, Option<LmtpConfig>, Option<ImapConfig>)> {
- let s3_region = Region {
- name: config.aws_region.clone(),
- endpoint: config.s3_endpoint,
- };
- let k2v_region = Region {
- name: config.aws_region,
- endpoint: config.k2v_endpoint,
- };
-
- let lp: ArcLoginProvider = match (config.login_static, config.login_ldap) {
- (Some(st), None) => Arc::new(StaticLoginProvider::new(st, k2v_region, s3_region)?),
- (None, Some(ld)) => Arc::new(LdapLoginProvider::new(ld, k2v_region, s3_region)?),
- (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);