mirror of https://github.com/rwf2/Rocket.git
Polished examples directory. Fixed `File` response bug.
This commit is contained in:
parent
433a9119bd
commit
877b37c903
|
@ -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" }
|
|
@ -3,12 +3,6 @@
|
||||||
|
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
use rocket::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>")]
|
#[route(GET, path = "/hello/<name>/<age>")]
|
||||||
fn hello(name: &str, age: i8) -> String {
|
fn hello(name: &str, age: i8) -> String {
|
||||||
|
@ -16,6 +10,5 @@ fn hello(name: &str, age: i8) -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let rocket = Rocket::new("localhost", 8000);
|
Rocket::new("localhost", 8000).mount_and_launch("/", routes![hello]);
|
||||||
rocket.mount_and_launch("/", routes![root, hello]);
|
|
||||||
}
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
[package]
|
[package]
|
||||||
name = "hello"
|
name = "hello_world"
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
authors = ["Sergio Benitez <sb@sergio.bz>"]
|
authors = ["Sergio Benitez <sb@sergio.bz>"]
|
||||||
|
|
|
@ -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]);
|
||||||
|
}
|
|
@ -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" }
|
|
@ -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]);
|
||||||
|
}
|
|
@ -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" }
|
|
@ -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]);
|
||||||
|
}
|
|
@ -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 |
|
@ -34,6 +34,16 @@ pub struct Route {
|
||||||
pub handler: Handler<'static>
|
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 {
|
impl<'a> fmt::Display for Route {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "{} {:?}", Green.paint(&self.method), Blue.paint(&self.path))
|
write!(f, "{} {:?}", Green.paint(&self.method), Blue.paint(&self.path))
|
||||||
|
|
|
@ -81,11 +81,11 @@ impl Responder for File {
|
||||||
res.headers_mut().set(header::ContentLength(size));
|
res.headers_mut().set(header::ContentLength(size));
|
||||||
*(res.status_mut()) = StatusCode::Ok;
|
*(res.status_mut()) = StatusCode::Ok;
|
||||||
|
|
||||||
let mut s = String::new();
|
let mut v = Vec::new();
|
||||||
self.read_to_string(&mut s).unwrap();
|
self.read_to_end(&mut v).unwrap();
|
||||||
|
|
||||||
let mut stream = res.start().unwrap();
|
let mut stream = res.start().unwrap();
|
||||||
stream.write_all(s.as_bytes()).unwrap();
|
stream.write_all(&v).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,9 +33,11 @@ impl Router {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Make a `Router` trait with this function. Rename this `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> {
|
pub fn route<'b>(&'b self, method: Method, uri: &str) -> Option<&'b Route> {
|
||||||
let mut matched_route = None;
|
let mut matched_route = None;
|
||||||
|
|
||||||
let path = Path::new(uri);
|
let path = Path::new(uri);
|
||||||
let num_components = path.components().count();
|
let num_components = path.components().count();
|
||||||
if let Some(routes) = self.routes.get(&(method, num_components)) {
|
if let Some(routes) = self.routes.get(&(method, num_components)) {
|
||||||
|
|
Loading…
Reference in New Issue