2022-10-29 17:09:56 +00:00
|
|
|
use anyhow::{Context, Result};
|
2022-10-28 22:41:02 +00:00
|
|
|
use serde::Deserialize;
|
2023-02-23 16:10:24 +00:00
|
|
|
use std::{env, fs::File, io::BufReader};
|
2022-10-28 22:41:02 +00:00
|
|
|
|
2023-02-23 16:10:24 +00:00
|
|
|
/// Email server credentials.
|
2022-10-28 22:41:02 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct EmailServer {
|
|
|
|
pub server_name: String,
|
|
|
|
pub email: String,
|
|
|
|
pub password: String,
|
|
|
|
}
|
|
|
|
|
2023-02-23 16:10:24 +00:00
|
|
|
/// UTC offset for time formatting.
|
2022-10-28 22:41:02 +00:00
|
|
|
#[derive(Deserialize)]
|
2023-02-23 02:22:31 +00:00
|
|
|
pub struct UtcOffset {
|
|
|
|
pub hours: i8,
|
|
|
|
pub minutes: i8,
|
2022-12-03 16:08:23 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 16:10:24 +00:00
|
|
|
/// Error messages for localization.
|
2022-12-03 16:08:23 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct ErrorMessages {
|
|
|
|
pub captcha_error: String,
|
|
|
|
pub email_error: String,
|
|
|
|
}
|
|
|
|
|
2023-02-23 16:10:24 +00:00
|
|
|
/// Field localization strings in a form with label and invalid feedback on wrong field input.
|
2022-12-03 16:08:23 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct Field {
|
|
|
|
pub label: String,
|
|
|
|
pub invalid_feedback: String,
|
|
|
|
}
|
|
|
|
|
2023-02-23 16:10:24 +00:00
|
|
|
/// Localization strings.
|
2022-12-03 16:08:23 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct Strings {
|
|
|
|
pub description: String,
|
|
|
|
pub title: String,
|
|
|
|
pub name_field: Field,
|
|
|
|
pub email_field: Field,
|
2023-02-23 16:10:24 +00:00
|
|
|
/// No invalid feedback because it is optional.
|
2022-12-03 16:08:23 +00:00
|
|
|
pub telefon_field_label: String,
|
|
|
|
pub message_field: Field,
|
|
|
|
pub captcha_field: Field,
|
|
|
|
pub submit: String,
|
|
|
|
pub success: String,
|
2022-11-01 23:24:17 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 16:10:24 +00:00
|
|
|
/// Configuration.
|
2022-10-28 22:41:02 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct Config {
|
2023-02-23 16:10:24 +00:00
|
|
|
/// The language tag of the HTML file.
|
2022-12-17 17:37:04 +00:00
|
|
|
pub lang: String,
|
2023-02-23 16:10:24 +00:00
|
|
|
/// The path prefix of all routes.
|
2022-10-28 22:41:02 +00:00
|
|
|
pub path_prefix: String,
|
2023-02-23 16:10:24 +00:00
|
|
|
/// The server socket address including port.
|
2023-02-23 02:22:31 +00:00
|
|
|
pub socket_address: String,
|
2022-10-28 22:41:02 +00:00
|
|
|
pub email_server: EmailServer,
|
2023-02-23 16:10:24 +00:00
|
|
|
/// From mailbox.
|
2023-02-23 02:22:31 +00:00
|
|
|
pub email_from: String,
|
2023-02-23 16:10:24 +00:00
|
|
|
/// To mailbox.
|
2023-02-23 02:22:31 +00:00
|
|
|
pub email_to: String,
|
|
|
|
pub log_file: String,
|
|
|
|
pub utc_offset: UtcOffset,
|
2022-12-03 16:08:23 +00:00
|
|
|
pub error_messages: ErrorMessages,
|
|
|
|
pub strings: Strings,
|
2022-10-28 22:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
2023-02-23 16:17:25 +00:00
|
|
|
/// Parses the configuration from the config path in the environment variable.
|
2023-02-23 16:10:24 +00:00
|
|
|
pub fn build() -> Result<Self> {
|
|
|
|
// The environment variable with the path to the config file.
|
2022-10-28 22:41:02 +00:00
|
|
|
let config_file_var = "CF_CONFIG_FILE";
|
|
|
|
let config_path = env::var(config_file_var)
|
2022-10-29 17:09:56 +00:00
|
|
|
.with_context(|| format!("Environment variable {config_file_var} missing!"))?;
|
2022-10-28 22:41:02 +00:00
|
|
|
|
2023-02-23 16:10:24 +00:00
|
|
|
let file = File::open(&config_path)
|
2022-10-29 17:09:56 +00:00
|
|
|
.with_context(|| format!("Can not open the config file at the path {config_path}"))?;
|
2023-02-23 16:10:24 +00:00
|
|
|
let reader = BufReader::new(file);
|
|
|
|
let config =
|
|
|
|
serde_yaml::from_reader(reader).context("Can not parse the YAML config file!")?;
|
2022-10-28 22:41:02 +00:00
|
|
|
|
2022-10-29 17:09:56 +00:00
|
|
|
Ok(config)
|
2022-10-28 22:41:02 +00:00
|
|
|
}
|
|
|
|
}
|