From 85dc0418b8b3b7e6d10a4ae85878a921b67c9baf Mon Sep 17 00:00:00 2001 From: necrashter Date: Sat, 23 Apr 2022 15:37:37 +0300 Subject: Save different networks to different folders --- src/db.rs | 22 ++++++++++++---------- src/handlers.rs | 7 +++++-- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/db.rs b/src/db.rs index 98aa984..ad4724c 100644 --- a/src/db.rs +++ b/src/db.rs @@ -28,14 +28,14 @@ pub struct Db { impl Db { pub fn new(config: Config) -> Self { - fs::create_dir_all("blocks").unwrap(); - fs::create_dir_all("users").unwrap(); + fs::create_dir_all(format!("blocks/{}", config.name)).unwrap(); + fs::create_dir_all(format!("users/{}", config.name)).unwrap(); let mut db = Db::default(); - if let Some(block_path) = last_block_content() { + if let Some(block_path) = last_block_content(&config.name) { db.populate_with_last_block(block_path); } - if let Ok(users_path) = read_users() { + if let Ok(users_path) = read_users(&config.name) { db.populate_with_users(users_path); } @@ -85,8 +85,8 @@ impl Db { } } -fn last_block_content() -> Option { - let blocks = read_block_name().unwrap(); +fn last_block_content(config_name: &str) -> Option { + let blocks = read_block_name(config_name).unwrap(); if blocks.is_empty() { return None; @@ -107,8 +107,9 @@ fn last_block_content() -> Option { return Some(blocks[last_block_index].to_str().unwrap().parse().unwrap()); } -fn read_block_name() -> io::Result> { - let entries = fs::read_dir("./blocks")? +fn read_block_name(config_name: &str) -> io::Result> { + let path = format!("./blocks/{}", config_name); + let entries = fs::read_dir(path)? .map(|res| res.map(|e| e.path())) .collect::, io::Error>>()?; @@ -122,8 +123,9 @@ fn parse_block(path: &str) -> u64 { block_u64 } -fn read_users() -> io::Result> { - let entries = fs::read_dir("./users")? +fn read_users(config_name: &str) -> io::Result> { + let path = format!("./users/{}", config_name); + let entries = fs::read_dir(path)? .map(|res| res.map(|e| e.path())) .collect::, io::Error>>()?; diff --git a/src/handlers.rs b/src/handlers.rs index f80aa60..d2a834f 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -333,7 +333,10 @@ pub async fn authenticate_user( }) .unwrap(); - fs::write(format!("users/{}.guy", new_user.user_id), user_at_rest_json).unwrap(); + fs::write( + format!("users/{}/{}.guy", db.config.name, new_user.user_id), + user_at_rest_json + ).unwrap(); let mut userlist = db.users.write(); userlist.insert(fingerprint.clone(), new_user); @@ -601,7 +604,7 @@ pub async fn propose_block( let block_json = serde_json::to_string(&new_block).unwrap(); fs::write( - format!("blocks/{}.block", new_block.timestamp.timestamp()), + format!("blocks/{}/{}.block", db.config.name, new_block.timestamp.timestamp()), block_json, ) .unwrap(); -- cgit v1.2.3-70-g09d2