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

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() {}