Polished examples directory. Fixed `File` response bug.

This commit is contained in:
Sergio Benitez 2016-03-22 16:27:12 -07:00
parent 433a9119bd
commit 877b37c903
13 changed files with 107 additions and 13 deletions

View File

@ -0,0 +1,8 @@
[package]
name = "hello_person"
version = "0.0.1"
authors = ["Sergio Benitez <sb@sergio.bz>"]
[dependencies]
rocket = { path = "../../lib" }
rocket_macros = { path = "../../macros" }

View File

@ -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/<name>/<age>")]
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]);
}

View File

@ -1,5 +1,5 @@
[package]
name = "hello"
name = "hello_world"
version = "0.0.1"
authors = ["Sergio Benitez <sb@sergio.bz>"]

View File

@ -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]);
}

View File

@ -0,0 +1,8 @@
[package]
name = "manual_routes"
version = "0.0.1"
authors = ["Sergio Benitez <sb@sergio.bz>"]
[dependencies]
rocket = { path = "../../lib" }
rocket_macros = { path = "../../macros" }

View File

@ -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/<any>", 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]);
}

View File

@ -0,0 +1,8 @@
[package]
name = "static_files"
version = "0.0.1"
authors = ["Sergio Benitez <sb@sergio.bz>"]
[dependencies]
rocket = { path = "../../lib" }
rocket_macros = { path = "../../macros" }

View File

@ -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 = "/<file>")]
fn files(file: &str) -> File {
File::open(format!("static/{}", file)).unwrap()
}
fn main() {
Rocket::new("localhost", 8000).mount_and_launch("/", routes![index, files]);
}

View File

@ -0,0 +1,5 @@
<h1>Hello, world!</h1>
<a href="rocket-icon.jpg">
<img src="rocket-icon.jpg" alt="A rocket icon." height=200 width=200 />
</a>

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 KiB

View File

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

View File

@ -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();
}
}

View File

@ -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)) {