2018-09-20 04:14:30 +00:00
|
|
|
#![feature(proc_macro_non_items, proc_macro_gen, decl_macro)]
|
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 {
|
|
|
|
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() {
|
2018-09-20 04:14:30 +00:00
|
|
|
rocket().launch();
|
2016-09-05 02:18:08 +00:00
|
|
|
}
|