switch to rocket::launch

refactor cache setup function
remove lazy_static dep, replace with OnceLock
This commit is contained in:
2024-11-30 22:08:47 -05:00
parent 69e8d97e3b
commit 537abe32c1
5 changed files with 82 additions and 77 deletions

3
Cargo.lock generated
View File

@@ -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",

View File

@@ -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"

View File

@@ -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),
}

View File

@@ -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<FileCache> = 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<PathBuf> = 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
}

View File

@@ -41,7 +41,7 @@ impl<'r> Responder<'r, 'static> for CachedFile {
#[get("/download/<file_hash>")]
pub async fn download_file(file_hash: String) -> Option<CachedFile> {
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<UploadFile<'_>>) -> Result<String, Strin
let filepath = PathBuf::from(format!(
"{}{}.{}",
&crate::CACHE_PATH.as_path().display(),
&crate::cache_path().as_path().display(),
&filename,
extension
));