Document config
This commit is contained in:
parent
a0c2edba56
commit
7992857e5f
3 changed files with 22 additions and 11 deletions
|
@ -1,9 +1,8 @@
|
|||
use anyhow::{Context, Result};
|
||||
use serde::Deserialize;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::{env, fs::File, io::BufReader};
|
||||
|
||||
/// Email server credentials.
|
||||
#[derive(Deserialize)]
|
||||
pub struct EmailServer {
|
||||
pub server_name: String,
|
||||
|
@ -11,30 +10,35 @@ pub struct EmailServer {
|
|||
pub password: String,
|
||||
}
|
||||
|
||||
/// UTC offset for time formatting.
|
||||
#[derive(Deserialize)]
|
||||
pub struct UtcOffset {
|
||||
pub hours: i8,
|
||||
pub minutes: i8,
|
||||
}
|
||||
|
||||
/// Error messages for localization.
|
||||
#[derive(Deserialize)]
|
||||
pub struct ErrorMessages {
|
||||
pub captcha_error: String,
|
||||
pub email_error: String,
|
||||
}
|
||||
|
||||
/// Field localization strings in a form with label and invalid feedback on wrong field input.
|
||||
#[derive(Deserialize)]
|
||||
pub struct Field {
|
||||
pub label: String,
|
||||
pub invalid_feedback: String,
|
||||
}
|
||||
|
||||
/// Localization strings.
|
||||
#[derive(Deserialize)]
|
||||
pub struct Strings {
|
||||
pub description: String,
|
||||
pub title: String,
|
||||
pub name_field: Field,
|
||||
pub email_field: Field,
|
||||
/// No invalid feedback because it is optional.
|
||||
pub telefon_field_label: String,
|
||||
pub message_field: Field,
|
||||
pub captcha_field: Field,
|
||||
|
@ -42,13 +46,19 @@ pub struct Strings {
|
|||
pub success: String,
|
||||
}
|
||||
|
||||
/// Configuration.
|
||||
#[derive(Deserialize)]
|
||||
pub struct Config {
|
||||
/// The language tag of the HTML file.
|
||||
pub lang: String,
|
||||
/// The path prefix of all routes.
|
||||
pub path_prefix: String,
|
||||
/// The server socket address including port.
|
||||
pub socket_address: String,
|
||||
pub email_server: EmailServer,
|
||||
/// From mailbox.
|
||||
pub email_from: String,
|
||||
/// To mailbox.
|
||||
pub email_to: String,
|
||||
pub log_file: String,
|
||||
pub utc_offset: UtcOffset,
|
||||
|
@ -57,16 +67,17 @@ pub struct Config {
|
|||
}
|
||||
|
||||
impl Config {
|
||||
pub fn new() -> Result<Self> {
|
||||
pub fn build() -> Result<Self> {
|
||||
// The environment variable with the path to the config file.
|
||||
let config_file_var = "CF_CONFIG_FILE";
|
||||
let config_path = env::var(config_file_var)
|
||||
.with_context(|| format!("Environment variable {config_file_var} missing!"))?;
|
||||
|
||||
let config_file = File::open(&config_path)
|
||||
let file = File::open(&config_path)
|
||||
.with_context(|| format!("Can not open the config file at the path {config_path}"))?;
|
||||
let config_reader = BufReader::new(config_file);
|
||||
let config: Self = serde_yaml::from_reader(config_reader)
|
||||
.context("Can not parse the YAML config file!")?;
|
||||
let reader = BufReader::new(file);
|
||||
let config =
|
||||
serde_yaml::from_reader(reader).context("Can not parse the YAML config file!")?;
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ pub struct Mailer {
|
|||
}
|
||||
|
||||
impl Mailer {
|
||||
pub fn new(config: &Config) -> Result<Self> {
|
||||
pub fn build(config: &Config) -> Result<Self> {
|
||||
let creds = Credentials::new(
|
||||
config.email_server.email.clone(),
|
||||
config.email_server.password.clone(),
|
||||
|
|
|
@ -13,8 +13,8 @@ pub struct AppState {
|
|||
|
||||
impl AppState {
|
||||
pub fn build() -> Result<Self> {
|
||||
let config = Config::new()?;
|
||||
let mailer = Arc::new(Mailer::new(&config)?);
|
||||
let config = Config::build()?;
|
||||
let mailer = Arc::new(Mailer::build(&config)?);
|
||||
|
||||
let config = Arc::new(config);
|
||||
let captcha_solutions = Arc::new(Mutex::new(CaptchaSolutions::default()));
|
||||
|
|
Loading…
Reference in a new issue