From 2a58800b352e7cb51271522668d08758f2915f75 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Thu, 17 Mar 2016 03:29:55 -0700 Subject: [PATCH] initial implementation of collision detection in routing. --- examples/hello/src/main.rs | 26 ++++++++++++++++---------- lib/src/lib.rs | 13 +++++++++++-- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/examples/hello/src/main.rs b/examples/hello/src/main.rs index 20dea772..10c5b870 100644 --- a/examples/hello/src/main.rs +++ b/examples/hello/src/main.rs @@ -17,22 +17,28 @@ fn simple() -> File { File::open("/tmp/index.html").unwrap() } -// #[route(GET, path = "/")] -// fn simple2() -> &'static str { -// "Hello, world!" -// } +#[route(GET, path = "/hello/")] +fn simple2() -> &'static str { + "Hello, world!" +} #[route(GET, path = "/hello")] fn simple3() -> String { String::from("Hello, world!") } -// #[route(GET, path = "//")] -// fn simple4(name: &str, age: i8) -> &str { -// name -// } +#[route(GET, path = "//")] +fn simple4(name: &str, age: i8) -> &str { + name +} + +#[route(GET, path = "/something")] +fn simple5() -> &'static str { + "hi" +} fn main() { - let rocket = Rocket::new("localhost", 8000); - rocket.mount_and_launch("/", routes![simple, simple3]); + let mut rocket = Rocket::new("localhost", 8000); + rocket.mount("/", routes![simple, simple2, simple3, simple4, simple5]); + rocket.mount_and_launch("/hello/", routes![simple, simple3, simple4, simple5]); } diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 326979db..298bbd27 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -5,6 +5,7 @@ pub mod error; pub mod response; pub mod request; pub mod param; +pub mod router; use std::io::Write; @@ -13,6 +14,7 @@ pub use error::Error; pub use response::{Response, HypResponse, HypFresh, Responder}; pub use request::Request; pub use param::FromParam; +pub use router::Router; use hyper::server::Handler as HypHandler; use hyper::server::Request as HypRequest; @@ -33,7 +35,7 @@ pub struct Rocket { address: &'static str, port: isize, handler: Option>, // just for testing - paths: Vec<&'static str> // for now, to check for collisions + router: Router // mounts: HashMap<&'static str, Route<'a>> } @@ -54,7 +56,8 @@ impl Rocket { Rocket { address: address, port: port, - handler: None + handler: None, + router: Router::new() } } @@ -65,7 +68,9 @@ impl Rocket { println!("\t* INSTALLED: {} '{}'", route.method, route.path); self.handler = Some((*route).clone()); } + println!("\t* {} '{}'", route.method, route.path); + self.router.add_route(route.method.clone(), base, route.path); } self @@ -77,6 +82,10 @@ impl Rocket { } pub fn launch(self) { + if self.router.has_collisions() { + println!("Warning: route collisions detected!"); + } + let full_addr = format!("{}:{}", self.address, self.port); println!("🚀 Rocket has launched from {}...", full_addr); let _ = Server::http(full_addr.as_str()).unwrap().handle(self);