diff --git a/.gitignore b/.gitignore index d01bd1a..04a5683 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +.vscode/ + # Generated by Cargo # will have compiled files and executables debug/ @@ -18,4 +20,8 @@ Cargo.lock # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ \ No newline at end of file +#.idea/ + +# Added by cargo + +/target \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..e043016 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "rust_forum" +version = "0.1.0" +edition = "2021" + +[dependencies] +axum = "0.7.9" +chrono = { version = "0.4.38", features = ["serde"] } +serde = { version = "1.0.215", features = ["derive"] } +serde_json = "1.0.133" +surrealdb = "2.1.1" +tokio = { version = "1.41.1", features = ["full"] } +tower-http = { version = "0.6.2", features = ["cors"] } diff --git a/configs/database_config.toml b/configs/database_config.toml new file mode 100644 index 0000000..4bb483e --- /dev/null +++ b/configs/database_config.toml @@ -0,0 +1,6 @@ +[database_config] +address = "localhost:8000" +username = "root" +password = "root" +namespace = "rust_forum" +database = "rust_forum" \ No newline at end of file diff --git a/configs/server_config.toml b/configs/server_config.toml new file mode 100644 index 0000000..bf0431a --- /dev/null +++ b/configs/server_config.toml @@ -0,0 +1,2 @@ +[server_config] +address = "localhost:2344" \ No newline at end of file diff --git a/src/database.rs b/src/database.rs new file mode 100644 index 0000000..cd5108b --- /dev/null +++ b/src/database.rs @@ -0,0 +1,45 @@ +use std::{sync::LazyLock, time::Duration}; + +use surrealdb::{ + engine::remote::ws::{Client, Ws}, + opt::auth::Root, + Surreal, +}; +use tokio::time::sleep; + +use crate::DatabaseConfig; + +static DB: LazyLock> = LazyLock::new(Surreal::init); + +pub async fn establish_connection() { + let database_config = DatabaseConfig::default(); + DB.connect::(database_config.address).await.unwrap(); + DB.signin(Root { + username: &database_config.username, + password: &database_config.password, + }) + .await + .unwrap(); + DB.use_ns(database_config.namespace).await.unwrap(); + DB.use_db(database_config.database).await.unwrap(); +} + +pub async fn is_alive() -> bool { + tokio::select! { + alive_status = DB.health() => { + match alive_status { + Ok(_) => true, + Err(_) => false, + } + }, + _ = sleep(Duration::from_secs(1)) => false + } +} + +pub async fn create_post() {} + +pub async fn read_post() {} + +pub async fn update_post() {} + +pub async fn delete_post() {} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..23eee63 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,90 @@ +use std::{fs::File, io::Read}; + +pub mod database; + +pub struct DatabaseConfig { + pub address: String, + pub username: String, + pub password: String, + pub namespace: String, + pub database: String, +} +impl Default for DatabaseConfig { + fn default() -> Self { + let mut database_config_file = File::open("./configs/database_config.toml").unwrap(); + let mut database_configs = String::default(); + database_config_file + .read_to_string(&mut database_configs) + .unwrap(); + let database_configs = database_configs + .lines() + .map(|line| line.trim_end()) + .collect::>(); + if database_configs[0] == "[database_config]" { + Self { + address: database_configs[1] + .split_once('=') + .unwrap() + .1 + .trim() + .to_string(), + username: database_configs[2] + .split_once('=') + .unwrap() + .1 + .trim() + .to_string(), + password: database_configs[3] + .split_once('=') + .unwrap() + .1 + .trim() + .to_string(), + namespace: database_configs[4] + .split_once('=') + .unwrap() + .1 + .trim() + .to_string(), + database: database_configs[5] + .split_once('=') + .unwrap() + .1 + .trim() + .to_string(), + } + } else { + panic!("Database Config File Must Include [database_config] at the First Line") + } + } +} + +pub struct ServerConfig { + pub address: String, +} + +impl Default for ServerConfig { + fn default() -> Self { + let mut server_config_file = File::open("./configs/server_config.toml").unwrap(); + let mut server_configs = String::default(); + server_config_file + .read_to_string(&mut server_configs) + .unwrap(); + let server_configs = server_configs + .lines() + .map(|line| line.trim_end()) + .collect::>(); + if server_configs[0] == "[server_config]" { + Self { + address: server_configs[1] + .split_once('=') + .unwrap() + .1 + .trim() + .to_string(), + } + } else { + panic!("Server Config File Must Include [server_config] at the First Line") + } + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..7e3d561 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,4 @@ +#[tokio::main] +async fn main() { + println!("Hello, world!"); +}