2016-03-12 18:45:19 +00:00
|
|
|
# Rocket
|
2016-03-12 18:54:38 +00:00
|
|
|
|
2016-04-05 02:04:46 +00:00
|
|
|
[![Build Status](https://travis-ci.com/SergioBenitez/rocket.svg?token=CVq3HTkPNimYtLm3RHCn&branch=master)](https://travis-ci.com/SergioBenitez/rocket)
|
|
|
|
|
2016-03-12 18:54:38 +00:00
|
|
|
Rocket is a work-in-progress web framework for Rust (nightly) with a focus on
|
2016-04-06 19:50:51 +00:00
|
|
|
ease-of-use, expressability, and speed. Here's an example of a complete Rocket
|
|
|
|
application:
|
2016-03-12 18:54:38 +00:00
|
|
|
|
|
|
|
```rust
|
|
|
|
#![feature(plugin)]
|
|
|
|
#![plugin(rocket_macros)]
|
|
|
|
|
|
|
|
extern crate rocket;
|
2016-03-15 03:46:55 +00:00
|
|
|
use rocket::Rocket;
|
2016-03-12 18:54:38 +00:00
|
|
|
|
2016-09-04 11:10:35 +00:00
|
|
|
#[get("/<name>/<age>")]
|
2016-04-06 19:50:51 +00:00
|
|
|
fn hello(name: &str, age: u8) -> String {
|
2016-04-03 11:28:09 +00:00
|
|
|
format!("Hello, {} year old named {}!", age, name)
|
2016-03-12 18:54:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2016-04-06 20:50:02 +00:00
|
|
|
Rocket::new("localhost", 8000).mount_and_launch("/hello", routes![hello]);
|
2016-03-12 18:54:38 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-04-06 19:50:51 +00:00
|
|
|
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.
|
|
|
|
|
2016-03-12 18:54:38 +00:00
|
|
|
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
|
|
|
|
above are required.
|
|
|
|
|
|
|
|
## Building
|
|
|
|
|
|
|
|
Try running the examples in the `examples/` folder. For instance, the following
|
2016-04-06 19:50:51 +00:00
|
|
|
sequence of commands builds and runs the `Hello, world!` example:
|
2016-03-12 18:54:38 +00:00
|
|
|
|
|
|
|
```
|
2016-04-06 19:50:51 +00:00
|
|
|
cd examples/hello_world
|
2016-03-12 18:54:38 +00:00
|
|
|
cargo run
|
|
|
|
```
|
|
|
|
|
2016-04-06 19:50:51 +00:00
|
|
|
Then visit `localhost:8000`. You should see `Hello, world!`.
|
2016-03-18 02:56:23 +00:00
|
|
|
|
2016-03-12 18:54:38 +00:00
|
|
|
### OS X
|
|
|
|
|
|
|
|
Apple has stopped shipping `openssl` with OS X.11. As such, if your build fails
|
2016-04-06 19:50:51 +00:00
|
|
|
to compile with some `openssl` related errors, you'll need to install `openssl`,
|
|
|
|
`cargo clean`, and then `cargo build` again. Here are some lightweight
|
|
|
|
instructions:
|
2016-03-12 18:54:38 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
brew install openssl
|
|
|
|
brew link --force openssl
|
|
|
|
export OPENSSL_INCLUDE_DIR=`brew --prefix openssl`/include
|
|
|
|
export OPENSSL_LIB_DIR=`brew --prefix openssl`/lib
|
|
|
|
```
|
|
|
|
|