diff --git a/Cargo.lock b/Cargo.lock index 6b14019..a97078d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,7 +10,9 @@ dependencies = [ "chrono", "colored", "futures", + "hyper", "serde", + "serde_json", "time", "tokio", "tokio-postgres", @@ -445,6 +447,12 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "futures" version = "0.3.30" @@ -628,6 +636,39 @@ dependencies = [ "digest", ] +[[package]] +name = "http" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http-body" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "hyper" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50dfd22e0e76d0f662d429a5f80fcaf3855009297eab6a0a9f8543834744ba05" +dependencies = [ + "bytes", + "http", + "http-body", + "tokio", +] + [[package]] name = "iana-time-zone" version = "0.1.60" @@ -681,6 +722,12 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + [[package]] name = "js-sys" version = "0.3.69" @@ -1065,6 +1112,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + [[package]] name = "scopeguard" version = "1.2.0" @@ -1091,6 +1144,17 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_json" +version = "1.0.120" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "serde_spanned" version = "0.6.6" diff --git a/Cargo.toml b/Cargo.toml index c2be76f..484ad8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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" \ No newline at end of file +futures = "0.3.30" +hyper = "1.4.1" +serde_json = "1.0.120" \ No newline at end of file diff --git a/src/config/config.toml b/src/config/config.toml index 1c0b0ef..c645b1f 100644 --- a/src/config/config.toml +++ b/src/config/config.toml @@ -1,4 +1,5 @@ [database] +typedb = "postgreSQL" # Only postgreSQL for the moment host = "localhost" port = 5432 username = "postgres" diff --git a/src/config/mod.rs b/src/config/mod.rs index 2c8c16c..313db6a 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -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, @@ -23,6 +24,7 @@ pub struct configTomlDatabase { #[derive(Debug)] pub struct configData { + pub typedb: String, pub host: String, pub port: u16, pub username: String, @@ -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, @@ -83,6 +86,7 @@ impl configToml { ) } None => ( + "unknown".to_owned(), "unknown".to_owned(), 0.to_owned(), "unknown".to_owned(), @@ -107,6 +111,7 @@ impl configToml { configToml { database: Some(configTomlDatabase { + typedb, host, port, username, diff --git a/src/database/database.rs b/src/database/database.rs index 0972162..494ed6f 100644 --- a/src/database/database.rs +++ b/src/database/database.rs @@ -15,25 +15,47 @@ pub async fn connectToDB() -> Result { - 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) + } } } } diff --git a/src/main.rs b/src/main.rs index a84b014..2cd04e0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(); } \ No newline at end of file diff --git a/src/utils/exception.rs b/src/utils/exception.rs new file mode 100644 index 0000000..5a18fd0 --- /dev/null +++ b/src/utils/exception.rs @@ -0,0 +1,5 @@ + +enum Error{ + DatabaseTypeError(tokio_postgres::Error), + +} \ No newline at end of file diff --git a/src/utils/mod.rs b/src/utils/mod.rs index d2eed6b..eda720b 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -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;