From f9f2850a528ec474a9ebf6d03b68acd81a53f110 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz <17555470+Az107@users.noreply.github.com> Date: Sat, 28 Jun 2025 22:42:46 +0200 Subject: [PATCH] Revert "Feature/better config (#36)" This reverts commit ad4026055a0366d8458fd553467f912100622c27. --- src/config.rs | 64 +++++++++++++-------------------------------- src/hteapot/brew.rs | 2 +- src/main.rs | 25 ++++++++++++++++-- 3 files changed, 42 insertions(+), 49 deletions(-) diff --git a/src/config.rs b/src/config.rs index 50de8e7..42e68c1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,9 +1,9 @@ // Written by Alberto Ruiz 2024-04-07 (Happy 3th monthsary) -// +// // This is the config module: responsible for loading application configuration // from a file and providing structured access to settings. -use std::{any::Any, collections::HashMap, fs, path::Path}; +use std::{any::Any, collections::HashMap, fs}; /// Dynamic TOML value representation. /// @@ -62,7 +62,7 @@ pub fn toml_parser(content: &str) -> HashMap { let mut map = HashMap::new(); let mut submap = HashMap::new(); let mut title = "".to_string(); - + let lines = content.split("\n"); for line in lines { if line.starts_with("#") || line.is_empty() { @@ -88,13 +88,13 @@ pub fn toml_parser(content: &str) -> HashMap { submap = HashMap::new(); continue; } - + // Split key and value let parts = line.split("=").collect::>(); if parts.len() != 2 { continue; } - + // Remove leading and trailing whitespace let key = parts[0] .trim() @@ -103,7 +103,7 @@ pub fn toml_parser(content: &str) -> HashMap { if key.is_empty() { continue; } - + // Remove leading and trailing whitespace let value = parts[1].trim(); let value = if value.contains('\'') || value.contains('"') { @@ -152,14 +152,14 @@ pub fn toml_parser(content: &str) -> HashMap { /// such as host, port, caching behavior, and proxy rules. #[derive(Debug)] pub struct Config { - pub port: u16, // Port number to listen - pub host: String, // Host name or IP - pub root: String, // Root directory to serve files + pub port: u16, // Port number to listen + pub host: String, // Host name or IP + pub root: String, // Root directory to serve files pub cache: bool, pub cache_ttl: u16, pub threads: u16, pub log_file: Option, - pub index: String, // Index file to serve by default + pub index: String, // Index file to serve by default // pub error: String, // Error file to serve when a file is not found pub proxy_rules: HashMap, } @@ -192,35 +192,6 @@ impl Config { } } - pub fn new_serve(path: &str) -> Config { - let mut s_path = "./".to_string(); - s_path.push_str(path); - let serving_path = Path::new(&s_path); - let file_name: &str; - let root_dir: String; - if serving_path.is_file() { - let parent_path = serving_path.parent().unwrap(); - root_dir = parent_path.to_str().unwrap().to_string(); - file_name = serving_path.file_name().unwrap().to_str().unwrap(); - } else { - file_name = "index.html"; - root_dir = serving_path.to_str().unwrap().to_string(); - }; - - Config { - port: 8080, - host: "0.0.0.0".to_string(), - root: root_dir, - index: file_name.to_string(), - log_file: None, - - threads: 1, - cache: false, - cache_ttl: 0, - proxy_rules: HashMap::new(), - } - } - /// Loads configuration from a TOML file, returning defaults on failure. /// /// Expects the file to contain `[HTEAPOT]` and optionally `[proxy]` sections. @@ -253,13 +224,13 @@ impl Config { // Suggested alternative parsing logic // if let Some(proxy_map) = map.get("proxy") { - // for k in proxy_map.keys() { - // if let Some(url) = proxy_map.get2(k) { - // proxy_rules.insert(k.clone(), url); - // } else { - // println!("Missing or invalid proxy URL for key: {}", k); - // } - // } + // for k in proxy_map.keys() { + // if let Some(url) = proxy_map.get2(k) { + // proxy_rules.insert(k.clone(), url); + // } else { + // println!("Missing or invalid proxy URL for key: {}", k); + // } + // } // } // Extract main configuration @@ -268,6 +239,7 @@ impl Config { // Suggested alternative parsing logic (Not working) // let map = map.get("HTEAPOT").unwrap_or(&TOMLSchema::new()); + Config { port: map.get2("port").unwrap_or(8080), host: map.get2("host").unwrap_or("".to_string()), diff --git a/src/hteapot/brew.rs b/src/hteapot/brew.rs index 23f0cdb..26bfb24 100644 --- a/src/hteapot/brew.rs +++ b/src/hteapot/brew.rs @@ -123,7 +123,7 @@ impl HttpRequest { println!("Read timeout"); break; } - Err(_e) => return Err("Error reading"), + Err(e) => return Err("Error reading"), } } diff --git a/src/main.rs b/src/main.rs index d8a502c..69799e6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -189,8 +189,29 @@ fn main() { return; } "--serve" | "-s" => { - let path = args.get(2).unwrap().clone(); - config::Config::new_serve(&path) + let mut c = config::Config::new_default(); + let serving_path = Some(args.get(2).unwrap().clone()); + let serving_path_str = serving_path.unwrap(); + let serving_path_str = serving_path_str.as_str(); + let serving_path = Path::new(serving_path_str); + if serving_path.is_dir() { + c.root = serving_path.to_str().unwrap_or_default().to_string(); + } else { + c.index = serving_path + .file_name() + .unwrap() + .to_str() + .unwrap_or_default() + .to_string(); + c.root = serving_path + .parent() + .unwrap_or(Path::new("./")) + .to_str() + .unwrap_or_default() + .to_string(); + } + c.host = "0.0.0.0".to_string(); + c } _ => config::Config::load_config(&args[1]), };