mirror of
https://github.com/SergioBenitez/Rocket.git
synced 2026-02-06 10:48:05 +00:00
This commit introduces support for method-less routes and route
attributes, which match _any_ valid method: `#[route("/")]`. The `Route`
structure's `method` field is now accordingly of type `Option<Route>`.
The syntax for the `route` attribute has changed in a breaking manner.
To set a method, a key/value of `method = NAME` must be introduced:
```rust
#[route("/", method = GET)]
```
If the method's name is a valid identifier, it can be used without
quotes. Otherwise it must be quoted:
```rust
// `GET` is a valid identifier, but `VERSION-CONTROL` is not
#[route("/", method = "VERSION-CONTROL")]
```
Closes #2731.
82 lines
1.9 KiB
Rust
82 lines
1.9 KiB
Rust
#[macro_use] extern crate rocket;
|
||
|
||
#[cfg(test)] mod tests;
|
||
|
||
#[derive(FromFormField)]
|
||
enum Lang {
|
||
#[field(value = "en")]
|
||
English,
|
||
#[field(value = "ru")]
|
||
#[field(value = "ру")]
|
||
Russian
|
||
}
|
||
|
||
#[derive(FromForm)]
|
||
struct Options<'r> {
|
||
emoji: bool,
|
||
name: Option<&'r str>,
|
||
}
|
||
|
||
// Try visiting:
|
||
// http://127.0.0.1:8000/hello/world
|
||
#[get("/world")]
|
||
fn world() -> &'static str {
|
||
"Hello, world!"
|
||
}
|
||
|
||
// Try visiting:
|
||
// http://127.0.0.1:8000/hello/мир
|
||
#[get("/мир")]
|
||
fn mir() -> &'static str {
|
||
"Привет, мир!"
|
||
}
|
||
|
||
// Try visiting:
|
||
// http://127.0.0.1:8000/wave/Rocketeer/100
|
||
#[get("/<name>/<age>", rank = 2)]
|
||
fn wave(name: &str, age: u8) -> String {
|
||
format!("👋 Hello, {} year old named {}!", age, name)
|
||
}
|
||
|
||
// Note: without the `..` in `opt..`, we'd need to pass `opt.emoji`, `opt.name`.
|
||
//
|
||
// Try visiting:
|
||
// http://127.0.0.1:8000/?emoji
|
||
// http://127.0.0.1:8000/?name=Rocketeer
|
||
// http://127.0.0.1:8000/?lang=ру
|
||
// http://127.0.0.1:8000/?lang=ру&emoji
|
||
// http://127.0.0.1:8000/?emoji&lang=en
|
||
// http://127.0.0.1:8000/?name=Rocketeer&lang=en
|
||
// http://127.0.0.1:8000/?emoji&name=Rocketeer
|
||
// http://127.0.0.1:8000/?name=Rocketeer&lang=en&emoji
|
||
// http://127.0.0.1:8000/?lang=ru&emoji&name=Rocketeer
|
||
#[get("/?<lang>&<opt..>")]
|
||
fn hello(lang: Option<Lang>, opt: Options<'_>) -> String {
|
||
let mut greeting = String::new();
|
||
if opt.emoji {
|
||
greeting.push_str("👋 ");
|
||
}
|
||
|
||
match lang {
|
||
Some(Lang::Russian) => greeting.push_str("Привет"),
|
||
Some(Lang::English) => greeting.push_str("Hello"),
|
||
None => greeting.push_str("Hi"),
|
||
}
|
||
|
||
if let Some(name) = opt.name {
|
||
greeting.push_str(", ");
|
||
greeting.push_str(name);
|
||
}
|
||
|
||
greeting.push('!');
|
||
greeting
|
||
}
|
||
|
||
#[launch]
|
||
fn rocket() -> _ {
|
||
rocket::build()
|
||
.mount("/", routes![hello])
|
||
.mount("/hello", routes![world, mir])
|
||
.mount("/wave", routes![wave])
|
||
}
|