Use path_prefix in template

This commit is contained in:
Mo 2022-10-29 01:07:43 +02:00
parent 640517b48c
commit cd90c982f6
4 changed files with 37 additions and 22 deletions

View file

@ -4,6 +4,7 @@ use lettre::message::{Mailbox, MessageBuilder};
use lettre::transport::smtp::authentication::Credentials; use lettre::transport::smtp::authentication::Credentials;
use lettre::Transport; use lettre::Transport;
use lettre::{Message, SmtpTransport}; use lettre::{Message, SmtpTransport};
use std::mem;
use crate::config; use crate::config;
@ -13,10 +14,10 @@ pub struct Mailer {
} }
impl Mailer { impl Mailer {
pub fn new(config: &config::Config) -> Result<Self> { pub fn new(config: &mut config::Config) -> Result<Self> {
let creds = Credentials::new( let creds = Credentials::new(
config.email_server.email.clone(), mem::take(&mut config.email_server.email),
config.email_server.password.clone(), mem::take(&mut config.email_server.password),
); );
let mailer = SmtpTransport::relay(&config.email_server.server_name) let mailer = SmtpTransport::relay(&config.email_server.server_name)
@ -26,12 +27,12 @@ impl Mailer {
let message_builder = Message::builder() let message_builder = Message::builder()
.from(Mailbox::new( .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) Address::new(&config.email_from.user, &config.email_from.domain)
.context("Failed to create the From email address!")?, .context("Failed to create the From email address!")?,
)) ))
.to(Mailbox::new( .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) Address::new(&config.email_to.user, &config.email_to.domain)
.context("Failed to create the To email address!")?, .context("Failed to create the To email address!")?,
)); ));

View file

@ -8,7 +8,7 @@ use rocket_dyn_templates::Template;
#[rocket::launch] #[rocket::launch]
fn rocket() -> _ { fn rocket() -> _ {
let config = config::Config::new(); let mut config = config::Config::new();
rocket::build() rocket::build()
.mount( .mount(
@ -16,7 +16,7 @@ fn rocket() -> _ {
rocket::routes![routes::index, routes::submit, routes::success], rocket::routes![routes::index, routes::submit, routes::success],
) )
.manage(captcha_solutions::SharedCaptchaSolutions::new()) .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) .manage(config)
.attach(Template::fairing()) .attach(Template::fairing())
} }

View file

@ -12,21 +12,23 @@ use crate::forms;
use crate::mailer; use crate::mailer;
#[derive(Serialize)] #[derive(Serialize)]
struct FormContext { struct FormContext<'a> {
path_prefix: &'a str,
id: u16, id: u16,
name: Option<String>, name: Option<&'a str>,
email: Option<String>, email: Option<&'a str>,
telefon: Option<String>, telefon: Option<&'a str>,
message: Option<String>, message: Option<&'a str>,
captcha: String, captcha: &'a str,
} }
#[get("/?<name>&<email>&<telefon>&<message>")] #[get("/?<name>&<email>&<telefon>&<message>")]
pub fn index( pub fn index(
name: Option<String>, name: Option<&str>,
email: Option<String>, email: Option<&str>,
telefon: Option<String>, telefon: Option<&str>,
message: Option<String>, message: Option<&str>,
config: &State<config::Config>,
captcha_solutions: &State<captcha_solutions::SharedCaptchaSolutions>, captcha_solutions: &State<captcha_solutions::SharedCaptchaSolutions>,
) -> Result<Template, BadRequest<()>> { ) -> Result<Template, BadRequest<()>> {
let captcha = captcha::by_name(captcha::Difficulty::Easy, captcha::CaptchaName::Lucy); 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 id = captcha_solutions.store_solution(&captcha.chars_as_string());
let form_context = FormContext { let form_context = FormContext {
path_prefix: &config.path_prefix,
id, id,
name, name,
email, email,
telefon, telefon,
message, message,
captcha: captcha_base64, captcha: &captcha_base64,
}; };
Ok(Template::render("form", &form_context)) Ok(Template::render("form", &form_context))
@ -66,7 +69,13 @@ pub fn submit(
if !captcha_solutions.check_answer(form.id, &form.captcha_answer) { if !captcha_solutions.check_answer(form.id, &form.captcha_answer) {
return Redirect::to( return Redirect::to(
path_prefix 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(_) => { Err(_) => {
return Redirect::to( return Redirect::to(
path_prefix path_prefix
+ &uri!(index(Some(name), Some(email), Some(telefon), Some(message))) + &uri!(index(
.to_string(), Some(&name),
Some(&email),
Some(&telefon),
Some(&message)
))
.to_string(),
) )
} }
} }

View file

@ -13,7 +13,7 @@
<h2>Kontakt-Formular</h2> <h2>Kontakt-Formular</h2>
<form action="/contact-form/submit" method="post"> <form action="{{ path_prefix }}/submit" method="post">
<input type="hidden" name="id" value="{{ id }}" required> <input type="hidden" name="id" value="{{ id }}" required>
<div class="mb-3"> <div class="mb-3">