1 parent
a643e7e3ab
commit
52d5f1745a
9 files changed
+120
-16
No files matched your search
@@ -43,6 +43,9 @@ pub struct AppConfig {
|
||||
#[serde(default = "default_data_dir")]
|
||||
pub data_dir: String,
|
||||
|
||||
#[serde(default)]
|
||||
pub log_dir: Option<String>,
|
||||
|
||||
#[serde(default = "default_session_timeout_hours")]
|
||||
pub session_timeout_hours: u64,
|
||||
|
||||
@@ -228,6 +231,7 @@ impl Default for AppConfig {
|
||||
max_attachment_size_mb: default_max_attachment_size_mb(),
|
||||
max_message_size_mb: default_max_message_size_mb(),
|
||||
data_dir: default_data_dir(),
|
||||
log_dir: None,
|
||||
session_timeout_hours: default_session_timeout_hours(),
|
||||
remember_me_enabled: default_remember_me_enabled(),
|
||||
remember_me_days: default_remember_me_days(),
|
||||
@@ -385,6 +389,15 @@ impl AppConfig {
|
||||
if self.port == 0 || self.imap_port == 0 || self.smtp_port == 0 || self.sieve_port == 0 {
|
||||
return Err("network ports must be greater than zero".to_string());
|
||||
}
|
||||
if let Some(log_dir) = self.log_dir.as_deref() {
|
||||
let log_dir = log_dir.trim();
|
||||
if log_dir.is_empty() {
|
||||
return Err("LOG_DIR / log_dir cannot be empty".to_string());
|
||||
}
|
||||
if !std::path::Path::new(log_dir).is_absolute() {
|
||||
return Err("LOG_DIR / log_dir must be an absolute path".to_string());
|
||||
}
|
||||
}
|
||||
if self.max_attachment_size_mb == 0 || self.max_attachment_size_mb > 512 {
|
||||
return Err("MAX_ATTACHMENT_SIZE_MB must be between 1 and 512".to_string());
|
||||
}
|
||||
@@ -475,6 +488,7 @@ mod tests {
|
||||
assert_eq!(config.max_attachment_size_mb, 10);
|
||||
assert_eq!(config.max_message_size_mb, 50);
|
||||
assert_eq!(config.data_dir, "/data");
|
||||
assert!(config.log_dir.is_none());
|
||||
assert_eq!(config.session_timeout_hours, 24);
|
||||
assert!(config.remember_me_enabled);
|
||||
assert_eq!(config.remember_me_days, 30);
|
||||
@@ -510,6 +524,7 @@ mod tests {
|
||||
.merge(("max_attachment_size_mb", 40usize))
|
||||
.merge(("max_message_size_mb", 80usize))
|
||||
.merge(("data_dir", "/var/rustbox"))
|
||||
.merge(("log_dir", "/var/log/rustbox"))
|
||||
.merge(("session_timeout_hours", 48u64))
|
||||
.merge(("remember_me_enabled", false))
|
||||
.merge(("remember_me_days", 14u64))
|
||||
@@ -536,6 +551,7 @@ mod tests {
|
||||
assert_eq!(config.max_attachment_size_mb, 40);
|
||||
assert_eq!(config.max_message_size_mb, 80);
|
||||
assert_eq!(config.data_dir, "/var/rustbox");
|
||||
assert_eq!(config.log_dir.as_deref(), Some("/var/log/rustbox"));
|
||||
assert_eq!(config.session_timeout_hours, 48);
|
||||
assert!(!config.remember_me_enabled);
|
||||
assert_eq!(config.remember_me_days, 14);
|
||||
|
||||
+44
-10
@@ -13,7 +13,8 @@ mod sieve;
|
||||
mod smtp;
|
||||
mod ui;
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::fs::OpenOptions;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
|
||||
use config::AppConfig;
|
||||
@@ -32,22 +33,25 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
tracing_subscriber::registry()
|
||||
.with(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")))
|
||||
.with(fmt::layer().json())
|
||||
.init();
|
||||
|
||||
let loaded_config = AppConfig::load()?;
|
||||
if args.iter().any(|arg| arg == "--check-config") {
|
||||
loaded_config.validate().map_err(|message| {
|
||||
std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidInput,
|
||||
format!("invalid RustBox configuration: {message}"),
|
||||
)
|
||||
})?;
|
||||
println!("RustBox configuration OK");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
init_tracing(loaded_config.log_dir.as_deref())?;
|
||||
loaded_config.validate().map_err(|message| {
|
||||
std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidInput,
|
||||
format!("invalid RustBox configuration: {message}"),
|
||||
)
|
||||
})?;
|
||||
if args.iter().any(|arg| arg == "--check-config") {
|
||||
println!("RustBox configuration OK");
|
||||
return Ok(());
|
||||
}
|
||||
let config = Arc::new(loaded_config);
|
||||
|
||||
let store = Arc::new(SessionStore::new_with_tls_verify(
|
||||
@@ -107,6 +111,36 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn init_tracing(log_dir: Option<&str>) -> Result<(), Box<dyn std::error::Error>> {
|
||||
if let Some(log_dir) = log_dir.map(str::trim).filter(|value| !value.is_empty()) {
|
||||
std::fs::create_dir_all(log_dir)?;
|
||||
let log_path = std::path::Path::new(log_dir).join("rustbox.log");
|
||||
let mut options = OpenOptions::new();
|
||||
options.create(true).append(true);
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
options.mode(0o600);
|
||||
}
|
||||
let file = options.open(log_path)?;
|
||||
tracing_subscriber::registry()
|
||||
.with(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")))
|
||||
.with(
|
||||
fmt::layer()
|
||||
.json()
|
||||
.with_ansi(false)
|
||||
.with_writer(Mutex::new(file)),
|
||||
)
|
||||
.init();
|
||||
} else {
|
||||
tracing_subscriber::registry()
|
||||
.with(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")))
|
||||
.with(fmt::layer().json().with_ansi(false))
|
||||
.init();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn shutdown_signal(idle_manager: Arc<IdleManager>) {
|
||||
let ctrl_c = async {
|
||||
if let Err(error) = tokio::signal::ctrl_c().await {
|
||||
|
||||
Reference in new issue
Block a user