configurable max website length
This commit is contained in:
parent
81d44da41c
commit
0a827492c0
3 changed files with 21 additions and 2 deletions
|
|
@ -12,6 +12,7 @@ pub struct Config {
|
|||
pub honeypot: bool,
|
||||
pub max_name_length: usize,
|
||||
pub max_message_length: usize,
|
||||
pub max_website_length: usize,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
|
|
@ -47,6 +48,10 @@ impl Config {
|
|||
.unwrap_or_else(|_| "1000".into())
|
||||
.parse()
|
||||
.map_err(|_| "BOOK_MAX_MESSAGE_LENGTH must be a number")?,
|
||||
max_website_length: env::var("BOOK_MAX_WEBSITE_LENGTH")
|
||||
.unwrap_or_else(|_| "100".into())
|
||||
.parse()
|
||||
.map_err(|_| "BOOK_MAX_WEBSITE_LENGTH must be a number")?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -54,9 +59,13 @@ impl Config {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::sync::Mutex;
|
||||
|
||||
static ENV_LOCK: Mutex<()> = Mutex::new(());
|
||||
|
||||
#[test]
|
||||
fn test_from_env() {
|
||||
let _lock = ENV_LOCK.lock().unwrap();
|
||||
env::set_var("BOOK_PORT", "9999");
|
||||
env::set_var("BOOK_DATA_DIR", "/tmp/gb");
|
||||
env::set_var("BOOK_SITE_TITLE", "test.rs");
|
||||
|
|
@ -83,6 +92,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_defaults() {
|
||||
let _lock = ENV_LOCK.lock().unwrap();
|
||||
env::set_var("BOOK_SITE_URL", "https://test.rs");
|
||||
env::set_var("BOOK_TELEGRAM_BOT_TOKEN", "123:ABC");
|
||||
env::set_var("BOOK_TELEGRAM_CHAT_ID", "12345");
|
||||
|
|
@ -99,6 +109,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_missing_required() {
|
||||
let _lock = ENV_LOCK.lock().unwrap();
|
||||
env::remove_var("BOOK_SITE_URL");
|
||||
env::remove_var("BOOK_TELEGRAM_BOT_TOKEN");
|
||||
env::remove_var("BOOK_TELEGRAM_CHAT_ID");
|
||||
|
|
|
|||
|
|
@ -69,8 +69,9 @@ async fn submit(
|
|||
if max_name > 0 && name.len() > max_name {
|
||||
return Html(format!("Name is too long (max {max_name} chars)."));
|
||||
}
|
||||
if website.len() > 100 {
|
||||
return Html("Website is too long (max 100 chars).".to_string());
|
||||
let max_web = state.config.max_website_length;
|
||||
if max_web > 0 && website.len() > max_web {
|
||||
return Html(format!("Website is too long (max {max_web} chars)."));
|
||||
}
|
||||
let max_msg = state.config.max_message_length;
|
||||
if max_msg > 0 && message.len() > max_msg {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue