use rocket::form::{Form, FromForm, Strict}; use rocket::response::status::BadRequest; use rocket::{get, post, State}; use rocket_dyn_templates::Template; use serde::Serialize; use crate::states; #[derive(Serialize)] struct FormContext { id: u16, captcha: String, } #[get("/")] pub fn index( captcha_solutions: &State, ) -> Result> { let captcha = captcha::by_name(captcha::Difficulty::Easy, captcha::CaptchaName::Lucy); let captcha_base64 = match captcha.as_base64() { Some(s) => s, None => return Err(BadRequest(None)), }; let id = captcha_solutions.store_solution(&captcha.chars_as_string()); let form_context = FormContext { id, captcha: captcha_base64, }; Ok(Template::render("form", &form_context)) } #[derive(FromForm)] pub struct ContactForm { id: u16, name: String, email: String, telefon: String, message: String, captcha_answer: String, } #[post("/submit", data = "
")] pub fn submit( form: Form>, captcha_solutions: &State, ) -> Result> { println!( "{}", captcha_solutions.check_answer(form.id, &form.captcha_answer) ); Ok("Ihre Anfrage wurde übermittelt. Wir melden uns bald bei Ihnen zurück.".to_string()) }