2016-03-28 09:34:09 +00:00
|
|
|
#![feature(plugin)]
|
|
|
|
#![plugin(rocket_macros)]
|
|
|
|
|
|
|
|
extern crate rocket;
|
|
|
|
|
2016-03-30 08:02:21 +00:00
|
|
|
mod files;
|
|
|
|
|
2016-03-28 09:34:09 +00:00
|
|
|
use rocket::Rocket;
|
|
|
|
use rocket::response::Redirect;
|
2016-03-30 08:02:21 +00:00
|
|
|
use rocket::error::Error;
|
2016-03-28 09:34:09 +00:00
|
|
|
|
2016-03-30 08:02:21 +00:00
|
|
|
#[route(GET, path = "/user/<username>")]
|
|
|
|
fn user_page(username: &str) -> String {
|
|
|
|
format!("This is {}'s page.", username)
|
2016-03-28 09:34:09 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 08:02:21 +00:00
|
|
|
// #[derive(FormItem)] // FIXME: Make that happen.
|
|
|
|
struct UserLogin<'a> {
|
|
|
|
username: &'a str,
|
|
|
|
password: &'a str
|
2016-03-28 09:34:09 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 08:02:21 +00:00
|
|
|
trait FormItem: Sized {
|
|
|
|
fn from_form_string(s: &str) -> Result<Self, Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FormItem for UserLogin<'a> {
|
|
|
|
fn from_form_string(s: &str) -> Result<Self, Error> {
|
|
|
|
Ok(UserLogin {
|
|
|
|
username: "this",
|
|
|
|
password: "that"
|
|
|
|
})
|
|
|
|
}
|
2016-03-28 09:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Actually look at form parameters.
|
2016-03-30 08:02:21 +00:00
|
|
|
// FIXME: fn login<'a>(user: UserLogin<'a>)
|
|
|
|
#[route(POST, path = "/login", form = "<user>")]
|
|
|
|
fn login(user: UserLogin) -> Result<Redirect, String> {
|
|
|
|
match user.username {
|
|
|
|
"Sergio" => match user.password {
|
|
|
|
"password" => Ok(Redirect::other("/user/some_name")),
|
|
|
|
_ => Err("Wrong password!".to_string())
|
|
|
|
},
|
|
|
|
_ => Err(format!("Unrecognized user, '{}'.", user.username))
|
2016-03-28 09:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2016-03-30 08:02:21 +00:00
|
|
|
let mut rocket = Rocket::new("localhost", 8000);
|
|
|
|
rocket.mount("/", routes![files::index, files::files, user_page, login]);
|
|
|
|
rocket.launch();
|
2016-03-28 09:34:09 +00:00
|
|
|
}
|