aerogramme/src/service.rs

63 lines
1.8 KiB
Rust
Raw Normal View History

2022-06-13 09:44:02 +00:00
use std::sync::Arc;
2022-06-03 15:26:25 +00:00
use std::task::{Context, Poll};
2022-06-07 10:38:59 +00:00
use anyhow::Result;
2022-06-03 15:26:25 +00:00
use boitalettres::errors::Error as BalError;
2022-06-03 15:37:39 +00:00
use boitalettres::proto::{Request, Response};
2022-06-14 08:19:24 +00:00
use boitalettres::server::accept::addr::AddrStream;
2022-06-03 15:26:25 +00:00
use futures::future::BoxFuture;
2022-06-09 08:43:38 +00:00
use futures::future::FutureExt;
2022-06-03 15:26:25 +00:00
use tower::Service;
2022-06-13 09:44:02 +00:00
use crate::session;
2022-06-15 16:40:39 +00:00
use crate::LoginProvider;
2022-06-09 08:43:38 +00:00
2022-06-07 10:38:59 +00:00
pub struct Instance {
2022-06-15 16:40:39 +00:00
login_provider: Arc<dyn LoginProvider + Send + Sync>,
2022-06-07 10:38:59 +00:00
}
impl Instance {
2022-06-15 16:40:39 +00:00
pub fn new(login_provider: Arc<dyn LoginProvider + Send + Sync>) -> Self {
Self { login_provider }
2022-06-07 10:38:59 +00:00
}
}
impl<'a> Service<&'a AddrStream> for Instance {
type Response = Connection;
type Error = anyhow::Error;
type Future = BoxFuture<'static, Result<Self::Response>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, addr: &'a AddrStream) -> Self::Future {
tracing::info!(remote_addr = %addr.remote_addr, local_addr = %addr.local_addr, "accept");
2022-06-15 16:40:39 +00:00
let lp = self.login_provider.clone();
async { Ok(Connection::new(lp)) }.boxed()
2022-06-07 10:38:59 +00:00
}
}
2022-06-03 15:26:25 +00:00
pub struct Connection {
2022-06-13 09:44:02 +00:00
session: session::Manager,
2022-06-03 15:26:25 +00:00
}
impl Connection {
2022-06-15 16:40:39 +00:00
pub fn new(login_provider: Arc<dyn LoginProvider + Send + Sync>) -> Self {
2022-06-14 08:19:24 +00:00
Self {
2022-06-15 16:40:39 +00:00
session: session::Manager::new(login_provider),
2022-06-14 08:19:24 +00:00
}
2022-06-03 15:37:39 +00:00
}
2022-06-03 15:26:25 +00:00
}
impl Service<Request> for Connection {
type Response = Response;
type Error = BalError;
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: Request) -> Self::Future {
tracing::debug!("Got request: {:#?}", req);
2022-06-13 09:44:02 +00:00
self.session.process(req)
2022-06-09 08:43:38 +00:00
}
}