From 0d97aa82f867d9cf2d2a9d6acb199cbd35c4c422 Mon Sep 17 00:00:00 2001 From: onihilist Date: Thu, 11 Jul 2024 16:33:29 +0200 Subject: [PATCH] Enhanced config file --- src/config/config.toml | 6 +++- src/config/mod.rs | 67 ++++++++++++++++++++++++++++++---------- src/database/database.rs | 18 ++++++----- src/todo.txt | 1 + 4 files changed, 67 insertions(+), 25 deletions(-) diff --git a/src/config/config.toml b/src/config/config.toml index 2a9848e..1c0b0ef 100644 --- a/src/config/config.toml +++ b/src/config/config.toml @@ -3,4 +3,8 @@ host = "localhost" port = 5432 username = "postgres" password = "root" -database = "RustyDB" \ No newline at end of file +database = "RustyDB" + +[server] +maxUser = 30 +timeout = 10000 #(ms) diff --git a/src/config/mod.rs b/src/config/mod.rs index 8fd6e7d..2c8c16c 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -7,17 +7,18 @@ use std::env; use crate::utils::Logs::UtilsData; #[derive(Serialize, Deserialize, Debug)] -struct configToml { - database: Option +pub(crate) struct configToml { + pub database: Option, + pub server: Option } #[derive(Serialize, Deserialize, Debug)] -struct configTomlDatabase { - host: String, - port: u16, - username: String, - password: String, - database: String +pub struct configTomlDatabase { + pub host: String, + pub port: u16, + pub username: String, + pub password: String, + pub database: String } #[derive(Debug)] @@ -29,7 +30,20 @@ pub struct configData { pub database: String } -impl configData { +#[derive(Serialize, Deserialize, Debug)] +pub struct configTomlServer { + pub maxUser: u16, + pub timeout: u32 +} + +#[derive(Debug)] +pub struct configServer { + pub maxUser: u16, + pub timeout: u32 +} + + +impl configToml { pub fn new() -> Self { let mut fp: String = "".to_owned(); @@ -52,7 +66,8 @@ impl configData { let logs: UtilsData = utils::Logs::initLog(None, format!("Config file was found : {}", fp), None); utils::Logs::info(logs); configToml { - database: None + database: None, + server: None, } }); @@ -76,12 +91,32 @@ impl configData { ) }; - configData { - host, - port, - username, - password, - database, + let (maxUser, timeout): + (u16, u32) = match configToml.server { + Some(server) => { + ( + server.maxUser, + server.timeout + ) + } + None => ( + 10.to_owned(), + 10000.to_owned(), + ) + }; + + configToml { + database: Some(configTomlDatabase { + host, + port, + username, + password, + database, + }), + server: Some(configTomlServer{ + maxUser, + timeout + }), } } } \ No newline at end of file diff --git a/src/database/database.rs b/src/database/database.rs index 129f017..acc7b53 100644 --- a/src/database/database.rs +++ b/src/database/database.rs @@ -1,19 +1,21 @@ use std::panic::resume_unwind; -use crate::config::configData; +use crate::config::{configToml, configTomlDatabase}; use tokio_postgres::{NoTls, Row}; use crate::server::users::userData; use crate::utils; +const CONFIG: configToml = configToml::new(); + pub async fn connectToDB() -> Result { - let config: configData = configData::new(); + let db: configTomlDatabase = CONFIG.database.unwrap(); let connection_string = format!("host={} port={} user={} password={}", // dbname={}", - config.host.trim(), - config.port, - config.username.trim(), - config.password.trim()/*, - config.database.trim()*/); + db.host, + db.port, + db.username.trim(), + db.password.trim(), + ); match tokio_postgres::connect(&connection_string, NoTls).await { Ok((client, connection)) => { @@ -23,7 +25,7 @@ pub async fn connectToDB() -> Result