feat: ✨ read configs
This commit is contained in:
parent
00e488a391
commit
fdb9609514
7 changed files with 167 additions and 1 deletions
45
src/database.rs
Normal file
45
src/database.rs
Normal 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
90
src/lib.rs
Normal 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
4
src/main.rs
Normal file
|
@ -0,0 +1,4 @@
|
|||
#[tokio::main]
|
||||
async fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue