-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(viz): implement Handler for Responder
- Loading branch information
Showing
4 changed files
with
42 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,67 @@ | ||
use std::{convert::Infallible, future::Future, net::SocketAddr, pin::Pin, sync::Arc}; | ||
use std::{convert::Infallible, net::SocketAddr, sync::Arc}; | ||
|
||
use crate::{ | ||
types::RouteInfo, Body, Handler, Incoming, IntoResponse, Method, Request, Response, StatusCode, | ||
Tree, | ||
future::{FutureExt, TryFutureExt}, | ||
types::RouteInfo, | ||
Body, BoxFuture, Handler, Incoming, IntoResponse, Method, Request, Response, StatusCode, Tree, | ||
}; | ||
|
||
/// Handles the HTTP [`Request`] and retures the HTTP [`Response`]. | ||
#[derive(Debug)] | ||
pub struct Responder { | ||
tree: Arc<Tree>, | ||
tree: Tree, | ||
addr: Option<SocketAddr>, | ||
} | ||
|
||
impl Responder { | ||
/// Creates a Responder for handling the [`Request`]. | ||
#[must_use] | ||
pub fn new(tree: Arc<Tree>, addr: Option<SocketAddr>) -> Self { | ||
pub fn new(tree: Tree, addr: Option<SocketAddr>) -> Self { | ||
Self { tree, addr } | ||
} | ||
} | ||
|
||
impl Handler<Request<Incoming>> for Responder { | ||
type Output = Result<Response, Infallible>; | ||
|
||
/// Serves a request and returns a response. | ||
async fn serve( | ||
mut req: Request<Incoming>, | ||
tree: Arc<Tree>, | ||
addr: Option<SocketAddr>, | ||
) -> Result<Response, Infallible> { | ||
fn call(&self, mut req: Request<Incoming>) -> BoxFuture<Self::Output> { | ||
let method = req.method().clone(); | ||
let path = req.uri().path().to_owned(); | ||
let path = req.uri().path().to_string(); | ||
|
||
let Some((handler, route)) = tree.find(&method, &path).or_else(|| { | ||
let matched = self.tree.find(&method, &path).or_else(|| { | ||
if method == Method::HEAD { | ||
tree.find(&Method::GET, &path) | ||
self.tree.find(&Method::GET, &path) | ||
} else { | ||
None | ||
} | ||
}) else { | ||
return Ok(StatusCode::NOT_FOUND.into_response()); | ||
}; | ||
}); | ||
|
||
if let Some((handler, route)) = matched { | ||
req.extensions_mut().insert(self.addr); | ||
req.extensions_mut().insert(Arc::from(RouteInfo { | ||
id: *route.id, | ||
pattern: route.pattern(), | ||
params: route.params().into(), | ||
})); | ||
|
||
req.extensions_mut().insert(addr); | ||
req.extensions_mut().insert(Arc::from(RouteInfo { | ||
id: *route.id, | ||
pattern: route.pattern(), | ||
params: route.params().into(), | ||
})); | ||
// req.set_state(tree.clone()); | ||
Ok(handler | ||
.call(req.map(Body::Incoming)) | ||
.await | ||
.unwrap_or_else(IntoResponse::into_response)) | ||
Box::pin( | ||
handler | ||
.call(req.map(Body::Incoming)) | ||
.unwrap_or_else(IntoResponse::into_response) | ||
.map(Result::Ok), | ||
) | ||
} else { | ||
Box::pin(async { Ok(StatusCode::NOT_FOUND.into_response()) }) | ||
} | ||
} | ||
} | ||
|
||
impl hyper::service::Service<Request<Incoming>> for Responder { | ||
type Response = Response; | ||
type Error = Infallible; | ||
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>; | ||
type Future = BoxFuture<Result<Self::Response, Self::Error>>; | ||
|
||
#[inline] | ||
fn call(&self, req: Request<Incoming>) -> Self::Future { | ||
Box::pin(Self::serve(req, self.tree.clone(), self.addr)) | ||
Handler::call(self, req) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters