Document routers
This commit is contained in:
parent
24b8594325
commit
90bce828ff
1 changed files with 41 additions and 35 deletions
|
@ -24,6 +24,35 @@ pub struct ContactFormParams<'a> {
|
||||||
error_message: Option<&'a str>,
|
error_message: Option<&'a str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Renders the contact form.
|
||||||
|
pub fn render_contact_form(params: ContactFormParams<'_>) -> Result<Response, AppError> {
|
||||||
|
// Generate a captcha.
|
||||||
|
let captcha = captcha::by_name(captcha::Difficulty::Easy, captcha::CaptchaName::Lucy);
|
||||||
|
let captcha_base64 = captcha.as_base64().context("Failed to create a captcha!")?;
|
||||||
|
// Get captcha solution.
|
||||||
|
let solution = captcha.chars_as_string();
|
||||||
|
// Store the captcha solution and get the captcha id.
|
||||||
|
let id = params.captcha_solutions.lock().unwrap().push(solution);
|
||||||
|
|
||||||
|
// Initialize the template.
|
||||||
|
let template = templates::ContactForm {
|
||||||
|
base: templates::Base {
|
||||||
|
lang: ¶ms.config.lang,
|
||||||
|
path_prefix: ¶ms.config.path_prefix,
|
||||||
|
},
|
||||||
|
was_validated: params.persistant_fields.is_some(),
|
||||||
|
id,
|
||||||
|
// Default is empty fields.
|
||||||
|
persistant_fields: params.persistant_fields.unwrap_or_default(),
|
||||||
|
captcha: captcha_base64,
|
||||||
|
error_message: params.error_message.unwrap_or_default(),
|
||||||
|
strings: ¶ms.config.strings,
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(template.into_response())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Index handler.
|
||||||
pub async fn index(
|
pub async fn index(
|
||||||
State(config): State<Arc<Config>>,
|
State(config): State<Arc<Config>>,
|
||||||
State(captcha_solutions): State<Arc<Mutex<CaptchaSolutions>>>,
|
State(captcha_solutions): State<Arc<Mutex<CaptchaSolutions>>>,
|
||||||
|
@ -38,30 +67,7 @@ pub async fn index(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render_contact_form(params: ContactFormParams<'_>) -> Result<Response, AppError> {
|
/// Submit handler.
|
||||||
let captcha = captcha::by_name(captcha::Difficulty::Easy, captcha::CaptchaName::Lucy);
|
|
||||||
let captcha_base64 = captcha.as_base64().context("Failed to create a captcha!")?;
|
|
||||||
|
|
||||||
let solution = captcha.chars_as_string();
|
|
||||||
|
|
||||||
let id = params.captcha_solutions.lock().unwrap().push(solution);
|
|
||||||
|
|
||||||
let template = templates::ContactForm {
|
|
||||||
base: templates::Base {
|
|
||||||
lang: ¶ms.config.lang,
|
|
||||||
path_prefix: ¶ms.config.path_prefix,
|
|
||||||
},
|
|
||||||
was_validated: params.persistant_fields.is_some(),
|
|
||||||
id,
|
|
||||||
persistant_fields: params.persistant_fields.unwrap_or_default(),
|
|
||||||
captcha: captcha_base64,
|
|
||||||
error_message: params.error_message.unwrap_or_default(),
|
|
||||||
strings: ¶ms.config.strings,
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(template.into_response())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn submit(
|
pub async fn submit(
|
||||||
State(config): State<Arc<Config>>,
|
State(config): State<Arc<Config>>,
|
||||||
State(captcha_solutions): State<Arc<Mutex<CaptchaSolutions>>>,
|
State(captcha_solutions): State<Arc<Mutex<CaptchaSolutions>>>,
|
||||||
|
@ -84,7 +90,7 @@ pub async fn submit(
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
match mailer
|
if let Err(e) = mailer
|
||||||
.send(
|
.send(
|
||||||
&form.persistant_fields.name,
|
&form.persistant_fields.name,
|
||||||
&form.persistant_fields.email,
|
&form.persistant_fields.email,
|
||||||
|
@ -93,8 +99,6 @@ pub async fn submit(
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(_) => (),
|
|
||||||
Err(e) => {
|
|
||||||
error!("{e:?}");
|
error!("{e:?}");
|
||||||
|
|
||||||
return render_contact_form(ContactFormParams {
|
return render_contact_form(ContactFormParams {
|
||||||
|
@ -104,14 +108,15 @@ pub async fn submit(
|
||||||
error_message: Some(&config.error_messages.email_error),
|
error_message: Some(&config.error_messages.email_error),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
success(config)
|
success(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Called on successful contact form submission.
|
||||||
pub fn success(config: Arc<Config>) -> Result<Response, AppError> {
|
pub fn success(config: Arc<Config>) -> Result<Response, AppError> {
|
||||||
info!("Successful contact form submission");
|
info!("Successful contact form submission");
|
||||||
|
|
||||||
|
// Initialize template.
|
||||||
let template = templates::Success {
|
let template = templates::Success {
|
||||||
base: templates::Base {
|
base: templates::Base {
|
||||||
lang: &config.lang,
|
lang: &config.lang,
|
||||||
|
@ -120,5 +125,6 @@ pub fn success(config: Arc<Config>) -> Result<Response, AppError> {
|
||||||
message: &config.strings.success,
|
message: &config.strings.success,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Render template.
|
||||||
Ok(template.into_response())
|
Ok(template.into_response())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue