aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorQuentin Dufour <quentin@deuxfleurs.fr>2022-06-03 11:38:01 +0200
committerQuentin Dufour <quentin@deuxfleurs.fr>2022-06-03 11:38:01 +0200
commit64e64bed433eb6d498add8fdf7c8405f0fa7e64c (patch)
tree37843d722eccc30ca40bd3a1e138e0c1f61ae40f
parentd0128a6d8a8e99976bae1be5aa7b3597893fac23 (diff)
downloadaerogramme-64e64bed433eb6d498add8fdf7c8405f0fa7e64c.tar.gz
aerogramme-64e64bed433eb6d498add8fdf7c8405f0fa7e64c.zip
Pass data through the stack
-rw-r--r--src/main.rs2
-rw-r--r--src/server.rs51
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<dyn LoginProvider>,
+pub struct Connection {
+ pub login_provider: Arc<Box<dyn LoginProvider + Send + Sync>>,
}
-struct Connection;
impl Service<Request> for Connection {
type Response = Response;
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 {
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<Arc<Self>> {
+impl Instance {
+ pub fn new(config: Config) -> Result<Self> {
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<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)?),
- (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<Self>) -> 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<Self> {
+ 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?;