mirror of https://github.com/rwf2/Rocket.git
Tweaks to the overview.
This commit is contained in:
parent
0972994285
commit
be50ddeca4
|
@ -9,10 +9,9 @@ detailed guide](guide). If you want to get started immediately, see
|
||||||
[quickstart](guide/quickstart) or the [getting started
|
[quickstart](guide/quickstart) or the [getting started
|
||||||
guide](guide/getting_started). Otherwise, welcome!
|
guide](guide/getting_started). Otherwise, welcome!
|
||||||
|
|
||||||
Rocket makes writing web applications easy, fast, and fun. Rather than just
|
Rocket makes writing web applications easy, fast, and fun. Below is a complete
|
||||||
_talk_ about how, we'd rather show you. Below is a complete Rocket application.
|
Rocket application. In fact, it's [one of many](thisexample) complete, runnable
|
||||||
In fact, it's [one of many](thisexample) complete, runnable examples in
|
examples in [Rocket's git repository](github). Can you figure out what it does?
|
||||||
[Rocket's GitHub](github). Try to see if you can figure it what it does.
|
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
#![feature(plugin)]
|
#![feature(plugin)]
|
||||||
|
@ -41,42 +40,44 @@ If you were to run this application, your console would show:
|
||||||
🚀 Rocket has launched from localhost:8000...
|
🚀 Rocket has launched from localhost:8000...
|
||||||
```
|
```
|
||||||
|
|
||||||
Here's a quick summary: this Rocket applications declares the `hello` route to
|
Here's a quick summary of what it does: first, on lines 7 - 10, it declares the
|
||||||
`GET /<name>/<age>`, which returns a `String` formatted with `name` and `age`
|
`hello` route to `GET /<name>/<age>`, which returns a `String` formatted with
|
||||||
from the dynamic path, on lines 7 - 10. Then, in the `main` function, it creates
|
`name` and `age` from the dynamic path. Then, in the `main` function, it creates
|
||||||
a new `Rocket` instance, mounts the `hello` route at `"/hello"`, and launches
|
a new `Rocket` instance, mounts the `hello` route at `"/hello"`, and launches
|
||||||
the application. That's it! Let's break it down.
|
the application. That's it! Let's break it down.
|
||||||
|
|
||||||
Let's start at the beginning: lines 1 and 2. Rocket depends on Rust nightly; it
|
Let's start with lines 1 and 2. Rocket depends on Rust nightly; it makes
|
||||||
makes extensive use of Rust's code generation facilities through compiler
|
extensive use of Rust's code generation facilities through compiler plugins.
|
||||||
plugins. Plugins are still experimental, so we have to tell Rust that we're okay
|
Plugins are still experimental, so we have to tell Rust that we're okay with
|
||||||
with that by writing `#![feature(plugin)]`. We also have to tell the compiler to
|
that by writing `#![feature(plugin)]`. We also have to tell the compiler to use
|
||||||
use Rocket's code generation crate during compilation with
|
Rocket's code generation crate during compilation with
|
||||||
`#![plugin(rocket_codegen)]`.
|
`#![plugin(rocket_codegen)]`. Lines 4 and 5 bring `rocket::Rocket` into the
|
||||||
|
namespace.
|
||||||
|
|
||||||
# Routes
|
# Routes
|
||||||
|
|
||||||
Lines 4 and 5 bring `rocket::Rocket` into the namespace. The fun begins on line
|
The fun begins on line 7, where the `hello` route and request handler are
|
||||||
7, where the `hello` route is declared. Let's talk about routes for a bit.
|
declared.
|
||||||
|
|
||||||
Every Rocket application is composed of some number of routes. A route is a
|
Rocket applications are composed primarily of request handlers and routes. A
|
||||||
combination of:
|
_request handler_ is a function that takes an arbitrary number of parameters and
|
||||||
|
returns a response. A _route_ is a combination of:
|
||||||
|
|
||||||
* A set of parameters to match an incoming request against.
|
* A set of parameters to match an incoming request against.
|
||||||
* A request handler to process the request and return a response.
|
* A request handler to process the request and return a response.
|
||||||
|
|
||||||
The set of parameters to match against includes static paths, dynamic paths,
|
The set of parameters to match against includes static paths, dynamic paths,
|
||||||
path segments, forms, query strings, and format specifiers. Rocket uses Rust
|
path segments, forms, query strings, and request format specifiers. Rocket uses
|
||||||
attributes, which look like function decorators in other languages, to make
|
Rust attributes, which look like function decorators in other languages, to make
|
||||||
declaring routes easy. In Rocket, you declare a route by annotating a function
|
declaring routes easy. Routes are declares by annotating a function with the set
|
||||||
with the set of parameters to match against. A complete route declaration looks
|
of parameters to match against. A complete route declaration looks like:
|
||||||
like:
|
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
#[get("/path/to/match/against")]
|
#[get("/index")]
|
||||||
|
fn index() -> &str { "Hello, World!" }
|
||||||
```
|
```
|
||||||
|
|
||||||
You can use `put`, `post`, `delete`, and `patch` in place of `get`.
|
You can also use `put`, `post`, `delete`, and `patch` in place of `get`.
|
||||||
|
|
||||||
## Dynamic Paths
|
## Dynamic Paths
|
||||||
|
|
||||||
|
@ -97,23 +98,24 @@ these segments won't be known until someone visits a URL that matches. For
|
||||||
example, if someone visit `Mike/21`, `<name>` will be `"Mike"`, and `<age>` will
|
example, if someone visit `Mike/21`, `<name>` will be `"Mike"`, and `<age>` will
|
||||||
be `21`. If someone else visits `Bob/91`, `<name>` and `<age>` will be `"Bob"`
|
be `21`. If someone else visits `Bob/91`, `<name>` and `<age>` will be `"Bob"`
|
||||||
and `91`, respectively. Rocket automatically parses dynamic path segments and
|
and `91`, respectively. Rocket automatically parses dynamic path segments and
|
||||||
passes them your request handler. You can immediately use the `name` and `age`
|
passes them to the request handler in variables with matching names. This means
|
||||||
in the handler - no parsing, no checking.
|
that `name` and `age` can be used immediately in the handler - no parsing, no
|
||||||
|
checking.
|
||||||
|
|
||||||
But wait: what happens if someone goes to a URL with an `<age>` that isn't a
|
But wait: what happens if someone goes to a URL with an `<age>` that isn't a
|
||||||
valid `u8`? In that case, Rocket doesn't call the handler. Instead, it tries
|
valid `u8`? In that case, Rocket doesn't call the handler. Instead, it
|
||||||
other matching routes and ultimately returns a `404` if all of them fail. If you
|
_forwards_ the request to the next matching route, if any, and ultimately
|
||||||
want to know if the user passed in a bad `<age>`, use a `Result<u8, &str>` or an
|
returns a `404` if all of them fail. If you want to know if the user passed in a
|
||||||
`Option<u8>` instead. Rocket has mechanisms to handle route collisions, where
|
bad `<age>`, simply use a `Result<u8, &str>` or an `Option<u8>` type for `age`
|
||||||
multiple routes can match the same URLs, too. For more details on routing, see
|
instead. For more details on routing, route collisions, and more see the
|
||||||
the [routing](guide/routing) chapter of the guide.
|
[routing](guide/routing) chapter of the guide.
|
||||||
|
|
||||||
Oh, one more thing before we move on! Notice how dynamic path parameters can be
|
Oh, one more thing before we move on! Notice how dynamic path parameters can be
|
||||||
of different types? Actually, path parameters can be of _any_ type, as long as
|
of different types? Actually, path parameters can be of _any_ type, as long as
|
||||||
that type implements Rocket's `FromParam` trait. Rocket uses the `FromParam`
|
that type implements Rocket's `FromParam` trait. Rocket uses the `FromParam`
|
||||||
implementation to parse and validate the parameter for you automatically. We've
|
implementation to parse and validate the parameter for you automatically. We've
|
||||||
implemented `FromParam` for every reasonable type in the standard library. See
|
implemented `FromParam` for plenty of types in the standard library. See the
|
||||||
the [FromParam](/docs/FromParam) documentation for more.
|
[FromParam](docs) documentation for more.
|
||||||
|
|
||||||
## Mounting
|
## Mounting
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue