webserver_colin_ugo/config/
mod.rs

1use std::collections::HashMap;
2use std::fs;
3use std::path::Path;
4use toml;
5use serde::Deserialize;
6
7/// Configuration pour le serveur web
8#[derive(Debug, Clone, Deserialize)]
9pub struct ServerConfig {
10    pub host: String,
11    pub port: u16,
12    pub public_dir: String,
13    pub log_file: String,
14    pub index_file: String,
15    pub not_found_page: String,
16    pub mime_types: HashMap<String, String>,
17}
18
19impl Default for ServerConfig {
20    /// Crée une nouvelle configuration de serveur avec des valeurs par défaut.
21    ///
22    /// # Retourne
23    ///
24    /// * Une instance de `ServerConfig` avec les valeurs par défaut.
25    fn default() -> Self {
26        let mut mime_types = HashMap::new();
27        mime_types.insert("html".to_string(), "text/html".to_string());
28        mime_types.insert("css".to_string(), "text/css".to_string());
29        mime_types.insert("js".to_string(), "application/javascript".to_string());
30        mime_types.insert("jpg".to_string(), "image/jpeg".to_string());
31        mime_types.insert("jpeg".to_string(), "image/jpeg".to_string());
32        mime_types.insert("png".to_string(), "image/png".to_string());
33        mime_types.insert("gif".to_string(), "image/gif".to_string());
34        mime_types.insert("txt".to_string(), "text/plain".to_string());
35        mime_types.insert("java".to_string(), "text/plain".to_string());
36        
37        Self {
38            host: "localhost".to_string(),
39            port: 8080,
40            public_dir: "public".to_string(),
41            log_file: "server.log".to_string(),
42            index_file: "index.html".to_string(),
43            not_found_page: "404.html".to_string(),
44            mime_types,
45        }
46    }
47}
48
49impl ServerConfig {
50    /// Crée une nouvelle instance de `ServerConfig` avec les valeurs fournies.
51    ///
52    /// # Arguments
53    ///
54    /// * `host` - L'adresse hôte du serveur.
55    /// * `port` - Le port sur lequel le serveur écoutera.
56    /// * `public_dir` - Le répertoire public à servir.
57    ///
58    /// # Retourne
59    ///
60    /// * Une instance de `ServerConfig` avec les valeurs fournies.
61    pub fn new(host: String, port: u16, public_dir: String) -> Self {
62        let default = Self::default();
63        Self {
64            host,
65            port,
66            public_dir,
67            log_file: default.log_file,
68            index_file: default.index_file,
69            not_found_page: default.not_found_page,
70            mime_types: default.mime_types,
71        }
72    }
73
74    /// Charge la configuration depuis le fichier TOML.
75    ///
76    /// # Arguments
77    ///
78    /// * `file_path` - Le chemin vers le fichier TOML.
79    ///
80    /// # Retourne
81    ///
82    /// * `Result<Self, String>` - La configuration chargée ou une erreur.
83    pub fn from_file<P: AsRef<Path>>(file_path: P) -> Result<Self, String> {
84        let contents = fs::read_to_string(&file_path)
85            .map_err(|e| format!("Impossible de lire le fichier de configuration: {}", e))?;
86        
87        toml::from_str(&contents)
88            .map_err(|e| format!("Erreur lors de l'analyse du fichier TOML: {}", e))
89    }
90
91    /// Retourne l'adresse de liaison du serveur sous forme de chaîne.
92    ///
93    /// # Retourne
94    ///
95    /// * Une chaîne représentant l'adresse de liaison du serveur.
96    pub fn bind_address(&self) -> String {
97        format!("{}:{}", self.host, self.port)
98    }
99}
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104
105    #[test]
106    fn test_default_config() {
107        let config = ServerConfig::default();
108        assert_eq!(config.host, "localhost");
109        assert_eq!(config.port, 8080);
110        assert_eq!(config.public_dir, "public");
111        assert_eq!(config.log_file, "server.log");
112        assert_eq!(config.index_file, "index.html");
113        assert_eq!(config.not_found_page, "404.html");
114        assert!(config.mime_types.contains_key("html"));
115        assert_eq!(config.mime_types.get("html").unwrap(), "text/html");
116    }
117
118    #[test]
119    fn test_custom_config() {
120        let config = ServerConfig::new("127.0.0.1".to_string(), 3000, "./static".to_string());
121        assert_eq!(config.host, "127.0.0.1");
122        assert_eq!(config.port, 3000);
123        assert_eq!(config.public_dir, "./static");
124        assert_eq!(config.log_file, "server.log");
125        assert_eq!(config.index_file, "index.html"); 
126        assert_eq!(config.not_found_page, "404.html"); 
127    }
128
129    #[test]
130    fn test_bind_address() {
131        let config = ServerConfig::new("127.0.0.1".to_string(), 3000, "./static".to_string());
132        assert_eq!(config.bind_address(), "127.0.0.1:3000");
133    }
134}