Pass data through the stack

This commit is contained in:
Quentin 2022-06-03 11:38:01 +02:00
parent d0128a6d8a
commit 64e64bed43
Signed by: quentin
GPG key ID: E9602264D639FF68
2 changed files with 38 additions and 19 deletions

View file

@ -122,7 +122,7 @@ async fn main() -> Result<()> {
Command::Server { config_file } => { Command::Server { config_file } => {
let config = read_config(config_file)?; let config = read_config(config_file)?;
let server = Server::new(config)?; let server = Server::new(config).await?;
server.run().await?; server.run().await?;
} }
Command::FirstLogin { Command::FirstLogin {

View file

@ -16,11 +16,10 @@ use std::task::{Context, Poll};
use tower::Service; use tower::Service;
use futures::future::BoxFuture; use futures::future::BoxFuture;
pub struct Server { pub struct Connection {
pub login_provider: Box<dyn LoginProvider>, pub login_provider: Arc<Box<dyn LoginProvider + Send + Sync>>,
} }
struct Connection;
impl Service<Request> for Connection { impl Service<Request> for Connection {
type Response = Response; type Response = Response;
type Error = anyhow::Error; type Error = anyhow::Error;
@ -58,8 +57,16 @@ impl Service<Request> for Connection {
}) })
} }
} }
impl Connection {
pub fn new(login_provider: Arc<Box<dyn LoginProvider + Send + Sync>>) -> Self {
Self { login_provider: login_provider }
}
}
pub struct Instance {
pub login_provider: Arc<Box<dyn LoginProvider + Send + Sync>>,
}
struct Instance;
impl<'a> Service<&'a AddrStream> for Instance { impl<'a> Service<&'a AddrStream> for Instance {
type Response = Connection; type Response = Connection;
type Error = anyhow::Error; type Error = anyhow::Error;
@ -71,12 +78,14 @@ impl<'a> Service<&'a AddrStream> for Instance {
fn call(&mut self, addr: &'a AddrStream) -> Self::Future { fn call(&mut self, addr: &'a AddrStream) -> Self::Future {
tracing::info!(remote_addr = %addr.remote_addr, local_addr = %addr.local_addr, "accept"); 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 Instance {
impl Server { pub fn new(config: Config) -> Result<Self> {
pub fn new(config: Config) -> Result<Arc<Self>> {
let s3_region = Region::Custom { let s3_region = Region::Custom {
name: config.aws_region.clone(), name: config.aws_region.clone(),
endpoint: config.s3_endpoint, endpoint: config.s3_endpoint,
@ -85,22 +94,32 @@ impl Server {
name: config.aws_region, name: config.aws_region,
endpoint: config.k2v_endpoint, endpoint: config.k2v_endpoint,
}; };
let login_provider: Box<dyn LoginProvider> = match (config.login_static, config.login_ldap) let login_provider: Arc<Box<dyn LoginProvider + Send + Sync>> = match (config.login_static, config.login_ldap)
{ {
(Some(st), None) => Box::new(StaticLoginProvider::new(st, k2v_region, s3_region)?), (Some(st), None) => Arc::new(Box::new(StaticLoginProvider::new(st, k2v_region, s3_region)?)),
(None, Some(ld)) => Box::new(LdapLoginProvider::new(ld, 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"), (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"), (None, None) => bail!("No login provider is set up in config file"),
}; };
Ok(Arc::new(Self { login_provider })) Ok(Self { login_provider })
}
}
pub struct Server {
pub incoming: AddrIncoming,
pub instance: Instance,
}
impl Server {
pub async fn new(config: Config) -> Result<Self> {
Ok(Self {
incoming: AddrIncoming::new("127.0.0.1:4567").await?,
instance: Instance::new(config)?,
})
} }
pub async fn run(self: &Arc<Self>) -> Result<()> { pub async fn run(self: Self) -> Result<()> {
// tracing_subscriber::fmt::init(); let server = ImapServer::new(self.incoming).serve(self.instance);
let incoming = AddrIncoming::new("127.0.0.1:4567").await?;
let server = ImapServer::new(incoming).serve(Instance);
let _ = server.await?; let _ = server.await?;
/*let creds = self.login_provider.login("quentin", "poupou").await?; /*let creds = self.login_provider.login("quentin", "poupou").await?;