Updated README.

This commit is contained in:
Sergio Benitez 2016-04-06 12:50:51 -07:00
parent e00b1a535a
commit 33c4d89614
1 changed files with 16 additions and 12 deletions

View File

@ -3,8 +3,8 @@
[![Build Status](https://travis-ci.com/SergioBenitez/rocket.svg?token=CVq3HTkPNimYtLm3RHCn&branch=master)](https://travis-ci.com/SergioBenitez/rocket) [![Build Status](https://travis-ci.com/SergioBenitez/rocket.svg?token=CVq3HTkPNimYtLm3RHCn&branch=master)](https://travis-ci.com/SergioBenitez/rocket)
Rocket is a work-in-progress web framework for Rust (nightly) with a focus on Rocket is a work-in-progress web framework for Rust (nightly) with a focus on
ease-of-use, expressability, and speed. It currently does not work. But, when it ease-of-use, expressability, and speed. Here's an example of a complete Rocket
does, the following will be the canonical "Hello, world!" example: application:
```rust ```rust
#![feature(plugin)] #![feature(plugin)]
@ -13,17 +13,22 @@ does, the following will be the canonical "Hello, world!" example:
extern crate rocket; extern crate rocket;
use rocket::Rocket; use rocket::Rocket;
#[route(GET, path = "/hello/<name>/<age>")] #[route(GET, path = "/<name>/<age>")]
fn hello(name: &str, age: i8) -> String { fn hello(name: &str, age: u8) -> String {
format!("Hello, {} year old named {}!", age, name) format!("Hello, {} year old named {}!", age, name)
} }
fn main() { fn main() {
let mut rocket = Rocket::new("localhost", 8000); let mut rocket = Rocket::new("localhost", 8000);
rocket.mount_and_launch("/", routes![hello]); rocket.mount_and_launch("/hello", routes![hello]);
} }
``` ```
Visiting `localhost:8000/hello/John/58`, for example, will trigger the `hello`
route resulting in the string `Hello, 58 year old named John!` being sent to the
browser. If an `<age>` string was passed in that can't be parsed as a `u8`, the
route won't get called, resulting in a 404 error.
Rocket requires a nightly version of Rust as it makes heavy use of syntax Rocket requires a nightly version of Rust as it makes heavy use of syntax
extensions. This also means that the first two unwieldly lines in the Rust file extensions. This also means that the first two unwieldly lines in the Rust file
above are required. above are required.
@ -31,22 +36,21 @@ above are required.
## Building ## Building
Try running the examples in the `examples/` folder. For instance, the following Try running the examples in the `examples/` folder. For instance, the following
sequence of commands builds the `Hello, world!` example: sequence of commands builds and runs the `Hello, world!` example:
``` ```
cd examples/hello cd examples/hello_world
cargo build
cargo run cargo run
``` ```
Then visit `localhost:8000/hello/{some_name}`, replacing `{some_name}` with some Then visit `localhost:8000`. You should see `Hello, world!`.
name.
### OS X ### OS X
Apple has stopped shipping `openssl` with OS X.11. As such, if your build fails Apple has stopped shipping `openssl` with OS X.11. As such, if your build fails
compile, you'll need to install `openssl`, `cargo clean`, and then `cargo build` to compile with some `openssl` related errors, you'll need to install `openssl`,
again. Here are some lightweight instructions: `cargo clean`, and then `cargo build` again. Here are some lightweight
instructions:
``` ```
brew install openssl brew install openssl