From 7f7542aac5fc40fba8743a0126efd85d6c9be158 Mon Sep 17 00:00:00 2001 From: xenua Date: Wed, 4 Jan 2023 14:01:00 +0100 Subject: [PATCH] =?UTF-8?q?configurable=20file=20structure=20=E2=9C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/data.rs | 31 ++++++++++++++++++------------- src/main.rs | 8 +------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/data.rs b/src/data.rs index c928cda..61fe1b5 100644 --- a/src/data.rs +++ b/src/data.rs @@ -9,12 +9,16 @@ use std::{ use std::os::unix::fs::DirBuilderExt; use dashmap::DashMap; - -const TEXT_DIR: String = "text".to_string(); -const FILE_DIR: String = "files".to_string(); -const REDIRECTS_FILE: String = "redirects".to_string(); -const STATS_FILE: String = "stats".to_string(); -const USERS_FILE: String = "users".to_string(); +use lazy_static::lazy_static; + +lazy_static! { + static ref TEXT_DIR: String = std::env::var("V_TEXT_DIR").unwrap_or("text".to_string()); + static ref FILE_DIR: String = std::env::var("V_FILE_DIR").unwrap_or("files".to_string()); + static ref REDIRECTS_FILE: String = + std::env::var("V_REDIRECTS_FILE").unwrap_or("redirects".to_string()); + static ref STATS_FILE: String = std::env::var("V_STATS_FILE").unwrap_or("stats".to_string()); + static ref USERS_FILE: String = std::env::var("V_USERS_FILE").unwrap_or("users".to_string()); +} #[derive(thiserror::Error, Debug)] pub enum Error { @@ -79,6 +83,7 @@ struct UserData { pub pw_hash: String, pub is_admin: bool, } + impl UserData { pub fn parse(line: &str, is_admin: bool) -> Result { let (username, pw_hash) = line.split_once(": ").ok_or(Error::Metadata)?; @@ -130,8 +135,8 @@ impl DataStorage { } dir_builder.create(self.base_dir)?; - dir_builder.create(self.base_dir.join(FILE_DIR))?; - dir_builder.create(self.base_dir.join(TEXT_DIR))?; + dir_builder.create(self.base_dir.join(*FILE_DIR))?; + dir_builder.create(self.base_dir.join(*TEXT_DIR))?; Ok(()) } @@ -140,7 +145,7 @@ impl DataStorage { let f = File::options() .create(true) .read(true) - .open(self.base_dir.join(REDIRECTS_FILE))?; + .open(self.base_dir.join(*REDIRECTS_FILE))?; // TODO: replace with meta_str_to_map and then self.redirects.extend @@ -156,7 +161,7 @@ impl DataStorage { } fn load_text(&mut self) -> Result<(), Error> { - for thing in std::fs::read_dir(self.base_dir.join(TEXT_DIR)).expect("fs error") { + for thing in std::fs::read_dir(self.base_dir.join(*TEXT_DIR)).expect("fs error") { let dir_entry = thing?; // i'm so good at naming things let id = dir_entry.file_name().into_string().or(Err(Error::FS))?; let (raw_metadata, text) = read_to_string(dir_entry.path())? @@ -175,7 +180,7 @@ impl DataStorage { } fn load_files(&mut self) -> Result<(), Error> { - for thing in std::fs::read_dir(self.base_dir.join(FILE_DIR)).expect("fs error") { + for thing in std::fs::read_dir(self.base_dir.join(*FILE_DIR)).expect("fs error") { let dir_entry = thing?; // i'm so good at naming things let filename = dir_entry .file_name() @@ -193,7 +198,7 @@ impl DataStorage { } fn assert_all_files_present(&self) -> Result<(), Error> { - let files_folder = self.base_dir.join(FILE_DIR); + let files_folder = self.base_dir.join(*FILE_DIR); for (id, _) in self.files.into_iter() { files_folder .join(id) @@ -205,7 +210,7 @@ impl DataStorage { fn load_users(&mut self) -> Result<(), Error> { let is_admin = true; - for line in read_to_string(self.base_dir.join(USERS_FILE))?.lines() { + for line in read_to_string(self.base_dir.join(*USERS_FILE))?.lines() { if line.starts_with("---") { is_admin = false; break; diff --git a/src/main.rs b/src/main.rs index 7877b8e..c423cb4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,16 +5,10 @@ mod up; use std::net::SocketAddr; use axum::{routing::get, Router}; - -use data::DataStorage; use lazy_static::lazy_static; use tera::Tera; -const TEXT_DIR: String = "text".to_string(); -const FILE_DIR: String = "files".to_string(); -const REDIRECTS_FILE: String = "redirects".to_string(); -const STATS_FILE: String = "stats".to_string(); -const USERS_FILE: String = "users".to_string(); +use data::DataStorage; lazy_static! { pub static ref TEMPLATES: Tera = {