Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 18 additions & 46 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -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.
///
Expand Down Expand Up @@ -62,7 +62,7 @@ pub fn toml_parser(content: &str) -> HashMap<String, TOMLSchema> {
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() {
Expand All @@ -88,13 +88,13 @@ pub fn toml_parser(content: &str) -> HashMap<String, TOMLSchema> {
submap = HashMap::new();
continue;
}

// Split key and value
let parts = line.split("=").collect::<Vec<&str>>();
if parts.len() != 2 {
continue;
}

// Remove leading and trailing whitespace
let key = parts[0]
.trim()
Expand All @@ -103,7 +103,7 @@ pub fn toml_parser(content: &str) -> HashMap<String, TOMLSchema> {
if key.is_empty() {
continue;
}

// Remove leading and trailing whitespace
let value = parts[1].trim();
let value = if value.contains('\'') || value.contains('"') {
Expand Down Expand Up @@ -152,14 +152,14 @@ pub fn toml_parser(content: &str) -> HashMap<String, TOMLSchema> {
/// 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<String>,
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<String, String>,
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion src/hteapot/brew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl HttpRequest {
println!("Read timeout");
break;
}
Err(_e) => return Err("Error reading"),
Err(e) => return Err("Error reading"),
}
}

Expand Down
25 changes: 23 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
};
Expand Down