Skip to content

Commit

Permalink
Error handling & config file update
Browse files Browse the repository at this point in the history
  • Loading branch information
onihilist committed Jul 18, 2024
1 parent 5609611 commit 78798d0
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 22 deletions.
64 changes: 64 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ name = "RustyServer"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = { version = "1.0.145", features = ["derive"] }
toml = "0.8.14"
Expand All @@ -14,4 +12,6 @@ time = "0.3.36"
tokio = { version = "1", features = ["full"] }
tokio-postgres = "0.7"
async-std = "1.12.0"
futures = "0.3.30"
futures = "0.3.30"
hyper = "1.4.1"
serde_json = "1.0.120"
1 change: 1 addition & 0 deletions src/config/config.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[database]
typedb = "postgreSQL" # Only postgreSQL for the moment
host = "localhost"
port = 5432
username = "postgres"
Expand Down
9 changes: 7 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub(crate) struct configToml {

#[derive(Serialize, Deserialize, Debug)]
pub struct configTomlDatabase {
pub typedb: String,
pub host: String,
pub port: u16,
pub username: String,
Expand All @@ -23,6 +24,7 @@ pub struct configTomlDatabase {

#[derive(Debug)]
pub struct configData {
pub typedb: String,
pub host: String,
pub port: u16,
pub username: String,
Expand Down Expand Up @@ -71,10 +73,11 @@ impl configToml {
}
});

let (host, port, username, password, database):
(String, u16, String, String, String) = match configToml.database {
let (typedb, host, port, username, password, database):
(String, String, u16, String, String, String) = match configToml.database {
Some(database) => {
(
database.typedb,
database.host,
database.port,
database.username,
Expand All @@ -83,6 +86,7 @@ impl configToml {
)
}
None => (
"unknown".to_owned(),
"unknown".to_owned(),
0.to_owned(),
"unknown".to_owned(),
Expand All @@ -107,6 +111,7 @@ impl configToml {

configToml {
database: Some(configTomlDatabase {
typedb,
host,
port,
username,
Expand Down
56 changes: 39 additions & 17 deletions src/database/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,47 @@ pub async fn connectToDB() -> Result<tokio_postgres::Client, tokio_postgres::Err
db.port,
db.username.trim(),
db.password.trim(),
);
);

match tokio_postgres::connect(&connection_string, NoTls).await {
Ok((client, connection)) => {
tokio::spawn(async move {
if let Err(e) = connection.await {
let logs = utils::Logs::initLog(None, format!("Impossible to connect to the database : {}", e), None);
utils::Logs::error(logs);
}
});
let logs = utils::Logs::initLog(None, format!("Connected to the database ({}:{})", db.host, db.port), None);
utils::Logs::success(logs);
Ok(client)
if (db.typedb == "postgreSQL") {

match tokio_postgres::connect(&connection_string, NoTls).await {
Ok((client, connection)) => {
tokio::spawn(async move {
if let Err(e) = connection.await {
let logs = utils::Logs::initLog(None, format!("Impossible to connect to the database : {}", e), None);
utils::Logs::error(logs);
}
});
let logs = utils::Logs::initLog(None, format!("Connected to the database ({}:{})", db.host, db.port), None);
utils::Logs::success(logs);
Ok(client)
}
Err(e) => {
let error_message = format!("Impossible to connect to the database : {}", e);
let logs = utils::Logs::initLog(None, error_message, None);
utils::Logs::error(logs);
Err(e)
}
}
Err(e) => {
let error_message = format!("Impossible to connect to the database : {}", e);
let logs = utils::Logs::initLog(None, error_message, None);
utils::Logs::error(logs);
Err(e)
} else {
match tokio_postgres::connect("null", NoTls).await {
Ok((client, connection)) => {
tokio::spawn(async move {
if let Err(e) = connection.await {
let logs = utils::Logs::initLog(None, format!("Impossible to connect to the database : {}", e), None);
utils::Logs::error(logs);
}
});
let logs = utils::Logs::initLog(None, format!("Connected to the database ({}:{})", db.host, db.port), None);
utils::Logs::success(logs);
Ok(client)
}
Err(e) => {
let logs = utils::Logs::initLog(None, format!("Only postgreSQL for the moment, but later MySQL, MariaDB, OracleDB, MongoDB : {}", e), None);
utils::Logs::warning(logs);
Err(e)
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ mod server;
mod database;
mod config;

use std::env;

#[tokio::main]
async fn main() {
env::set_var("RUST_BACKTRACE", "0");
server::startServer().await.unwrap();
}
5 changes: 5 additions & 0 deletions src/utils/exception.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

enum Error{
DatabaseTypeError(tokio_postgres::Error),

}
3 changes: 3 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
mod exception;

use colored::Colorize;

pub mod Logs {
use std::fmt;
use std::fmt::DebugStruct;
use std::task::Context;
use colored::Colorize;
Expand Down

0 comments on commit 78798d0

Please sign in to comment.