diff --git a/Cargo.lock b/Cargo.lock index b4d1aa2..5f7c97e 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -1333,7 +1333,6 @@ dependencies = [ "data-encoding", "flate2", "futures", - "lazy_static", "log", "memmap2", "mime", diff --git a/Cargo.toml b/Cargo.toml index 51e4172..1514b5b 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,6 @@ tar = "~0.4" # and some stuff I don't know why it isn't in std rand = { version = "~0.8", features = ["std", "alloc", "getrandom", "std_rng", "log"] } -lazy_static = "~1.4" regex = "~1.11" chrono = "~0.4" memmap2 = "~0.9" \ No newline at end of file diff --git a/src/filecache/mod.rs b/src/filecache/mod.rs index b2fa4ff..fe91d95 100755 --- a/src/filecache/mod.rs +++ b/src/filecache/mod.rs @@ -244,13 +244,13 @@ impl FileCache { // do nothing, this is sent when a file is being removed } DebouncedEvent::Create(p) => { - match crate::FILECACHE.add(p.as_path()) { + match crate::filecache().add(p.as_path()) { Ok(hash) => log::info!("[{}] Found new file @ {} ({})",thread_name, &p.as_path().to_string_lossy(), hash.to_string()), Err(e) => log::error!("[{}] Found a new file but there was an error adding to internal cache\nFile -> {} \nError -> {:#?}", thread_name, &p.as_path().to_string_lossy(), &e), } } DebouncedEvent::Write(p) => { - match crate::FILECACHE.add(p.as_path()) { + match crate::filecache().add(p.as_path()) { Ok(hash) => log::info!("[{}] A file was updated @ {} ({})",thread_name, &p.as_path().to_string_lossy(), hash.to_string()), Err(e) => log::error!("[{}] Found a new file but there was an error updating the internal cache\nFile -> {} \nError -> {:#?}", thread_name, &p.as_path().to_string_lossy(), &e), } diff --git a/src/main.rs b/src/main.rs index 926988f..0003e9a 100755 --- a/src/main.rs +++ b/src/main.rs @@ -41,34 +41,88 @@ mod filecache; mod routes; use std::{fs, path::PathBuf}; +use std::sync::OnceLock; use filecache::*; -use lazy_static::lazy_static; use rocket::fairing::AdHoc; static CACHE_DIR: &str = "CACHE/"; -lazy_static! { - static ref FILECACHE: FileCache = { - let mut cwd = std::env::current_dir().unwrap(); - cwd.push(CACHE_DIR); +fn filecache() -> &'static FileCache { + static FILECACHE: OnceLock = OnceLock::new(); + FILECACHE.get_or_init( + || { + let mut cwd = std::env::current_dir().unwrap(); + cwd.push(CACHE_DIR); - FileCache::new(&cwd) - }; + FileCache::new(&cwd) + }) } -lazy_static! { - static ref CACHE_PATH: PathBuf = { - let mut cwd = std::env::current_dir().unwrap(); - cwd.push(&CACHE_DIR); +fn cache_path() -> &'static PathBuf { + static CACHE_PATH: OnceLock = OnceLock::new(); + CACHE_PATH.get_or_init( + || { + let mut cwd = std::env::current_dir().unwrap(); + cwd.push(&CACHE_DIR); - cwd - }; + cwd + } + ) } -#[rocket::main] -async fn main() -> Result<(), rocket::Error> { - let _rocket = rocket::build() +fn cache_setup() -> () { + let keep_file = { + let mut marker_path = PathBuf::from(cache_path().as_path()); + marker_path.push("rfile.meta"); + + marker_path + }; + + + if !(cache_path().is_dir()) { + match fs::create_dir(cache_path().as_path()) { + Ok(_) => log::info!( + "crated new cache directory @ {}", + cache_path().as_path().to_string_lossy() + ), + Err(e) => { + log::error!("Error creating cache directory - {}", &e); + panic!("{}", &e); + } + } + } + + if !keep_file.exists() { + match fs::File::create(&keep_file) { + Ok(_) => log::info!( + "crated new enviroment file @ {}", + cache_path().as_path().to_string_lossy() + ), + Err(e) => { + log::error!("Error creating enviroment file - {}", &e); + panic!("{}", &e); + } + } + } + + match filecache().add(&keep_file) { + Ok(_) => { + log::info!("added {} to cache", &keep_file.as_path().to_string_lossy()) + } + Err(e) => match e { + CacheEntryError::NotFound => todo!(), + CacheEntryError::FileLocked => todo!(), + CacheEntryError::FileExists => todo!(), + }, + } + + () +} + +#[launch] +async fn rfile_rocket() -> _ { + let rocket = rocket::build() .mount( "/", rocket::routes![ @@ -78,58 +132,11 @@ async fn main() -> Result<(), rocket::Error> { routes::upload_file ], ) - .attach(AdHoc::on_liftoff("Application setup", |_| { - Box::pin(async move { - lazy_static::initialize(&CACHE_PATH); - lazy_static::initialize(&FILECACHE); + .attach( + AdHoc::on_liftoff("Application setup", |_| Box::pin(async move { + cache_setup(); + })) + ); - let keep_file = { - let mut marker_path = PathBuf::from(&CACHE_PATH.as_path()); - marker_path.push("rfile.meta"); - - marker_path - }; - - if !(&CACHE_PATH.is_dir()) { - match fs::create_dir(&CACHE_PATH.as_path()) { - Ok(_) => log::info!( - "crated new cache directory @ {}", - &CACHE_PATH.as_path().to_string_lossy() - ), - Err(e) => { - log::error!("Error creating cache directory - {}", &e); - panic!("{}", &e); - } - } - } - - if !keep_file.exists() { - match fs::File::create(&keep_file) { - Ok(_) => log::info!( - "crated new enviroment file @ {}", - &CACHE_PATH.as_path().to_string_lossy() - ), - Err(e) => { - log::error!("Error creating enviroment file - {}", &e); - panic!("{}", &e); - } - } - } - - match FILECACHE.add(&keep_file) { - Ok(_) => { - log::info!("added {} to cache", &keep_file.as_path().to_string_lossy()) - } - Err(e) => match e { - CacheEntryError::NotFound => todo!(), - CacheEntryError::FileLocked => todo!(), - CacheEntryError::FileExists => todo!(), - }, - } - }) - })) - .ignite().await? - .launch().await?; - - Ok(()) - } + rocket +} \ No newline at end of file diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 570366a..e29ff35 100755 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -41,7 +41,7 @@ impl<'r> Responder<'r, 'static> for CachedFile { #[get("/download/")] pub async fn download_file(file_hash: String) -> Option { - let file = match crate::FILECACHE.get(file_hash) { + let file = match crate::filecache().get(file_hash) { Ok(fe) => fe, Err(_) => { panic!() @@ -137,7 +137,7 @@ pub async fn upload_file(mut form: Form>) -> Result