Merge branch 'reduce-arcs' into user-management
This commit is contained in:
commit
edbfd3ed78
12
src/lib.rs
12
src/lib.rs
|
@ -42,13 +42,12 @@ pub use types::*;
|
|||
pub const REQUEST_URI_MAX_LEN: usize = 1024;
|
||||
pub const GEMINI_PORT: u16 = 1965;
|
||||
|
||||
type Handler = Arc<dyn Fn(Request) -> HandlerResponse + Send + Sync>;
|
||||
type Handler = Box<dyn Fn(Request) -> HandlerResponse + Send + Sync>;
|
||||
pub (crate) type HandlerResponse = Pin<Box<dyn Future<Output = Result<Response>> + Send>>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Server {
|
||||
tls_acceptor: TlsAcceptor,
|
||||
listener: Arc<TcpListener>,
|
||||
routes: Arc<RoutingNode<Handler>>,
|
||||
timeout: Duration,
|
||||
complex_timeout: Option<Duration>,
|
||||
|
@ -61,9 +60,9 @@ impl Server {
|
|||
Builder::bind(addr)
|
||||
}
|
||||
|
||||
async fn serve(self) -> Result<()> {
|
||||
async fn serve(self, listener: TcpListener) -> Result<()> {
|
||||
loop {
|
||||
let (stream, _addr) = self.listener.accept().await
|
||||
let (stream, _addr) = listener.accept().await
|
||||
.context("Failed to accept client")?;
|
||||
let this = self.clone();
|
||||
|
||||
|
@ -358,7 +357,7 @@ impl<A: ToSocketAddrs> Builder<A> {
|
|||
H: Send + Sync + 'static + Fn(Request) -> F,
|
||||
F: Send + Sync + 'static + Future<Output = Result<Response>>
|
||||
{
|
||||
let wrapped = Arc::new(move|req| Box::pin((handler)(req)) as HandlerResponse);
|
||||
let wrapped = Box::new(move|req| Box::pin((handler)(req)) as HandlerResponse);
|
||||
self.routes.add_route(path, wrapped);
|
||||
self
|
||||
}
|
||||
|
@ -374,7 +373,6 @@ impl<A: ToSocketAddrs> Builder<A> {
|
|||
|
||||
let server = Server {
|
||||
tls_acceptor: TlsAcceptor::from(config),
|
||||
listener: Arc::new(listener),
|
||||
routes: Arc::new(self.routes),
|
||||
timeout: self.timeout,
|
||||
complex_timeout: self.complex_body_timeout_override,
|
||||
|
@ -382,7 +380,7 @@ impl<A: ToSocketAddrs> Builder<A> {
|
|||
manager: UserManager::new(self.data_dir)?,
|
||||
};
|
||||
|
||||
server.serve().await
|
||||
server.serve(listener).await
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ pub trait UserManagementRoutes: private::Sealed {
|
|||
) -> Self
|
||||
where
|
||||
UserData: Serialize + DeserializeOwned + 'static + Send + Sync,
|
||||
Handler: Send + Sync + 'static + Fn(Request, RegisteredUser<UserData>) -> F,
|
||||
Handler: Clone + Send + Sync + 'static + Fn(Request, RegisteredUser<UserData>) -> F,
|
||||
F: Send + Sync + 'static + Future<Output = Result<Response>>;
|
||||
}
|
||||
|
||||
|
@ -71,10 +71,9 @@ impl<A: ToSocketAddrs> UserManagementRoutes for crate::Builder<A> {
|
|||
) -> Self
|
||||
where
|
||||
UserData: Serialize + DeserializeOwned + 'static + Send + Sync,
|
||||
Handler: Send + Sync + 'static + Fn(Request, RegisteredUser<UserData>) -> F,
|
||||
Handler: Clone + Send + Sync + 'static + Fn(Request, RegisteredUser<UserData>) -> F,
|
||||
F: Send + Sync + 'static + Future<Output = Result<Response>>
|
||||
{
|
||||
let handler = std::sync::Arc::new(handler);
|
||||
self.add_route(path, move|request| {
|
||||
let handler = handler.clone();
|
||||
async move {
|
||||
|
|
Loading…
Reference in a new issue