diff options
-rwxr-xr-x | bin/dev-server | 3 | ||||
-rw-r--r-- | src/db/mod.rs | 6 | ||||
-rw-r--r-- | src/mail.rs | 17 | ||||
-rw-r--r-- | src/model/config.rs | 6 | ||||
-rw-r--r-- | src/utils/cookie.rs | 2 | ||||
-rw-r--r-- | templates/income/create.html | 4 | ||||
-rw-r--r-- | templates/income/update.html | 4 | ||||
-rw-r--r-- | templates/payment/create.html | 4 | ||||
-rw-r--r-- | templates/payment/table/search.html | 2 | ||||
-rw-r--r-- | templates/payment/update.html | 4 |
10 files changed, 31 insertions, 21 deletions
diff --git a/bin/dev-server b/bin/dev-server index 195791c..197b94c 100755 --- a/bin/dev-server +++ b/bin/dev-server @@ -7,7 +7,8 @@ CMD=${1:-run} export RUST_LOG="budget=info" export AUTH_SECRET="pMkW8PR7bjhc24bbvZLIEHfcooLzrGIh" export DB_PATH="database.db" -export MOCK_MAILS="true" +export MAILS_MOCK="true" +export MAILS_FROM="budget@localhost" export SECURE_COOKIES="false" export SOCKET_ADDRESS="0.0.0.0:3000" diff --git a/src/db/mod.rs b/src/db/mod.rs index d0c4f7b..b1fdeea 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -38,7 +38,11 @@ async fn apply_migrations(conn: &Connection) -> Result<()> { .await?) } -async fn set_pragma(conn: &Connection, key: impl Into<String>, value: impl Into<String>) -> Result<()> { +async fn set_pragma( + conn: &Connection, + key: impl Into<String>, + value: impl Into<String>, +) -> Result<()> { let key = key.into(); let value = value.into(); Ok(conn diff --git a/src/mail.rs b/src/mail.rs index 7017654..d7df246 100644 --- a/src/mail.rs +++ b/src/mail.rs @@ -7,7 +7,6 @@ use tokio::process::Command; use crate::model::config::Config; static FROM_NAME: &str = "Budget"; -static FROM_ADDRESS: &str = "budget@guyonvarch.me"; #[derive(Clone)] pub struct Recipient { @@ -21,15 +20,15 @@ pub async fn send( subject: &str, message: &str, ) -> bool { - let headers = format_headers(recipients.clone(), subject); + let headers = format_headers(config, recipients.clone(), subject); log::info!( "Sending mail{}\n{}", - if config.mock_mails { " (MOCK)" } else { "" }, + if config.mails_mock { " (MOCK)" } else { "" }, headers.clone() ); - if config.mock_mails { + if config.mails_mock { true } else { let recipient_addresses = recipients @@ -41,7 +40,7 @@ pub async fn send( // https://github.com/NixOS/nixpkgs/issues/90248 let mut command = Command::new("/run/wrappers/bin/sendmail"); command.kill_on_drop(true); - command.arg("-f").arg(FROM_ADDRESS); + command.arg("-f").arg(config.mails_from.clone()); command.arg("--").args(recipient_addresses); command .stdin(Stdio::piped()) @@ -72,7 +71,11 @@ pub async fn send( } } -fn format_headers(recipients: Vec<Recipient>, subject: &str) -> String { +fn format_headers( + config: &Config, + recipients: Vec<Recipient>, + subject: &str, +) -> String { let recipients = recipients .into_iter() .map(|r| format_address(&r.name, &r.address)) @@ -82,7 +85,7 @@ fn format_headers(recipients: Vec<Recipient>, subject: &str) -> String { format!( "Date: {}\nFrom: {}\nTo: {}\nSubject: {}", Utc::now().to_rfc2822(), - format_address(FROM_NAME, FROM_ADDRESS), + format_address(FROM_NAME, &config.mails_from), recipients, subject, ) diff --git a/src/model/config.rs b/src/model/config.rs index 1fa5bb4..e0267d5 100644 --- a/src/model/config.rs +++ b/src/model/config.rs @@ -6,7 +6,8 @@ use std::str::FromStr; pub struct Config { pub auth_secret: String, pub db_path: String, - pub mock_mails: bool, + pub mails_mock: bool, + pub mails_from: String, pub secure_cookies: bool, pub socket_address: SocketAddr, } @@ -15,7 +16,8 @@ pub fn from_env() -> Result<Config, String> { Ok(Config { auth_secret: read_string("AUTH_SECRET")?, db_path: read_string("DB_PATH")?, - mock_mails: read_bool("MOCK_MAILS")?, + mails_mock: read_bool("MAILS_MOCK")?, + mails_from: read_string("MAILS_FROM")?, secure_cookies: read_bool("SECURE_COOKIES")?, socket_address: read_socket_address("SOCKET_ADDRESS")?, }) diff --git a/src/utils/cookie.rs b/src/utils/cookie.rs index c621621..e21e7d4 100644 --- a/src/utils/cookie.rs +++ b/src/utils/cookie.rs @@ -8,7 +8,7 @@ const TOKEN_BYTES: usize = 20; pub fn login(config: &Config, token: &str) -> Result<String, String> { let signed_token = signed::sign(&config.auth_secret, token)?; - Ok(cookie(config, &signed_token, 24 * 60 * 60)) + Ok(cookie(config, &signed_token, 365 * 24 * 60 * 60)) } pub fn logout(config: &Config) -> String { diff --git a/templates/income/create.html b/templates/income/create.html index 2f57250..6ea1fb0 100644 --- a/templates/income/create.html +++ b/templates/income/create.html @@ -49,7 +49,7 @@ {% for user in users %} <option value="{{ user.id }}" - {% if ("" ~ user.id) == ("" ~ user_id) %} selected {% endif %} + {% if "" ~ user.id == "" ~ user_id %} selected {% endif %} > {{ user.name }} </option> @@ -65,7 +65,7 @@ {% for month in months %} <option value="{{ loop.index }}" - {% if "" ~ loop.index == month_index %} + {% if "" ~ loop.index == "" ~ month_index %} selected {% endif %} > diff --git a/templates/income/update.html b/templates/income/update.html index f5f976e..14d76d2 100644 --- a/templates/income/update.html +++ b/templates/income/update.html @@ -52,7 +52,7 @@ {% for user in users %} <option value="{{ user.id }}" - {% if "" ~ user.id == user_id %} selected {% endif %} + {% if "" ~ user.id == "" ~ user_id %} selected {% endif %} > {{ user.name }} </option> @@ -68,7 +68,7 @@ {% for month in months %} <option value="{{ loop.index }}" - {% if "" ~ loop.index == month_index %} selected {% endif %} + {% if "" ~ loop.index == "" ~ month_index %} selected {% endif %} > {{ month }} </option> diff --git a/templates/payment/create.html b/templates/payment/create.html index 8d18c13..379662a 100644 --- a/templates/payment/create.html +++ b/templates/payment/create.html @@ -69,7 +69,7 @@ {% for user in users %} <option value="{{ user.id }}" - {% if ("" ~ user.id) == ("" ~ user_id) %} + {% if "" ~ user.id == "" ~ user_id %} selected {% endif %} > @@ -88,7 +88,7 @@ {% for category in categories %} <option value="{{ category.id }}" - {% if "" ~ category.id == category_id %} selected {% endif %} + {% if "" ~ category.id == "" ~ category_id %} selected {% endif %} > {{ category.name }} </option> diff --git a/templates/payment/table/search.html b/templates/payment/table/search.html index cb72282..3516da8 100644 --- a/templates/payment/table/search.html +++ b/templates/payment/table/search.html @@ -38,7 +38,7 @@ {% for user in users %} <option value="{{ user.id }}" - {% if user.id == query.user %} selected {% endif %} + {% if "" ~ user.id == "" ~ query.user %} selected {% endif %} > {{ user.name }} </option> diff --git a/templates/payment/update.html b/templates/payment/update.html index dfa0892..0cd7a06 100644 --- a/templates/payment/update.html +++ b/templates/payment/update.html @@ -81,7 +81,7 @@ {% for user in users %} <option value="{{ user.id }}" - {% if "" ~ user.id == user_id %} selected {% endif %} + {% if "" ~ user.id == "" ~ user_id %} selected {% endif %} > {{ user.name }} </option> @@ -97,7 +97,7 @@ {% for category in categories %} <option value="{{ category.id }}" - {% if "" ~ category.id == category_id %} selected {% endif %} + {% if "" ~ category.id == "" ~ category_id %} selected {% endif %} > {{ category.name }} </option> |