2016-08-07 06:14:05 +00:00
|
|
|
#![feature(plugin, custom_derive, custom_attribute)]
|
2016-09-09 03:38:58 +00:00
|
|
|
#![plugin(rocket_codegen)]
|
2016-08-07 06:14:05 +00:00
|
|
|
|
2016-10-09 04:02:42 +00:00
|
|
|
extern crate rocket_contrib;
|
2016-08-07 06:14:05 +00:00
|
|
|
extern crate rocket;
|
2016-10-09 04:02:42 +00:00
|
|
|
|
2016-12-29 20:06:31 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2016-10-09 04:02:42 +00:00
|
|
|
use std::collections::HashMap;
|
2016-08-07 06:14:05 +00:00
|
|
|
|
2016-10-12 07:14:42 +00:00
|
|
|
use rocket::request::Form;
|
2016-09-12 01:57:04 +00:00
|
|
|
use rocket::response::Redirect;
|
2016-10-04 00:09:13 +00:00
|
|
|
use rocket::http::{Cookie, Cookies};
|
2016-10-09 04:02:42 +00:00
|
|
|
use rocket_contrib::Template;
|
2016-08-07 06:14:05 +00:00
|
|
|
|
|
|
|
#[derive(FromForm)]
|
|
|
|
struct Message {
|
2016-12-29 20:06:31 +00:00
|
|
|
message: String,
|
2016-08-07 06:14:05 +00:00
|
|
|
}
|
|
|
|
|
2016-10-12 07:14:42 +00:00
|
|
|
#[post("/submit", data = "<message>")]
|
2017-03-07 09:19:06 +00:00
|
|
|
fn submit(mut cookies: Cookies, message: Form<Message>) -> Redirect {
|
2017-01-27 07:08:15 +00:00
|
|
|
cookies.add(Cookie::new("message", message.into_inner().message));
|
2016-09-12 01:57:04 +00:00
|
|
|
Redirect::to("/")
|
2016-08-07 06:14:05 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 11:06:28 +00:00
|
|
|
#[get("/")]
|
2017-03-07 09:19:06 +00:00
|
|
|
fn index(cookies: Cookies) -> Template {
|
|
|
|
let cookie = cookies.get("message");
|
2016-10-09 04:02:42 +00:00
|
|
|
let mut context = HashMap::new();
|
2017-01-27 07:08:15 +00:00
|
|
|
if let Some(ref cookie) = cookie {
|
|
|
|
context.insert("message", cookie.value());
|
2016-10-09 04:02:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Template::render("index", &context)
|
2016-08-07 06:14:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2016-10-04 02:48:33 +00:00
|
|
|
rocket::ignite().mount("/", routes![submit, index]).launch()
|
2016-08-07 06:14:05 +00:00
|
|
|
}
|