diff --git a/examples/hello_person/Cargo.toml b/examples/hello_person/Cargo.toml new file mode 100644 index 00000000..21381940 --- /dev/null +++ b/examples/hello_person/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "hello_person" +version = "0.0.1" +authors = ["Sergio Benitez "] + +[dependencies] +rocket = { path = "../../lib" } +rocket_macros = { path = "../../macros" } diff --git a/examples/hello/src/main.rs b/examples/hello_person/src/main.rs similarity index 53% rename from examples/hello/src/main.rs rename to examples/hello_person/src/main.rs index c81c84f6..de507481 100644 --- a/examples/hello/src/main.rs +++ b/examples/hello_person/src/main.rs @@ -3,12 +3,6 @@ extern crate rocket; use rocket::Rocket; -use std::fs::File; - -#[route(GET, path = "/")] -fn root() -> File { - File::open("/tmp/index.html").unwrap() -} #[route(GET, path = "/hello//")] fn hello(name: &str, age: i8) -> String { @@ -16,6 +10,5 @@ fn hello(name: &str, age: i8) -> String { } fn main() { - let rocket = Rocket::new("localhost", 8000); - rocket.mount_and_launch("/", routes![root, hello]); + Rocket::new("localhost", 8000).mount_and_launch("/", routes![hello]); } diff --git a/examples/hello/Cargo.toml b/examples/hello_world/Cargo.toml similarity index 88% rename from examples/hello/Cargo.toml rename to examples/hello_world/Cargo.toml index 06d53b5a..2e4a531d 100644 --- a/examples/hello/Cargo.toml +++ b/examples/hello_world/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "hello" +name = "hello_world" version = "0.0.1" authors = ["Sergio Benitez "] diff --git a/examples/hello_world/src/main.rs b/examples/hello_world/src/main.rs new file mode 100644 index 00000000..a1e7d9d5 --- /dev/null +++ b/examples/hello_world/src/main.rs @@ -0,0 +1,14 @@ +#![feature(plugin)] +#![plugin(rocket_macros)] + +extern crate rocket; +use rocket::Rocket; + +#[route(GET, path = "/")] +fn root() -> &'static str { + "Hello, world!" +} + +fn main() { + Rocket::new("localhost", 8000).mount_and_launch("/", routes![root]); +} diff --git a/examples/manual_routes/Cargo.toml b/examples/manual_routes/Cargo.toml new file mode 100644 index 00000000..05095dfe --- /dev/null +++ b/examples/manual_routes/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "manual_routes" +version = "0.0.1" +authors = ["Sergio Benitez "] + +[dependencies] +rocket = { path = "../../lib" } +rocket_macros = { path = "../../macros" } diff --git a/examples/manual_routes/src/main.rs b/examples/manual_routes/src/main.rs new file mode 100644 index 00000000..7f74f473 --- /dev/null +++ b/examples/manual_routes/src/main.rs @@ -0,0 +1,25 @@ +extern crate rocket; + +use rocket::{Rocket, Request, Response, Route}; +use rocket::Method::*; + +fn root(req: Request) -> Response<'static> { + let name = req.get_param::<&str>(0).unwrap_or("unnamed"); + Response::new(format!("Hello, {}!", name)) +} + +// TODO: Work with these lifetimes. +#[allow(dead_code)] +fn lifetime_root<'a>(req: Request<'a>) -> Response<'a> { + Response::new(req.get_uri()) +} + +fn main() { + let first = Route::new(Get, "/hello", root); + let second = Route::new(Get, "/hello/", root); + Rocket::new("localhost", 8000).mount_and_launch("/", &[&first, &second]); + + // This below _should_ work. + // let lifetime = Route::new(Get, "/other", lifetime_root); + // Rocket::new("localhost", 8000).mount_and_launch("/", &[&lifetime]); +} diff --git a/examples/static_files/Cargo.toml b/examples/static_files/Cargo.toml new file mode 100644 index 00000000..7d6a7512 --- /dev/null +++ b/examples/static_files/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "static_files" +version = "0.0.1" +authors = ["Sergio Benitez "] + +[dependencies] +rocket = { path = "../../lib" } +rocket_macros = { path = "../../macros" } diff --git a/examples/static_files/src/main.rs b/examples/static_files/src/main.rs new file mode 100644 index 00000000..3a55d065 --- /dev/null +++ b/examples/static_files/src/main.rs @@ -0,0 +1,21 @@ +#![feature(plugin)] +#![plugin(rocket_macros)] + +extern crate rocket; + +use rocket::Rocket; +use std::fs::File; + +#[route(GET, path = "/")] +fn index() -> File { + File::open("static/index.html").unwrap() +} + +#[route(GET, path = "/")] +fn files(file: &str) -> File { + File::open(format!("static/{}", file)).unwrap() +} + +fn main() { + Rocket::new("localhost", 8000).mount_and_launch("/", routes![index, files]); +} diff --git a/examples/static_files/static/index.html b/examples/static_files/static/index.html new file mode 100644 index 00000000..67ddd36a --- /dev/null +++ b/examples/static_files/static/index.html @@ -0,0 +1,5 @@ +

Hello, world!

+ + + A rocket icon. + diff --git a/examples/static_files/static/rocket-icon.jpg b/examples/static_files/static/rocket-icon.jpg new file mode 100644 index 00000000..b6000fb0 Binary files /dev/null and b/examples/static_files/static/rocket-icon.jpg differ diff --git a/lib/src/lib.rs b/lib/src/lib.rs index cea9d98b..f149f719 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -34,6 +34,16 @@ pub struct Route { pub handler: Handler<'static> } +impl Route { + pub fn new(method: Method, path: &'static str, handler: Handler<'static>) -> Route { + Route { + method: method, + path: path, + handler: handler + } + } +} + impl<'a> fmt::Display for Route { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{} {:?}", Green.paint(&self.method), Blue.paint(&self.path)) diff --git a/lib/src/response.rs b/lib/src/response.rs index 9c9c51a0..d38a15c7 100644 --- a/lib/src/response.rs +++ b/lib/src/response.rs @@ -81,11 +81,11 @@ impl Responder for File { res.headers_mut().set(header::ContentLength(size)); *(res.status_mut()) = StatusCode::Ok; - let mut s = String::new(); - self.read_to_string(&mut s).unwrap(); + let mut v = Vec::new(); + self.read_to_end(&mut v).unwrap(); let mut stream = res.start().unwrap(); - stream.write_all(s.as_bytes()).unwrap(); + stream.write_all(&v).unwrap(); } } diff --git a/lib/src/router/mod.rs b/lib/src/router/mod.rs index 0514593a..a275dbd8 100644 --- a/lib/src/router/mod.rs +++ b/lib/src/router/mod.rs @@ -33,9 +33,11 @@ impl Router { } // TODO: Make a `Router` trait with this function. Rename this `Router` - // struct to something like `RocketRouter`. + // struct to something like `RocketRouter`. If that happens, returning a + // `Route` structure is inflexible. Have it be an associated type. pub fn route<'b>(&'b self, method: Method, uri: &str) -> Option<&'b Route> { let mut matched_route = None; + let path = Path::new(uri); let num_components = path.components().count(); if let Some(routes) = self.routes.get(&(method, num_components)) {