Skip to content

Commit

Permalink
Enhanced config file
Browse files Browse the repository at this point in the history
  • Loading branch information
onihilist committed Jul 11, 2024
1 parent fc897cd commit 0d97aa8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 25 deletions.
6 changes: 5 additions & 1 deletion src/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ host = "localhost"
port = 5432
username = "postgres"
password = "root"
database = "RustyDB"
database = "RustyDB"

[server]
maxUser = 30
timeout = 10000 #(ms)
67 changes: 51 additions & 16 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ use std::env;
use crate::utils::Logs::UtilsData;

#[derive(Serialize, Deserialize, Debug)]
struct configToml {
database: Option<configTomlDatabase>
pub(crate) struct configToml {
pub database: Option<configTomlDatabase>,
pub server: Option<configTomlServer>
}

#[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)]
Expand All @@ -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();
Expand All @@ -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,
}
});

Expand All @@ -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
}),
}
}
}
18 changes: 10 additions & 8 deletions src/database/database.rs
Original file line number Diff line number Diff line change
@@ -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<tokio_postgres::Client, tokio_postgres::Error> {

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)) => {
Expand All @@ -23,7 +25,7 @@ pub async fn connectToDB() -> Result<tokio_postgres::Client, tokio_postgres::Err
utils::Logs::error(logs);
}
});
let logs = utils::Logs::initLog(None, format!("Connected to the database ({}:{})", config.host.trim(), config.port), None);
let logs = utils::Logs::initLog(None, format!("Connected to the database ({}:{})", db.host, db.port), None);
utils::Logs::success(logs);
Ok(client)
}
Expand Down
1 change: 1 addition & 0 deletions src/todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
========== TO DO ==========
2. FAIRE LE SENDER POUR ENVOYER LES MERDES DANS CHECKPROTOCOL !!!!
3. Fix le problème du nom du sender quand on receive un message
4. Use les variables server du fichier de config

========== IN PROGRESS ==========

Expand Down

0 comments on commit 0d97aa8

Please sign in to comment.