feat: read configs

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-11-26 22:30:25 +03:00
parent 00e488a391
commit fdb9609514
7 changed files with 167 additions and 1 deletions

6
.gitignore vendored
View file

@ -1,3 +1,5 @@
.vscode/
# Generated by Cargo # Generated by Cargo
# will have compiled files and executables # will have compiled files and executables
debug/ debug/
@ -19,3 +21,7 @@ Cargo.lock
# and can be added to the global gitignore or merged into this file. For a more nuclear # 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. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/ #.idea/
# Added by cargo
/target

13
Cargo.toml Normal file
View file

@ -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"] }

View file

@ -0,0 +1,6 @@
[database_config]
address = "localhost:8000"
username = "root"
password = "root"
namespace = "rust_forum"
database = "rust_forum"

View file

@ -0,0 +1,2 @@
[server_config]
address = "localhost:2344"

45
src/database.rs Normal file
View file

@ -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<Surreal<Client>> = LazyLock::new(Surreal::init);
pub async fn establish_connection() {
let database_config = DatabaseConfig::default();
DB.connect::<Ws>(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() {}

90
src/lib.rs Normal file
View file

@ -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::<Vec<&str>>();
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::<Vec<&str>>();
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")
}
}
}

4
src/main.rs Normal file
View file

@ -0,0 +1,4 @@
#[tokio::main]
async fn main() {
println!("Hello, world!");
}