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;
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<Self> {
pub fn new(config: &mut config::Config) -> Result<Self> {
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!")?,
));

View file

@ -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())
}

View file

@ -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<String>,
email: Option<String>,
telefon: Option<String>,
message: Option<String>,
captcha: String,
name: Option<&'a str>,
email: Option<&'a str>,
telefon: Option<&'a str>,
message: Option<&'a str>,
captcha: &'a str,
}
#[get("/?<name>&<email>&<telefon>&<message>")]
pub fn index(
name: Option<String>,
email: Option<String>,
telefon: Option<String>,
message: Option<String>,
name: Option<&str>,
email: Option<&str>,
telefon: Option<&str>,
message: Option<&str>,
config: &State<config::Config>,
captcha_solutions: &State<captcha_solutions::SharedCaptchaSolutions>,
) -> Result<Template, BadRequest<()>> {
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,7 +84,12 @@ pub fn submit(
Err(_) => {
return Redirect::to(
path_prefix
+ &uri!(index(Some(name), Some(email), Some(telefon), Some(message)))
+ &uri!(index(
Some(&name),
Some(&email),
Some(&telefon),
Some(&message)
))
.to_string(),
)
}

View file

@ -13,7 +13,7 @@
<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>
<div class="mb-3">