From 64e64bed433eb6d498add8fdf7c8405f0fa7e64c Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 3 Jun 2022 11:38:01 +0200 Subject: Pass data through the stack --- src/main.rs | 2 +- src/server.rs | 51 +++++++++++++++++++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3c2ef7f..8a8a255 100644 --- a/src/main.rs +++ b/src/main.rs @@ -122,7 +122,7 @@ async fn main() -> Result<()> { Command::Server { config_file } => { let config = read_config(config_file)?; - let server = Server::new(config)?; + let server = Server::new(config).await?; server.run().await?; } Command::FirstLogin { diff --git a/src/server.rs b/src/server.rs index 6f3f6ac..6aafe28 100644 --- a/src/server.rs +++ b/src/server.rs @@ -16,11 +16,10 @@ use std::task::{Context, Poll}; use tower::Service; use futures::future::BoxFuture; -pub struct Server { - pub login_provider: Box, +pub struct Connection { + pub login_provider: Arc>, } -struct Connection; impl Service for Connection { type Response = Response; type Error = anyhow::Error; @@ -58,8 +57,16 @@ impl Service for Connection { }) } } +impl Connection { + pub fn new(login_provider: Arc>) -> Self { + Self { login_provider: login_provider } + } +} + +pub struct Instance { + pub login_provider: Arc>, +} -struct Instance; impl<'a> Service<&'a AddrStream> for Instance { type Response = Connection; type Error = anyhow::Error; @@ -71,12 +78,14 @@ impl<'a> Service<&'a AddrStream> for Instance { fn call(&mut self, addr: &'a AddrStream) -> Self::Future { tracing::info!(remote_addr = %addr.remote_addr, local_addr = %addr.local_addr, "accept"); - Box::pin(async { Ok(Connection) }) + let lp = self.login_provider.clone(); + Box::pin(async move { + Ok(Connection::new(lp)) + }) } } - -impl Server { - pub fn new(config: Config) -> Result> { +impl Instance { + pub fn new(config: Config) -> Result { let s3_region = Region::Custom { name: config.aws_region.clone(), endpoint: config.s3_endpoint, @@ -85,22 +94,32 @@ impl Server { name: config.aws_region, endpoint: config.k2v_endpoint, }; - let login_provider: Box = match (config.login_static, config.login_ldap) + let login_provider: Arc> = match (config.login_static, config.login_ldap) { - (Some(st), None) => Box::new(StaticLoginProvider::new(st, k2v_region, s3_region)?), - (None, Some(ld)) => Box::new(LdapLoginProvider::new(ld, k2v_region, s3_region)?), + (Some(st), None) => Arc::new(Box::new(StaticLoginProvider::new(st, k2v_region, s3_region)?)), + (None, Some(ld)) => Arc::new(Box::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(Arc::new(Self { login_provider })) + Ok(Self { login_provider }) } +} - pub async fn run(self: &Arc) -> Result<()> { - // tracing_subscriber::fmt::init(); +pub struct Server { + pub incoming: AddrIncoming, + pub instance: Instance, +} - let incoming = AddrIncoming::new("127.0.0.1:4567").await?; +impl Server { + pub async fn new(config: Config) -> Result { + Ok(Self { + incoming: AddrIncoming::new("127.0.0.1:4567").await?, + instance: Instance::new(config)?, + }) + } - let server = ImapServer::new(incoming).serve(Instance); + pub async fn run(self: Self) -> Result<()> { + let server = ImapServer::new(self.incoming).serve(self.instance); let _ = server.await?; /*let creds = self.login_provider.login("quentin", "poupou").await?; -- cgit v1.2.3