lexa_framework/server/
settings.rs

1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ Copyright: (c) 2023, Mike 'PhiSyX' S. (https://github.com/PhiSyX)         ┃
3// ┃ SPDX-License-Identifier: MPL-2.0                                          ┃
4// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
5// ┃                                                                           ┃
6// ┃  This Source Code Form is subject to the terms of the Mozilla Public      ┃
7// ┃  License, v. 2.0. If a copy of the MPL was not distributed with this      ┃
8// ┃  file, You can obtain one at https://mozilla.org/MPL/2.0/.                ┃
9// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
10
11use std::{net, path};
12
13// --------- //
14// Structure //
15// --------- //
16
17#[derive(Debug)]
18#[derive(serde::Deserialize)]
19pub struct Settings {
20	/// Nom d'hôte / IP de connexion du serveur.
21	pub host: net::IpAddr,
22	/// Port de connexion du serveur.
23	pub port: u16,
24	/// Paramètres TLS.
25	pub tls: Option<SettingsTLS>,
26
27	#[serde(default)]
28	pub static_resources: Vec<SettingsStaticResource>,
29}
30
31#[derive(Debug)]
32#[derive(serde::Deserialize)]
33pub struct SettingsTLS {
34	/// Certificat TLS.
35	pub cert: path::PathBuf,
36	/// Clé TSL.
37	pub key: path::PathBuf,
38}
39
40#[derive(Debug)]
41#[derive(Clone)]
42#[derive(serde::Deserialize)]
43pub struct SettingsStaticResource {
44	/// Un chemin d'URL accessible aux utilisateurs.
45	pub url_path: String,
46	/// Répertoire des ressources statiques.
47	pub dir_path: path::PathBuf,
48}
49
50// -------------- //
51// Implémentation //
52// -------------- //
53
54impl Settings {
55	pub fn fetch_or_default(
56		config_dir: &impl AsRef<path::Path>,
57		file_extension: &crate::application::SettingsLoaderExtension,
58	) -> Self {
59		lexa_fs::load(config_dir, "server", file_extension).unwrap_or_else(
60			|err| {
61				let default_settings = Default::default();
62				log::error!(
63					"Attempt to retrieve server configuration file failed.
64					 File: `{}.{}`
65					 Error: `{}`",
66					config_dir.as_ref().join("server").display(),
67					&file_extension,
68					err
69				);
70				log::warn!(
71					"The default server configuration parameters returned \
72					 are: {:#?}",
73					&default_settings
74				);
75				default_settings
76			},
77		)
78	}
79}
80
81// -------------- //
82// Implémentation // -> Interface
83// -------------- //
84
85impl Default for Settings {
86	fn default() -> Self {
87		Self {
88			host: net::IpAddr::V4(net::Ipv4Addr::new(127, 0, 0, 1)),
89			port: 80,
90			tls: Default::default(),
91			static_resources: Default::default(),
92		}
93	}
94}