Working integration v1

This commit is contained in:
Quentin 2022-06-02 16:29:16 +02:00
parent 109337b904
commit 2d1e6bfffb
Signed by: quentin
GPG key ID: E9602264D639FF68

View file

@ -12,6 +12,13 @@ use boitalettres::server::accept::addr::{AddrIncoming, AddrStream};
use boitalettres::server::Server as ImapServer; use boitalettres::server::Server as ImapServer;
use tracing_subscriber; use tracing_subscriber;
use std::task::{Context, Poll};
use tower::Service;
use std::future::Future;
use std::pin::Pin;
use std::error::Error;
pub struct Server { pub struct Server {
pub login_provider: Box<dyn LoginProvider>, pub login_provider: Box<dyn LoginProvider>,
} }
@ -21,6 +28,29 @@ async fn handle_req(req: Request) -> Result<Response> {
Ok(Response::ok("Done")?) Ok(Response::ok("Done")?)
} }
struct Echo;
impl Service<Request> for Echo {
type Response = Response;
type Error = Box<dyn Error + Send + Sync>;
type Future = Pin<Box<dyn futures::Future<Output = Result<Self::Response, Self::Error>> + Send>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: Request) -> Self::Future {
Box::pin(Echo::handle_req(req))
}
}
impl Echo {
async fn handle_req(req: Request) -> Result<Response, Box<dyn Error + Send + Sync>> {
println!("Got request: {:#?}", req);
Ok(Response::ok("Done").unwrap())
}
}
impl Server { impl Server {
pub fn new(config: Config) -> Result<Arc<Self>> { pub fn new(config: Config) -> Result<Arc<Self>> {
let s3_region = Region::Custom { let s3_region = Region::Custom {
@ -46,13 +76,18 @@ impl Server {
let incoming = AddrIncoming::new("127.0.0.1:4567").await?; let incoming = AddrIncoming::new("127.0.0.1:4567").await?;
let make_service = tower::service_fn(|addr: &AddrStream| { let make_service = tower::service_fn(|addr: &AddrStream| {
tracing::debug!(remote_addr = %addr.remote_addr, local_addr = %addr.local_addr, "accept"); tracing::debug!(remote_addr = %addr.remote_addr, local_addr = %addr.local_addr, "accept");
let service = tower::ServiceBuilder::new().service_fn(handle_req); //let service = tower::ServiceBuilder::new().service_fn(handle_req);
//let service = tower::service_fn(handle_req);
let service = Echo;
futures::future::ok::<_, std::convert::Infallible>(service) futures::future::ok::<_, std::convert::Infallible>(service)
//service
}); });
//println!("{:?}", make_service);
let server = ImapServer::new(incoming).serve(make_service); let server = ImapServer::new(incoming).serve(make_service);
let _ = server.await?; let _ = server.await?;