Browse Source

configurable file structure

xenua 2 years ago
parent
commit
7f7542aac5
  1. 31
      src/data.rs
  2. 8
      src/main.rs

31
src/data.rs

@ -9,12 +9,16 @@ use std::{
use std::os::unix::fs::DirBuilderExt; use std::os::unix::fs::DirBuilderExt;
use dashmap::DashMap; use dashmap::DashMap;
use lazy_static::lazy_static;
const TEXT_DIR: String = "text".to_string();
const FILE_DIR: String = "files".to_string(); lazy_static! {
const REDIRECTS_FILE: String = "redirects".to_string(); static ref TEXT_DIR: String = std::env::var("V_TEXT_DIR").unwrap_or("text".to_string());
const STATS_FILE: String = "stats".to_string(); static ref FILE_DIR: String = std::env::var("V_FILE_DIR").unwrap_or("files".to_string());
const USERS_FILE: String = "users".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)] #[derive(thiserror::Error, Debug)]
pub enum Error { pub enum Error {
@ -79,6 +83,7 @@ struct UserData {
pub pw_hash: String, pub pw_hash: String,
pub is_admin: bool, pub is_admin: bool,
} }
impl UserData { impl UserData {
pub fn parse(line: &str, is_admin: bool) -> Result<Self, Error> { pub fn parse(line: &str, is_admin: bool) -> Result<Self, Error> {
let (username, pw_hash) = line.split_once(": ").ok_or(Error::Metadata)?; 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)?;
dir_builder.create(self.base_dir.join(FILE_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(*TEXT_DIR))?;
Ok(()) Ok(())
} }
@ -140,7 +145,7 @@ impl DataStorage {
let f = File::options() let f = File::options()
.create(true) .create(true)
.read(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 // 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> { 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 dir_entry = thing?; // i'm so good at naming things
let id = dir_entry.file_name().into_string().or(Err(Error::FS))?; let id = dir_entry.file_name().into_string().or(Err(Error::FS))?;
let (raw_metadata, text) = read_to_string(dir_entry.path())? let (raw_metadata, text) = read_to_string(dir_entry.path())?
@ -175,7 +180,7 @@ impl DataStorage {
} }
fn load_files(&mut self) -> Result<(), Error> { 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 dir_entry = thing?; // i'm so good at naming things
let filename = dir_entry let filename = dir_entry
.file_name() .file_name()
@ -193,7 +198,7 @@ impl DataStorage {
} }
fn assert_all_files_present(&self) -> Result<(), Error> { 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() { for (id, _) in self.files.into_iter() {
files_folder files_folder
.join(id) .join(id)
@ -205,7 +210,7 @@ impl DataStorage {
fn load_users(&mut self) -> Result<(), Error> { fn load_users(&mut self) -> Result<(), Error> {
let is_admin = true; 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("---") { if line.starts_with("---") {
is_admin = false; is_admin = false;
break; break;

8
src/main.rs

@ -5,16 +5,10 @@ mod up;
use std::net::SocketAddr; use std::net::SocketAddr;
use axum::{routing::get, Router}; use axum::{routing::get, Router};
use data::DataStorage;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use tera::Tera; use tera::Tera;
const TEXT_DIR: String = "text".to_string(); use data::DataStorage;
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();
lazy_static! { lazy_static! {
pub static ref TEMPLATES: Tera = { pub static ref TEMPLATES: Tera = {

Loading…
Cancel
Save