2016-04-04 11:14:18 +00:00
|
|
|
#![feature(plugin, custom_derive)]
|
2016-09-09 03:38:58 +00:00
|
|
|
#![plugin(rocket_codegen)]
|
2016-03-28 09:34:09 +00:00
|
|
|
|
|
|
|
extern crate rocket;
|
|
|
|
|
2016-03-30 08:02:21 +00:00
|
|
|
mod files;
|
2016-10-17 22:14:57 +00:00
|
|
|
#[cfg(test)] mod tests;
|
2016-03-30 08:02:21 +00:00
|
|
|
|
2016-10-12 07:14:42 +00:00
|
|
|
use rocket::request::Form;
|
2016-03-28 09:34:09 +00:00
|
|
|
use rocket::response::Redirect;
|
|
|
|
|
2016-04-04 11:14:18 +00:00
|
|
|
#[derive(FromForm)]
|
|
|
|
struct UserLogin<'r> {
|
|
|
|
username: &'r str,
|
|
|
|
password: &'r str,
|
2016-10-12 07:14:42 +00:00
|
|
|
age: Result<usize, &'r str>,
|
2016-03-28 09:34:09 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 07:14:42 +00:00
|
|
|
#[post("/login", data = "<user_form>")]
|
|
|
|
fn login<'a>(user_form: Form<'a, UserLogin<'a>>) -> Result<Redirect, String> {
|
|
|
|
let user = user_form.get();
|
|
|
|
match user.age {
|
|
|
|
Ok(age) if age < 21 => return Err(format!("Sorry, {} is too young!", age)),
|
|
|
|
Ok(age) if age > 120 => return Err(format!("Are you sure you're {}?", age)),
|
|
|
|
Err(e) => return Err(format!("'{}' is not a valid integer.", e)),
|
|
|
|
Ok(_) => { /* Move along, adult. */ }
|
|
|
|
};
|
|
|
|
|
|
|
|
if user.username == "Sergio" {
|
|
|
|
match user.password {
|
2016-11-03 18:09:08 +00:00
|
|
|
"password" => Ok(Redirect::to("/user/Sergio")),
|
2016-03-30 08:02:21 +00:00
|
|
|
_ => Err("Wrong password!".to_string())
|
2016-10-12 07:14:42 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(format!("Unrecognized user, '{}'.", user.username))
|
2016-03-28 09:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-12 07:14:42 +00:00
|
|
|
|
|
|
|
#[get("/user/<username>")]
|
2016-04-05 02:02:51 +00:00
|
|
|
fn user_page(username: &str) -> String {
|
|
|
|
format!("This is {}'s page.", username)
|
|
|
|
}
|
|
|
|
|
2016-03-28 09:34:09 +00:00
|
|
|
fn main() {
|
2016-10-04 02:48:33 +00:00
|
|
|
rocket::ignite()
|
|
|
|
.mount("/", routes![files::index, files::files, user_page, login])
|
|
|
|
.launch();
|
2016-03-28 09:34:09 +00:00
|
|
|
}
|