initial implementation of collision detection in routing.

This commit is contained in:
Sergio Benitez 2016-03-17 03:29:55 -07:00
parent da2b0ed35a
commit 2a58800b35
2 changed files with 27 additions and 12 deletions

View File

@ -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 = "/<name>/<age>")]
// fn simple4(name: &str, age: i8) -> &str {
// name
// }
#[route(GET, path = "/<name>/<age>")]
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]);
}

View File

@ -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<Route<'static>>, // 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);