2018-06-28 15:55:15 +00:00
|
|
|
#[macro_use] extern crate rocket;
|
2016-04-06 10:26:43 +00:00
|
|
|
|
2017-01-31 10:43:19 +00:00
|
|
|
#[cfg(test)] mod tests;
|
|
|
|
|
2020-07-30 06:07:22 +00:00
|
|
|
use rocket::Request;
|
|
|
|
use rocket::response::{content, status};
|
|
|
|
use rocket::http::Status;
|
2017-01-02 06:55:08 +00:00
|
|
|
|
2016-09-04 08:05:17 +00:00
|
|
|
#[get("/hello/<name>/<age>")]
|
2017-03-31 07:18:58 +00:00
|
|
|
fn hello(name: String, age: i8) -> String {
|
2016-04-06 10:26:43 +00:00
|
|
|
format!("Hello, {} year old named {}!", age, name)
|
|
|
|
}
|
|
|
|
|
2020-07-30 06:07:22 +00:00
|
|
|
#[get("/<code>")]
|
|
|
|
fn forced_error(code: u16) -> Status {
|
|
|
|
Status::raw(code)
|
|
|
|
}
|
|
|
|
|
2017-09-23 02:04:14 +00:00
|
|
|
#[catch(404)]
|
2020-07-30 06:07:22 +00:00
|
|
|
fn not_found(req: &Request<'_>) -> content::Html<String> {
|
2017-07-12 22:21:45 +00:00
|
|
|
content::Html(format!("<p>Sorry, but '{}' is not a valid path!</p>
|
2016-07-16 04:09:08 +00:00
|
|
|
<p>Try visiting /hello/<name>/<age> instead.</p>",
|
2017-01-31 10:43:19 +00:00
|
|
|
req.uri()))
|
2016-04-06 10:26:43 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 06:07:22 +00:00
|
|
|
#[catch(default)]
|
|
|
|
fn default_catcher(status: Status, req: &Request<'_>) -> status::Custom<String> {
|
|
|
|
let msg = format!("{} - {} ({})", status.code, status.reason, req.uri());
|
|
|
|
status::Custom(status, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rocket() -> rocket::Rocket {
|
|
|
|
rocket::ignite()
|
2017-04-19 00:42:44 +00:00
|
|
|
// .mount("/", routes![hello, hello]) // uncoment this to get an error
|
2020-07-30 06:07:22 +00:00
|
|
|
.mount("/", routes![hello, forced_error])
|
|
|
|
.register(catchers![not_found, default_catcher])
|
|
|
|
}
|
2017-03-16 05:10:09 +00:00
|
|
|
|
2020-07-30 06:07:22 +00:00
|
|
|
#[rocket::main]
|
|
|
|
async fn main() {
|
|
|
|
if let Err(e) = rocket().launch().await {
|
2019-09-08 20:53:53 +00:00
|
|
|
println!("Whoops! Rocket didn't launch!");
|
2020-07-30 06:07:22 +00:00
|
|
|
println!("Error: {:?}", e);
|
2019-09-08 20:53:53 +00:00
|
|
|
};
|
2016-04-06 10:26:43 +00:00
|
|
|
}
|