Skip to content

Commit ad40260

Browse files
authored
Feature/better config (#36)
* adhoc serve added * minor fixes
1 parent 0fb19f8 commit ad40260

3 files changed

Lines changed: 49 additions & 42 deletions

File tree

src/config.rs

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Written by Alberto Ruiz 2024-04-07 (Happy 3th monthsary)
2-
//
2+
//
33
// This is the config module: responsible for loading application configuration
44
// from a file and providing structured access to settings.
55

6-
use std::{any::Any, collections::HashMap, fs};
6+
use std::{any::Any, collections::HashMap, fs, path::Path};
77

88
/// Dynamic TOML value representation.
99
///
@@ -62,7 +62,7 @@ pub fn toml_parser(content: &str) -> HashMap<String, TOMLSchema> {
6262
let mut map = HashMap::new();
6363
let mut submap = HashMap::new();
6464
let mut title = "".to_string();
65-
65+
6666
let lines = content.split("\n");
6767
for line in lines {
6868
if line.starts_with("#") || line.is_empty() {
@@ -88,13 +88,13 @@ pub fn toml_parser(content: &str) -> HashMap<String, TOMLSchema> {
8888
submap = HashMap::new();
8989
continue;
9090
}
91-
91+
9292
// Split key and value
9393
let parts = line.split("=").collect::<Vec<&str>>();
9494
if parts.len() != 2 {
9595
continue;
9696
}
97-
97+
9898
// Remove leading and trailing whitespace
9999
let key = parts[0]
100100
.trim()
@@ -103,7 +103,7 @@ pub fn toml_parser(content: &str) -> HashMap<String, TOMLSchema> {
103103
if key.is_empty() {
104104
continue;
105105
}
106-
106+
107107
// Remove leading and trailing whitespace
108108
let value = parts[1].trim();
109109
let value = if value.contains('\'') || value.contains('"') {
@@ -152,14 +152,14 @@ pub fn toml_parser(content: &str) -> HashMap<String, TOMLSchema> {
152152
/// such as host, port, caching behavior, and proxy rules.
153153
#[derive(Debug)]
154154
pub struct Config {
155-
pub port: u16, // Port number to listen
156-
pub host: String, // Host name or IP
157-
pub root: String, // Root directory to serve files
155+
pub port: u16, // Port number to listen
156+
pub host: String, // Host name or IP
157+
pub root: String, // Root directory to serve files
158158
pub cache: bool,
159159
pub cache_ttl: u16,
160160
pub threads: u16,
161161
pub log_file: Option<String>,
162-
pub index: String, // Index file to serve by default
162+
pub index: String, // Index file to serve by default
163163
// pub error: String, // Error file to serve when a file is not found
164164
pub proxy_rules: HashMap<String, String>,
165165
}
@@ -192,6 +192,35 @@ impl Config {
192192
}
193193
}
194194

195+
pub fn new_serve(path: &str) -> Config {
196+
let mut s_path = "./".to_string();
197+
s_path.push_str(path);
198+
let serving_path = Path::new(&s_path);
199+
let file_name: &str;
200+
let root_dir: String;
201+
if serving_path.is_file() {
202+
let parent_path = serving_path.parent().unwrap();
203+
root_dir = parent_path.to_str().unwrap().to_string();
204+
file_name = serving_path.file_name().unwrap().to_str().unwrap();
205+
} else {
206+
file_name = "index.html";
207+
root_dir = serving_path.to_str().unwrap().to_string();
208+
};
209+
210+
Config {
211+
port: 8080,
212+
host: "0.0.0.0".to_string(),
213+
root: root_dir,
214+
index: file_name.to_string(),
215+
log_file: None,
216+
217+
threads: 1,
218+
cache: false,
219+
cache_ttl: 0,
220+
proxy_rules: HashMap::new(),
221+
}
222+
}
223+
195224
/// Loads configuration from a TOML file, returning defaults on failure.
196225
///
197226
/// Expects the file to contain `[HTEAPOT]` and optionally `[proxy]` sections.
@@ -224,13 +253,13 @@ impl Config {
224253

225254
// Suggested alternative parsing logic
226255
// if let Some(proxy_map) = map.get("proxy") {
227-
// for k in proxy_map.keys() {
228-
// if let Some(url) = proxy_map.get2(k) {
229-
// proxy_rules.insert(k.clone(), url);
230-
// } else {
231-
// println!("Missing or invalid proxy URL for key: {}", k);
232-
// }
233-
// }
256+
// for k in proxy_map.keys() {
257+
// if let Some(url) = proxy_map.get2(k) {
258+
// proxy_rules.insert(k.clone(), url);
259+
// } else {
260+
// println!("Missing or invalid proxy URL for key: {}", k);
261+
// }
262+
// }
234263
// }
235264

236265
// Extract main configuration
@@ -239,7 +268,6 @@ impl Config {
239268
// Suggested alternative parsing logic (Not working)
240269
// let map = map.get("HTEAPOT").unwrap_or(&TOMLSchema::new());
241270

242-
243271
Config {
244272
port: map.get2("port").unwrap_or(8080),
245273
host: map.get2("host").unwrap_or("".to_string()),

src/hteapot/brew.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl HttpRequest {
123123
println!("Read timeout");
124124
break;
125125
}
126-
Err(e) => return Err("Error reading"),
126+
Err(_e) => return Err("Error reading"),
127127
}
128128
}
129129

src/main.rs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -189,29 +189,8 @@ fn main() {
189189
return;
190190
}
191191
"--serve" | "-s" => {
192-
let mut c = config::Config::new_default();
193-
let serving_path = Some(args.get(2).unwrap().clone());
194-
let serving_path_str = serving_path.unwrap();
195-
let serving_path_str = serving_path_str.as_str();
196-
let serving_path = Path::new(serving_path_str);
197-
if serving_path.is_dir() {
198-
c.root = serving_path.to_str().unwrap_or_default().to_string();
199-
} else {
200-
c.index = serving_path
201-
.file_name()
202-
.unwrap()
203-
.to_str()
204-
.unwrap_or_default()
205-
.to_string();
206-
c.root = serving_path
207-
.parent()
208-
.unwrap_or(Path::new("./"))
209-
.to_str()
210-
.unwrap_or_default()
211-
.to_string();
212-
}
213-
c.host = "0.0.0.0".to_string();
214-
c
192+
let path = args.get(2).unwrap().clone();
193+
config::Config::new_serve(&path)
215194
}
216195
_ => config::Config::load_config(&args[1]),
217196
};

0 commit comments

Comments
 (0)