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);