From 795ae64aa293d5a4d212dc7311fc6623aec7f71e Mon Sep 17 00:00:00 2001 From: rattatwinko Date: Wed, 23 Jul 2025 23:22:25 +0200 Subject: [PATCH] peak refactoring. - main method was too complex - now its better, **to read** --- .idea/material_theme_project_new.xml | 4 +- html/index.html | 1 + html/script.js | 2 +- src/main.rs | 76 +++++++++++++++++----------- 4 files changed, 52 insertions(+), 31 deletions(-) diff --git a/.idea/material_theme_project_new.xml b/.idea/material_theme_project_new.xml index eab7168..ebf0656 100644 --- a/.idea/material_theme_project_new.xml +++ b/.idea/material_theme_project_new.xml @@ -3,7 +3,9 @@ diff --git a/html/index.html b/html/index.html index cc80df3..2bfa053 100644 --- a/html/index.html +++ b/html/index.html @@ -10,6 +10,7 @@

Hello from RSTTP

+

Special Heading2

diff --git a/html/script.js b/html/script.js index 189d452..874be91 100644 --- a/html/script.js +++ b/html/script.js @@ -3,7 +3,7 @@ document.addEventListener("DOMContentLoaded", () => { function updateDateTime() { const now = new Date(); - dateElement.textContent = `Current date and time: ${now.toLocaleString()}`; + dateElement.textContent = `Current date and time: ${now}`; } // Update once on load diff --git a/src/main.rs b/src/main.rs index b801e04..823df9d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -258,59 +258,68 @@ async fn run_server( } } +#[derive(Clone)] +struct ServerConfig { + address: String, + base_path: Arc, + cache: Arc>, + stats: Arc, +} + #[tokio::main] async fn main() -> io::Result<()> { - let args: Vec = std::env::args().collect(); + let config = parse_arguments()?; + let server_handle = spawn_server(config.clone()); + run_interface(config, server_handle).await +} +fn parse_arguments() -> io::Result { + let args: Vec = std::env::args().collect(); if args.len() < 4 { eprintln!("Usage: {}

", args[0]); std::process::exit(1); } - let address = &args[1]; - let port = &args[2]; let path = Path::new(&args[3]); - if !path.exists() || !path.is_dir() { eprintln!("Error: The specified path does not exist or is not a directory"); std::process::exit(1); } - let base_path = Arc::new(path.canonicalize()?); - let bind_addr = format!("{}:{}", address, port); - let cache = Arc::new(RwLock::new(FileCache::new(3600))); - let stats = Arc::new(ServerStats::new()); + let bind_addr = format!("{}:{}", &args[1], &args[2]); - // Spawn the server in a separate task - let server_stats = Arc::clone(&stats); - let server_cache = Arc::clone(&cache); - let server_base_path = Arc::clone(&base_path); - let server_addr = bind_addr.clone(); - - let server_handle = tokio::spawn(async move { + Ok(ServerConfig { + address: bind_addr, + base_path: Arc::new(path.canonicalize()?), + cache: Arc::new(RwLock::new(FileCache::new(3600))), + stats: Arc::new(ServerStats::new()), + }) +} + +fn spawn_server(config: ServerConfig) -> tokio::task::JoinHandle> { + tokio::spawn(async move { run_server( - server_addr, - server_base_path, - server_cache, - server_stats, + config.address, + config.base_path, + config.cache, + config.stats, ).await - }); + }) +} - // Initialize and run TUI +async fn run_interface( + config: ServerConfig, + server_handle: tokio::task::JoinHandle>, +) -> io::Result<()> { let mut tui = Tui::new()?; let mut background = false; while !background { - match tui.run(Arc::clone(&stats), bind_addr.clone()) { + match tui.run(Arc::clone(&config.stats), config.address.clone()) { Ok(true) => background = true, // Ctrl+B pressed Ok(false) => { - // Check for Ctrl+C or continue - if event::poll(std::time::Duration::from_millis(100))? { - if let Event::Key(key) = event::read()? { - if key.code == KeyCode::Char('c') && key.modifiers == KeyModifiers::CONTROL { - break; - } - } + if should_exit()? { + break; } } Err(e) => { @@ -326,4 +335,13 @@ async fn main() -> io::Result<()> { } Ok(()) +} + +fn should_exit() -> io::Result { + if event::poll(std::time::Duration::from_millis(100))? { + if let Event::Key(key) = event::read()? { + return Ok(key.code == KeyCode::Char('c') && key.modifiers == KeyModifiers::CONTROL); + } + } + Ok(false) } \ No newline at end of file