From cd90c982f678047107924f817e1ae5dac67f9841 Mon Sep 17 00:00:00 2001 From: Mo8it Date: Sat, 29 Oct 2022 01:07:43 +0200 Subject: [PATCH] Use path_prefix in template --- src/mailer.rs | 11 ++++++----- src/main.rs | 4 ++-- src/routes.rs | 42 ++++++++++++++++++++++++++-------------- templates/form.html.tera | 2 +- 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/mailer.rs b/src/mailer.rs index b531c86..3772f9b 100644 --- a/src/mailer.rs +++ b/src/mailer.rs @@ -4,6 +4,7 @@ use lettre::message::{Mailbox, MessageBuilder}; use lettre::transport::smtp::authentication::Credentials; use lettre::Transport; use lettre::{Message, SmtpTransport}; +use std::mem; use crate::config; @@ -13,10 +14,10 @@ pub struct Mailer { } impl Mailer { - pub fn new(config: &config::Config) -> Result { + pub fn new(config: &mut config::Config) -> Result { let creds = Credentials::new( - config.email_server.email.clone(), - config.email_server.password.clone(), + mem::take(&mut config.email_server.email), + mem::take(&mut config.email_server.password), ); let mailer = SmtpTransport::relay(&config.email_server.server_name) @@ -26,12 +27,12 @@ impl Mailer { let message_builder = Message::builder() .from(Mailbox::new( - Some(config.email_from.name.clone()), + Some(mem::take(&mut config.email_from.name)), Address::new(&config.email_from.user, &config.email_from.domain) .context("Failed to create the From email address!")?, )) .to(Mailbox::new( - Some(config.email_to.name.clone()), + Some(mem::take(&mut config.email_to.name)), Address::new(&config.email_to.user, &config.email_to.domain) .context("Failed to create the To email address!")?, )); diff --git a/src/main.rs b/src/main.rs index f59c6a1..0d09e66 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ use rocket_dyn_templates::Template; #[rocket::launch] fn rocket() -> _ { - let config = config::Config::new(); + let mut config = config::Config::new(); rocket::build() .mount( @@ -16,7 +16,7 @@ fn rocket() -> _ { rocket::routes![routes::index, routes::submit, routes::success], ) .manage(captcha_solutions::SharedCaptchaSolutions::new()) - .manage(mailer::Mailer::new(&config).expect("Failed to create mailer!")) + .manage(mailer::Mailer::new(&mut config).expect("Failed to create mailer!")) .manage(config) .attach(Template::fairing()) } diff --git a/src/routes.rs b/src/routes.rs index ab12dbb..24b15f9 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -12,21 +12,23 @@ use crate::forms; use crate::mailer; #[derive(Serialize)] -struct FormContext { +struct FormContext<'a> { + path_prefix: &'a str, id: u16, - name: Option, - email: Option, - telefon: Option, - message: Option, - captcha: String, + name: Option<&'a str>, + email: Option<&'a str>, + telefon: Option<&'a str>, + message: Option<&'a str>, + captcha: &'a str, } #[get("/?&&&")] pub fn index( - name: Option, - email: Option, - telefon: Option, - message: Option, + name: Option<&str>, + email: Option<&str>, + telefon: Option<&str>, + message: Option<&str>, + config: &State, captcha_solutions: &State, ) -> Result> { let captcha = captcha::by_name(captcha::Difficulty::Easy, captcha::CaptchaName::Lucy); @@ -38,12 +40,13 @@ pub fn index( let id = captcha_solutions.store_solution(&captcha.chars_as_string()); let form_context = FormContext { + path_prefix: &config.path_prefix, id, name, email, telefon, message, - captcha: captcha_base64, + captcha: &captcha_base64, }; Ok(Template::render("form", &form_context)) @@ -66,7 +69,13 @@ pub fn submit( if !captcha_solutions.check_answer(form.id, &form.captcha_answer) { return Redirect::to( path_prefix - + &uri!(index(Some(name), Some(email), Some(telefon), Some(message))).to_string(), + + &uri!(index( + Some(&name), + Some(&email), + Some(&telefon), + Some(&message) + )) + .to_string(), ); } @@ -75,8 +84,13 @@ pub fn submit( Err(_) => { return Redirect::to( path_prefix - + &uri!(index(Some(name), Some(email), Some(telefon), Some(message))) - .to_string(), + + &uri!(index( + Some(&name), + Some(&email), + Some(&telefon), + Some(&message) + )) + .to_string(), ) } } diff --git a/templates/form.html.tera b/templates/form.html.tera index cbdadd9..67d0d10 100644 --- a/templates/form.html.tera +++ b/templates/form.html.tera @@ -13,7 +13,7 @@

Kontakt-Formular

-
+