2019-08-20 23:53:00 +00:00
|
|
|
#![feature(proc_macro_hygiene)]
|
2016-09-05 02:18:08 +00:00
|
|
|
|
2018-08-07 02:58:07 +00:00
|
|
|
#[macro_use] extern crate rocket;
|
2016-09-05 02:18:08 +00:00
|
|
|
|
2016-12-29 03:29:12 +00:00
|
|
|
#[cfg(test)] mod tests;
|
|
|
|
|
2018-09-20 04:14:30 +00:00
|
|
|
use rocket::request::{Form, LenientForm};
|
|
|
|
|
2016-09-05 02:18:08 +00:00
|
|
|
#[derive(FromForm)]
|
2017-03-31 06:06:53 +00:00
|
|
|
struct Person {
|
2020-02-11 21:23:28 +00:00
|
|
|
/// Use the `form` attribute to expect an invalid Rust identifier in the HTTP form.
|
|
|
|
#[form(field = "first-name")]
|
2017-03-31 06:06:53 +00:00
|
|
|
name: String,
|
2016-09-05 02:18:08 +00:00
|
|
|
age: Option<u8>
|
|
|
|
}
|
|
|
|
|
2018-09-20 04:14:30 +00:00
|
|
|
#[get("/hello?<person..>")]
|
|
|
|
fn hello(person: Option<Form<Person>>) -> String {
|
|
|
|
if let Some(person) = person {
|
|
|
|
if let Some(age) = person.age {
|
|
|
|
format!("Hello, {} year old named {}!", age, person.name)
|
|
|
|
} else {
|
|
|
|
format!("Hello {}!", person.name)
|
|
|
|
}
|
2016-09-05 02:18:08 +00:00
|
|
|
} else {
|
2018-09-20 04:14:30 +00:00
|
|
|
"We're gonna need a name, and only a name.".into()
|
2016-09-05 02:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 04:14:30 +00:00
|
|
|
#[get("/hello?age=20&<person..>")]
|
|
|
|
fn hello_20(person: LenientForm<Person>) -> String {
|
|
|
|
format!("20 years old? Hi, {}!", person.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rocket() -> rocket::Rocket {
|
|
|
|
rocket::ignite().mount("/", routes![hello, hello_20])
|
|
|
|
}
|
|
|
|
|
2016-09-05 02:18:08 +00:00
|
|
|
fn main() {
|
2019-08-25 02:19:11 +00:00
|
|
|
let _ = rocket().launch();
|
2016-09-05 02:18:08 +00:00
|
|
|
}
|