From dc18bf2d1cc88ee6713f4e2ea24de01262b123d1 Mon Sep 17 00:00:00 2001 From: Emi Tatsuo Date: Thu, 19 Nov 2020 22:33:44 -0500 Subject: [PATCH] Fix examples (& also bugs with args in lib.rs) I thought I was clever with Into :( --- examples/certificates.rs | 3 ++- examples/document.rs | 3 ++- examples/serve_dir.rs | 3 ++- src/lib.rs | 9 ++++++--- src/routing.rs | 6 +++--- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/examples/certificates.rs b/examples/certificates.rs index 541fbe5..143c71c 100644 --- a/examples/certificates.rs +++ b/examples/certificates.rs @@ -19,7 +19,8 @@ async fn main() -> Result<()> { let users = Arc::>>::default(); Server::bind(("0.0.0.0", GEMINI_PORT)) - .serve(move|req| handle_request(users.clone(), req)) + .add_route("/", move|req| handle_request(users.clone(), req)) + .serve() .await } diff --git a/examples/document.rs b/examples/document.rs index 8ff6bbb..cc889c6 100644 --- a/examples/document.rs +++ b/examples/document.rs @@ -12,7 +12,8 @@ async fn main() -> Result<()> { .init(); Server::bind(("localhost", GEMINI_PORT)) - .serve(handle_request) + .add_route("/",handle_request) + .serve() .await } diff --git a/examples/serve_dir.rs b/examples/serve_dir.rs index fd26ac4..de3e0b0 100644 --- a/examples/serve_dir.rs +++ b/examples/serve_dir.rs @@ -11,7 +11,8 @@ async fn main() -> Result<()> { .init(); Server::bind(("localhost", GEMINI_PORT)) - .serve(handle_request) + .add_route("/", handle_request) + .serve() .await } diff --git a/src/lib.rs b/src/lib.rs index fd68eb6..daf9a98 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -290,12 +290,15 @@ impl Builder { /// "endpoint". Entering a relative or malformed path will result in a panic. /// /// For more information about routing mechanics, see the docs for [`RoutingNode`]. - pub fn add_route(mut self, path: &'static str, handler: impl Into) -> Self { - self.routes.add_route(path, handler); + pub fn add_route(mut self, path: &'static str, handler: H) -> Self + where + H: Fn(Request) -> HandlerResponse + Send + Sync + 'static, + { + self.routes.add_route(path, Arc::new(handler)); self } - pub async fn serve(mut self) -> Result<()> { + pub async fn serve(mut self) -> Result<()> { let config = tls_config(&self.cert_path, &self.key_path) .context("Failed to create TLS config")?; diff --git a/src/routing.rs b/src/routing.rs index 2788af0..225a58e 100644 --- a/src/routing.rs +++ b/src/routing.rs @@ -76,7 +76,7 @@ impl RoutingNode { /// static strings. If you would like to add a string dynamically, please use /// [`RoutingNode::add_route_by_path()`] in order to appropriately deal with any /// errors that might arise. - pub fn add_route(&mut self, path: &'static str, handler: impl Into) { + pub fn add_route(&mut self, path: &'static str, handler: Handler) { let path: Path = path.try_into().expect("Malformed path route received"); self.add_route_by_path(path, handler).unwrap(); } @@ -87,7 +87,7 @@ impl RoutingNode { /// this method. /// /// For information about how routes work, see [`RoutingNode::match_path()`] - pub fn add_route_by_path(&mut self, mut path: Path, handler: impl Into) -> Result<(), ConflictingRouteError>{ + pub fn add_route_by_path(&mut self, mut path: Path, handler: Handler) -> Result<(), ConflictingRouteError>{ debug_assert!(path.is_absolute()); path.normalize(false); @@ -99,7 +99,7 @@ impl RoutingNode { if node.0.is_some() { Err(ConflictingRouteError()) } else { - node.0 = Some(handler.into()); + node.0 = Some(handler); Ok(()) } }