aerogramme/src/server.rs

99 lines
2.9 KiB
Rust
Raw Normal View History

2022-05-19 12:33:49 +00:00
use anyhow::{bail, Result};
use std::sync::Arc;
use rusoto_signature::Region;
use crate::config::*;
use crate::login::{ldap_provider::*, static_provider::*, *};
use crate::mailbox::Mailbox;
2022-06-01 16:00:56 +00:00
use boitalettres::proto::{Request, Response};
use boitalettres::server::accept::addr::{AddrIncoming, AddrStream};
use boitalettres::server::Server as ImapServer;
use tracing_subscriber;
2022-06-02 14:29:16 +00:00
use std::task::{Context, Poll};
use tower::Service;
use std::future::Future;
use std::pin::Pin;
use std::error::Error;
2022-05-19 12:33:49 +00:00
pub struct Server {
pub login_provider: Box<dyn LoginProvider>,
}
2022-06-02 15:28:26 +00:00
struct Connection;
impl Service<Request> for Connection {
2022-06-02 14:29:16 +00:00
type Response = Response;
2022-06-02 15:04:50 +00:00
type Error = anyhow::Error;
type Future = Pin<Box<dyn futures::Future<Output = Result<Self::Response>> + Send>>;
2022-06-02 14:29:16 +00:00
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: Request) -> Self::Future {
2022-06-02 15:28:26 +00:00
Box::pin(async move {
println!("Got request: {:#?}", req);
Ok(Response::ok("Done")?)
})
2022-06-02 14:29:16 +00:00
}
}
2022-06-02 15:28:26 +00:00
struct Instance;
impl<'a> Service<&'a AddrStream> for Instance {
type Response = Connection;
2022-06-02 15:12:58 +00:00
type Error = anyhow::Error;
type Future = Pin<Box<dyn futures::Future<Output = Result<Self::Response>> + Send>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, addr: &'a AddrStream) -> Self::Future {
println!("{}, {}", addr.remote_addr, addr.local_addr);
2022-06-02 15:28:26 +00:00
Box::pin(async {
Ok(Connection)
})
2022-06-02 15:12:58 +00:00
}
}
2022-06-02 15:04:50 +00:00
2022-05-19 12:33:49 +00:00
impl Server {
pub fn new(config: Config) -> Result<Arc<Self>> {
let s3_region = Region::Custom {
2022-05-19 13:14:36 +00:00
name: config.aws_region.clone(),
2022-05-19 12:33:49 +00:00
endpoint: config.s3_endpoint,
};
let k2v_region = Region::Custom {
2022-05-19 13:14:36 +00:00
name: config.aws_region,
2022-05-19 12:33:49 +00:00
endpoint: config.k2v_endpoint,
};
let login_provider: Box<dyn LoginProvider> = 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(_), 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"),
};
2022-05-19 13:14:36 +00:00
Ok(Arc::new(Self { login_provider }))
2022-05-19 12:33:49 +00:00
}
pub async fn run(self: &Arc<Self>) -> Result<()> {
2022-06-01 16:00:56 +00:00
// tracing_subscriber::fmt::init();
let incoming = AddrIncoming::new("127.0.0.1:4567").await?;
2022-06-02 15:28:26 +00:00
let server = ImapServer::new(incoming).serve(Instance);
2022-06-01 16:00:56 +00:00
let _ = server.await?;
/*let creds = self.login_provider.login("quentin", "poupou").await?;
2022-05-19 12:33:49 +00:00
2022-05-19 13:14:36 +00:00
let mut mailbox = Mailbox::new(&creds, "TestMailbox".to_string()).await?;
2022-05-19 12:33:49 +00:00
2022-06-01 16:00:56 +00:00
mailbox.test().await?;*/
2022-05-19 12:33:49 +00:00
Ok(())
}
}