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, path :: Path } ;
6+ use std:: { any:: Any , collections:: HashMap , fs} ;
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 ) ]
154154pub 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,35 +192,6 @@ 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-
224195 /// Loads configuration from a TOML file, returning defaults on failure.
225196 ///
226197 /// Expects the file to contain `[HTEAPOT]` and optionally `[proxy]` sections.
@@ -253,13 +224,13 @@ impl Config {
253224
254225 // Suggested alternative parsing logic
255226 // if let Some(proxy_map) = map.get("proxy") {
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- // }
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+ // }
263234 // }
264235
265236 // Extract main configuration
@@ -268,6 +239,7 @@ impl Config {
268239 // Suggested alternative parsing logic (Not working)
269240 // let map = map.get("HTEAPOT").unwrap_or(&TOMLSchema::new());
270241
242+
271243 Config {
272244 port : map. get2 ( "port" ) . unwrap_or ( 8080 ) ,
273245 host : map. get2 ( "host" ) . unwrap_or ( "" . to_string ( ) ) ,
0 commit comments