Skip to content

Commit 895a023

Browse files
chore: remove ServerConfigRoot
1 parent e5194c1 commit 895a023

21 files changed

+458
-646
lines changed

ferron-common/src/lib.rs

Lines changed: 6 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::HashMap, error::Error, future::Future, net::SocketAddr, pin::Pin};
1+
use std::{error::Error, future::Future, net::SocketAddr, pin::Pin};
22

33
use async_channel::Sender;
44
use async_trait::async_trait;
@@ -394,86 +394,6 @@ impl ResponseDataBuilder {
394394
}
395395
}
396396

397-
/// Represents the root configuration for the server.
398-
///
399-
/// This struct encapsulates a mapping between configuration property names and their corresponding
400-
/// YAML values, allowing for organized access to server settings.
401-
pub struct ServerConfigRoot {
402-
hashmap: HashMap<String, ServerConfig>,
403-
}
404-
405-
impl ServerConfigRoot {
406-
/// Constructs a new `ServerConfigRoot` instance from a given YAML hash.
407-
///
408-
/// This function takes a YAML object, expects it to be a hash (mapping), and converts it into
409-
/// a `HashMap` where each key is a `String` and each value is a `Yaml` object. This allows for
410-
/// easy retrieval of configuration properties by their names.
411-
///
412-
/// # Parameters
413-
///
414-
/// - `hashmap_yaml`: A reference to `ServerConfig` object expected to be a hash containing configuration properties.
415-
///
416-
/// # Returns
417-
///
418-
/// A `ServerConfigRoot` instance containing the parsed configuration properties.
419-
pub fn new(hashmap_yaml: &ServerConfig) -> Self {
420-
let mut hashmap = HashMap::new();
421-
422-
if let Some(hashmap_yaml) = hashmap_yaml.as_hash() {
423-
for (key, value) in hashmap_yaml.iter() {
424-
if let Some(key_str) = key.as_str() {
425-
hashmap.insert(key_str.to_string(), value.clone());
426-
}
427-
}
428-
}
429-
430-
ServerConfigRoot { hashmap }
431-
}
432-
433-
/// Constructs a new `ServerConfigRoot` instance from an existing `HashMap<String, ServerConfig>`.
434-
///
435-
/// # Parameters
436-
///
437-
/// - `hashmap`: A `HashMap<String, ServerConfig>` hashmap.
438-
///
439-
/// # Returns
440-
///
441-
/// A `ServerConfigRoot` instance containing the parsed configuration properties.
442-
pub fn from_hash(hashmap: HashMap<String, ServerConfig>) -> Self {
443-
ServerConfigRoot { hashmap }
444-
}
445-
446-
/// Retrieves a configuration property by its name.
447-
///
448-
/// This function looks up the provided property name in the internal hashmap and returns
449-
/// a clone of the corresponding `Yaml` value if found. If the property is not found, it
450-
/// returns `Yaml::BadValue`.
451-
///
452-
/// # Parameters
453-
///
454-
/// - `property`: A string slice representing the name of the configuration property to retrieve.
455-
///
456-
/// # Returns
457-
///
458-
/// A `ServerConfig` object corresponding to the requested property, or `ServerConfig::BadValue` if the property
459-
/// does not exist in the configuration.
460-
pub fn get(&self, property: &str) -> ServerConfig {
461-
match self.hashmap.get(property) {
462-
Some(yaml) => yaml.clone(),
463-
None => ServerConfig::BadValue,
464-
}
465-
}
466-
467-
/// Provides a reference to an underlying hashmap.
468-
///
469-
/// # Returns
470-
///
471-
/// A reference to `HashMap<String, ServerConfig>` hashmap.
472-
pub fn as_hash(&self) -> &HashMap<String, ServerConfig> {
473-
&self.hashmap
474-
}
475-
}
476-
477397
/// Defines the interface for server module handlers, specifying how requests should be processed.
478398
#[async_trait]
479399
pub trait ServerModuleHandlers {
@@ -492,7 +412,7 @@ pub trait ServerModuleHandlers {
492412
async fn request_handler(
493413
&mut self,
494414
request: RequestData,
495-
config: &ServerConfigRoot,
415+
config: &ServerConfig,
496416
socket_data: &SocketData,
497417
error_logger: &ErrorLogger,
498418
) -> Result<ResponseData, Box<dyn Error + Send + Sync>>;
@@ -512,7 +432,7 @@ pub trait ServerModuleHandlers {
512432
async fn proxy_request_handler(
513433
&mut self,
514434
request: RequestData,
515-
config: &ServerConfigRoot,
435+
config: &ServerConfig,
516436
socket_data: &SocketData,
517437
error_logger: &ErrorLogger,
518438
) -> Result<ResponseData, Box<dyn Error + Send + Sync>>;
@@ -572,7 +492,7 @@ pub trait ServerModuleHandlers {
572492
&mut self,
573493
upgraded_request: HyperUpgraded,
574494
connect_address: &str,
575-
config: &ServerConfigRoot,
495+
config: &ServerConfig,
576496
socket_data: &SocketData,
577497
error_logger: &ErrorLogger,
578498
) -> Result<(), Box<dyn Error + Send + Sync>>;
@@ -601,7 +521,7 @@ pub trait ServerModuleHandlers {
601521
&mut self,
602522
websocket: HyperWebsocket,
603523
uri: &hyper::Uri,
604-
config: &ServerConfigRoot,
524+
config: &ServerConfig,
605525
socket_data: &SocketData,
606526
error_logger: &ErrorLogger,
607527
) -> Result<(), Box<dyn Error + Send + Sync>>;
@@ -616,11 +536,7 @@ pub trait ServerModuleHandlers {
616536
/// # Returns
617537
///
618538
/// `true` if the module is a module supporting WebSocket requests, or `false` otherwise.
619-
fn does_websocket_requests(
620-
&mut self,
621-
config: &ServerConfigRoot,
622-
socket_data: &SocketData,
623-
) -> bool;
539+
fn does_websocket_requests(&mut self, config: &ServerConfig, socket_data: &SocketData) -> bool;
624540
}
625541

626542
/// Represents a server module that can provide handlers for processing requests.

ferron/src/modules/blocklist.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::sync::Arc;
33

44
use async_trait::async_trait;
55
use ferron_common::{
6-
ErrorLogger, HyperResponse, RequestData, ResponseData, ServerConfig, ServerConfigRoot,
7-
ServerModule, ServerModuleHandlers, SocketData,
6+
ErrorLogger, HyperResponse, RequestData, ResponseData, ServerConfig, ServerModule,
7+
ServerModuleHandlers, SocketData,
88
};
99
use ferron_common::{HyperUpgraded, WithRuntime};
1010
use hyper::StatusCode;
@@ -62,7 +62,7 @@ impl ServerModuleHandlers for BlockListModuleHandlers {
6262
async fn request_handler(
6363
&mut self,
6464
request: RequestData,
65-
_config: &ServerConfigRoot,
65+
_config: &ServerConfig,
6666
socket_data: &SocketData,
6767
_error_logger: &ErrorLogger,
6868
) -> Result<ResponseData, Box<dyn Error + Send + Sync>> {
@@ -82,7 +82,7 @@ impl ServerModuleHandlers for BlockListModuleHandlers {
8282
async fn proxy_request_handler(
8383
&mut self,
8484
request: RequestData,
85-
_config: &ServerConfigRoot,
85+
_config: &ServerConfig,
8686
_socket_data: &SocketData,
8787
_error_logger: &ErrorLogger,
8888
) -> Result<ResponseData, Box<dyn Error + Send + Sync>> {
@@ -107,7 +107,7 @@ impl ServerModuleHandlers for BlockListModuleHandlers {
107107
&mut self,
108108
_upgraded_request: HyperUpgraded,
109109
_connect_address: &str,
110-
_config: &ServerConfigRoot,
110+
_config: &ServerConfig,
111111
_socket_data: &SocketData,
112112
_error_logger: &ErrorLogger,
113113
) -> Result<(), Box<dyn Error + Send + Sync>> {
@@ -122,18 +122,14 @@ impl ServerModuleHandlers for BlockListModuleHandlers {
122122
&mut self,
123123
_websocket: HyperWebsocket,
124124
_uri: &hyper::Uri,
125-
_config: &ServerConfigRoot,
125+
_config: &ServerConfig,
126126
_socket_data: &SocketData,
127127
_error_logger: &ErrorLogger,
128128
) -> Result<(), Box<dyn Error + Send + Sync>> {
129129
Ok(())
130130
}
131131

132-
fn does_websocket_requests(
133-
&mut self,
134-
_config: &ServerConfigRoot,
135-
_socket_data: &SocketData,
136-
) -> bool {
132+
fn does_websocket_requests(&mut self, _config: &ServerConfig, _socket_data: &SocketData) -> bool {
137133
false
138134
}
139135
}

ferron/src/modules/default_handler_checks.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::error::Error;
22

33
use async_trait::async_trait;
44
use ferron_common::{
5-
ErrorLogger, HyperResponse, RequestData, ResponseData, ServerConfigRoot, ServerModule,
5+
ErrorLogger, HyperResponse, RequestData, ResponseData, ServerConfig, ServerModule,
66
ServerModuleHandlers, SocketData,
77
};
88
use ferron_common::{HyperUpgraded, WithRuntime};
@@ -39,7 +39,7 @@ impl ServerModuleHandlers for DefaultHandlerChecksModuleHandlers {
3939
async fn request_handler(
4040
&mut self,
4141
request: RequestData,
42-
_config: &ServerConfigRoot,
42+
_config: &ServerConfig,
4343
_socket_data: &SocketData,
4444
_error_logger: &ErrorLogger,
4545
) -> Result<ResponseData, Box<dyn Error + Send + Sync>> {
@@ -77,7 +77,7 @@ impl ServerModuleHandlers for DefaultHandlerChecksModuleHandlers {
7777
async fn proxy_request_handler(
7878
&mut self,
7979
request: RequestData,
80-
_config: &ServerConfigRoot,
80+
_config: &ServerConfig,
8181
_socket_data: &SocketData,
8282
_error_logger: &ErrorLogger,
8383
) -> Result<ResponseData, Box<dyn Error + Send + Sync>> {
@@ -106,7 +106,7 @@ impl ServerModuleHandlers for DefaultHandlerChecksModuleHandlers {
106106
&mut self,
107107
_upgraded_request: HyperUpgraded,
108108
_connect_address: &str,
109-
_config: &ServerConfigRoot,
109+
_config: &ServerConfig,
110110
_socket_data: &SocketData,
111111
_error_logger: &ErrorLogger,
112112
) -> Result<(), Box<dyn Error + Send + Sync>> {
@@ -121,18 +121,14 @@ impl ServerModuleHandlers for DefaultHandlerChecksModuleHandlers {
121121
&mut self,
122122
_websocket: HyperWebsocket,
123123
_uri: &hyper::Uri,
124-
_config: &ServerConfigRoot,
124+
_config: &ServerConfig,
125125
_socket_data: &SocketData,
126126
_error_logger: &ErrorLogger,
127127
) -> Result<(), Box<dyn Error + Send + Sync>> {
128128
Ok(())
129129
}
130130

131-
fn does_websocket_requests(
132-
&mut self,
133-
_config: &ServerConfigRoot,
134-
_socket_data: &SocketData,
135-
) -> bool {
131+
fn does_websocket_requests(&mut self, _config: &ServerConfig, _socket_data: &SocketData) -> bool {
136132
false
137133
}
138134
}

ferron/src/modules/non_standard_codes.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use async_trait::async_trait;
1515
use base64::{engine::general_purpose, Engine};
1616
use fancy_regex::RegexBuilder;
1717
use ferron_common::{
18-
ErrorLogger, HyperResponse, RequestData, ResponseData, ServerConfig, ServerConfigRoot,
19-
ServerModule, ServerModuleHandlers, SocketData,
18+
ErrorLogger, HyperResponse, RequestData, ResponseData, ServerConfig, ServerModule,
19+
ServerModuleHandlers, SocketData,
2020
};
2121
use ferron_common::{HyperUpgraded, WithRuntime};
2222
use http_body_util::{BodyExt, Empty};
@@ -227,7 +227,7 @@ impl ServerModuleHandlers for NonStandardCodesModuleHandlers {
227227
async fn request_handler(
228228
&mut self,
229229
request: RequestData,
230-
config: &ServerConfigRoot,
230+
config: &ServerConfig,
231231
socket_data: &SocketData,
232232
error_logger: &ErrorLogger,
233233
) -> Result<ResponseData, Box<dyn Error + Send + Sync>> {
@@ -401,7 +401,7 @@ impl ServerModuleHandlers for NonStandardCodesModuleHandlers {
401401
};
402402

403403
if let Some((username, password)) = parse_basic_auth(authorization_str) {
404-
if let Some(users_vec_yaml) = config.get("users").as_vec() {
404+
if let Some(users_vec_yaml) = config["users"].as_vec() {
405405
let mut authorized_user = None;
406406
for user_yaml in users_vec_yaml {
407407
if let Some(username_db) = user_yaml["name"].as_str() {
@@ -482,7 +482,7 @@ impl ServerModuleHandlers for NonStandardCodesModuleHandlers {
482482
async fn proxy_request_handler(
483483
&mut self,
484484
request: RequestData,
485-
_config: &ServerConfigRoot,
485+
_config: &ServerConfig,
486486
_socket_data: &SocketData,
487487
_error_logger: &ErrorLogger,
488488
) -> Result<ResponseData, Box<dyn Error + Send + Sync>> {
@@ -507,7 +507,7 @@ impl ServerModuleHandlers for NonStandardCodesModuleHandlers {
507507
&mut self,
508508
_upgraded_request: HyperUpgraded,
509509
_connect_address: &str,
510-
_config: &ServerConfigRoot,
510+
_config: &ServerConfig,
511511
_socket_data: &SocketData,
512512
_error_logger: &ErrorLogger,
513513
) -> Result<(), Box<dyn Error + Send + Sync>> {
@@ -522,18 +522,14 @@ impl ServerModuleHandlers for NonStandardCodesModuleHandlers {
522522
&mut self,
523523
_websocket: HyperWebsocket,
524524
_uri: &hyper::Uri,
525-
_config: &ServerConfigRoot,
525+
_config: &ServerConfig,
526526
_socket_data: &SocketData,
527527
_error_logger: &ErrorLogger,
528528
) -> Result<(), Box<dyn Error + Send + Sync>> {
529529
Ok(())
530530
}
531531

532-
fn does_websocket_requests(
533-
&mut self,
534-
_config: &ServerConfigRoot,
535-
_socket_data: &SocketData,
536-
) -> bool {
532+
fn does_websocket_requests(&mut self, _config: &ServerConfig, _socket_data: &SocketData) -> bool {
537533
false
538534
}
539535
}

0 commit comments

Comments
 (0)