diff options
| -rw-r--r-- | examples/windmark.rs | 43 | ||||
| -rw-r--r-- | src/context/error.rs | 18 | ||||
| -rw-r--r-- | src/context/hook.rs | 17 | ||||
| -rw-r--r-- | src/context/route.rs | 18 | ||||
| -rw-r--r-- | src/handler/response/error.rs | 7 | ||||
| -rw-r--r-- | src/router.rs | 89 |
6 files changed, 82 insertions, 110 deletions
diff --git a/examples/windmark.rs b/examples/windmark.rs index 7c46d9d..687704d 100644 --- a/examples/windmark.rs +++ b/examples/windmark.rs @@ -80,22 +80,22 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { r.mount("/module", success!("This is a module!")); }); router.attach_async(Clicker::default()); - router.set_pre_route_callback(|context| { - info!( - "accepted connection from {} to {}", - context.tcp.peer_addr().unwrap().ip(), - context.url.to_string() - ) - }); - router.set_post_route_callback(|context, content| { - content.content = - content.content.replace("Welcome!", "Welcome to Windmark!"); - - info!( - "closed connection from {}", - context.tcp.peer_addr().unwrap().ip() - ) - }); + // router.set_pre_route_callback(|context| { + // info!( + // "accepted connection from {} to {}", + // context.tcp.peer_addr().unwrap().ip(), + // context.url.to_string() + // ) + // }); + // router.set_post_route_callback(|context, content| { + // content.content = + // content.content.replace("Welcome!", "Welcome to Windmark!"); + // + // info!( + // "closed connection from {}", + // context.tcp.peer_addr().unwrap().ip() + // ) + // }); router.add_header(|_| "```\nART IS COOL\n```\nhi".to_string()); router.add_footer(|_| "Copyright 2022".to_string()); router.add_footer(|context| { @@ -110,13 +110,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { .with_mime("text/plain") .clone() }); - router.mount( - "/ip", - success!( - context, - format!("Hello, {}", context.tcp.peer_addr().unwrap().ip()) - ), - ); router.mount("/test", success!("hi there\n=> / back")); router.mount( "/temporary-failure", @@ -211,8 +204,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { Response::success(*clicks) } }); - router.mount("/async-nothing", |_| { - async { Response::success("This is an async route.") } + router.mount("/async-nothing", |context| { + async move { Response::success(context.url.path()) } }); router.mount( "/async-macro", diff --git a/src/context/error.rs b/src/context/error.rs index eb1e00e..a9a4b85 100644 --- a/src/context/error.rs +++ b/src/context/error.rs @@ -17,24 +17,18 @@ // SPDX-License-Identifier: GPL-3.0-only use openssl::x509::X509; -use tokio::net::TcpStream; use url::Url; #[allow(clippy::module_name_repetitions)] -pub struct ErrorContext<'a> { - pub tcp: &'a TcpStream, - pub url: &'a Url, - pub certificate: &'a Option<X509>, +pub struct ErrorContext { + pub url: Url, + pub certificate: Option<X509>, } -impl<'a> ErrorContext<'a> { - pub const fn new( - tcp: &'a TcpStream, - url: &'a Url, - certificate: &'a Option<X509>, - ) -> Self { +impl ErrorContext { + #[must_use] + pub const fn new(url: Url, certificate: Option<X509>) -> Self { Self { - tcp, url, certificate, } diff --git a/src/context/hook.rs b/src/context/hook.rs index 44804fa..736696f 100644 --- a/src/context/hook.rs +++ b/src/context/hook.rs @@ -18,26 +18,23 @@ use matchit::Params; use openssl::x509::X509; -use tokio::net::TcpStream; use url::Url; #[allow(clippy::module_name_repetitions)] pub struct HookContext<'a> { - pub tcp: &'a TcpStream, - pub url: &'a Url, - pub params: Option<&'a Params<'a, 'a>>, - pub certificate: &'a Option<X509>, + pub url: Url, + pub params: Option<Params<'a, 'a>>, + pub certificate: Option<X509>, } impl<'a> HookContext<'a> { + #[must_use] pub const fn new( - tcp: &'a TcpStream, - url: &'a Url, - params: Option<&'a Params<'a, 'a>>, - certificate: &'a Option<X509>, + url: Url, + params: Option<Params<'a, 'a>>, + certificate: Option<X509>, ) -> Self { Self { - tcp, url, params, certificate, diff --git a/src/context/route.rs b/src/context/route.rs index 61eca0c..b3b105d 100644 --- a/src/context/route.rs +++ b/src/context/route.rs @@ -18,26 +18,24 @@ use matchit::Params; use openssl::x509::X509; -use tokio::net::TcpStream; use url::Url; #[allow(clippy::module_name_repetitions)] +#[derive(Clone)] pub struct RouteContext<'a> { - pub tcp: &'a TcpStream, - pub url: &'a Url, - pub params: &'a Params<'a, 'a>, - pub certificate: &'a Option<X509>, + pub url: Url, + pub params: Params<'a, 'a>, + pub certificate: Option<X509>, } impl<'a> RouteContext<'a> { + #[must_use] pub const fn new( - tcp: &'a TcpStream, - url: &'a Url, - params: &'a Params<'a, 'a>, - certificate: &'a Option<X509>, + url: Url, + params: Params<'a, 'a>, + certificate: Option<X509>, ) -> Self { Self { - tcp, url, params, certificate, diff --git a/src/handler/response/error.rs b/src/handler/response/error.rs index da57898..c433a46 100644 --- a/src/handler/response/error.rs +++ b/src/handler/response/error.rs @@ -19,10 +19,7 @@ use crate::{context::ErrorContext, Response}; #[allow(clippy::module_name_repetitions)] -pub trait ErrorResponse: - FnMut(ErrorContext<'_>) -> Response + Send + Sync -{ -} +pub trait ErrorResponse: FnMut(ErrorContext) -> Response + Send + Sync {} -impl<T> ErrorResponse for T where T: FnMut(ErrorContext<'_>) -> Response + Send + Sync +impl<T> ErrorResponse for T where T: FnMut(ErrorContext) -> Response + Send + Sync {} diff --git a/src/router.rs b/src/router.rs index 4b13f8d..b4cd0c7 100644 --- a/src/router.rs +++ b/src/router.rs @@ -348,44 +348,45 @@ impl Router { for module in &mut *self.async_modules.lock().await { module .on_pre_route(HookContext::new( - stream.get_ref(), - &url, - route.as_ref().map_or(None, |route| Some(&route.params)), - &stream.ssl().peer_certificate(), + url.clone(), + route + .as_ref() + .map_or(None, |route| Some(route.params.clone())), + stream.ssl().peer_certificate().clone(), )) .await; } for module in &mut *self.modules.lock().unwrap() { module.on_pre_route(HookContext::new( - stream.get_ref(), - &url, - route.as_ref().map_or(None, |route| Some(&route.params)), - &stream.ssl().peer_certificate(), + url.clone(), + route + .as_ref() + .map_or(None, |route| Some(route.params.clone())), + stream.ssl().peer_certificate().clone(), )); } (*self.pre_route_callback).lock().unwrap()(HookContext::new( - stream.get_ref(), - &url, - route.as_ref().map_or(None, |route| Some(&route.params)), - &stream.ssl().peer_certificate(), + url.clone(), + route + .as_ref() + .map_or(None, |route| Some(route.params.clone())), + stream.ssl().peer_certificate(), )); let peer_certificate = stream.ssl().peer_certificate(); let mut content = if let Ok(ref route) = route { let footers_length = (*self.footers.lock().unwrap()).len(); + let route_context = RouteContext::new( + url.clone(), + route.params.clone(), + stream.ssl().peer_certificate(), + ); for partial_header in &mut *self.headers.lock().unwrap() { - header.push_str(&format!( - "{}\n", - partial_header(RouteContext::new( - stream.get_ref(), - &url, - &route.params, - &stream.ssl().peer_certificate() - )), - )); + header + .push_str(&format!("{}\n", partial_header(route_context.clone()),)); } for (i, partial_footer) in { @@ -394,12 +395,7 @@ impl Router { } { footer.push_str(&format!( "{}{}", - partial_footer(RouteContext::new( - stream.get_ref(), - &url, - &route.params, - &stream.ssl().peer_certificate() - )), + partial_footer(route_context.clone()), if footers_length > 1 && i != footers_length - 1 { "\n" } else { @@ -409,48 +405,45 @@ impl Router { } let mut lock = (*route.value).lock().await; - let handler = lock.call(RouteContext::new( - stream.get_ref(), - &url, - &route.params, - &peer_certificate, - )); + let handler = lock.call(route_context); handler.await } else { (*self.error_handler).lock().unwrap()(ErrorContext::new( - stream.get_ref(), - &url, - &peer_certificate, + url.clone(), + peer_certificate.clone(), )) }; for module in &mut *self.async_modules.lock().await { module .on_post_route(HookContext::new( - stream.get_ref(), - &url, - route.as_ref().map_or(None, |route| Some(&route.params)), - &stream.ssl().peer_certificate(), + url.clone(), + route + .as_ref() + .map_or(None, |route| Some(route.params.clone())), + stream.ssl().peer_certificate().clone(), )) .await; } for module in &mut *self.modules.lock().unwrap() { module.on_post_route(HookContext::new( - stream.get_ref(), - &url, - route.as_ref().map_or(None, |route| Some(&route.params)), - &stream.ssl().peer_certificate(), + url.clone(), + route + .as_ref() + .map_or(None, |route| Some(route.params.clone())), + stream.ssl().peer_certificate().clone(), )); } (*self.post_route_callback).lock().unwrap()( HookContext::new( - stream.get_ref(), - &url, - route.as_ref().map_or(None, |route| Some(&route.params)), - &stream.ssl().peer_certificate(), + url.clone(), + route + .as_ref() + .map_or(None, |route| Some(route.params.clone())), + stream.ssl().peer_certificate(), ), &mut content, ); |