switch to rocket::launch
refactor cache setup function remove lazy_static dep, replace with OnceLock
This commit is contained in:
3
Cargo.lock
generated
3
Cargo.lock
generated
@@ -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",
|
||||
|
||||
@@ -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"
|
||||
@@ -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),
|
||||
}
|
||||
|
||||
79
src/main.rs
79
src/main.rs
@@ -41,60 +41,50 @@ 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 = {
|
||||
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)
|
||||
};
|
||||
})
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref CACHE_PATH: PathBuf = {
|
||||
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
|
||||
};
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
#[rocket::main]
|
||||
async fn main() -> Result<(), rocket::Error> {
|
||||
let _rocket = rocket::build()
|
||||
.mount(
|
||||
"/",
|
||||
rocket::routes![
|
||||
routes::index,
|
||||
routes::download_file,
|
||||
routes::query_file,
|
||||
routes::upload_file
|
||||
],
|
||||
)
|
||||
.attach(AdHoc::on_liftoff("Application setup", |_| {
|
||||
Box::pin(async move {
|
||||
lazy_static::initialize(&CACHE_PATH);
|
||||
lazy_static::initialize(&FILECACHE);
|
||||
|
||||
fn cache_setup() -> () {
|
||||
let keep_file = {
|
||||
let mut marker_path = PathBuf::from(&CACHE_PATH.as_path());
|
||||
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()) {
|
||||
|
||||
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()
|
||||
cache_path().as_path().to_string_lossy()
|
||||
),
|
||||
Err(e) => {
|
||||
log::error!("Error creating cache directory - {}", &e);
|
||||
@@ -107,7 +97,7 @@ async fn main() -> Result<(), rocket::Error> {
|
||||
match fs::File::create(&keep_file) {
|
||||
Ok(_) => log::info!(
|
||||
"crated new enviroment file @ {}",
|
||||
&CACHE_PATH.as_path().to_string_lossy()
|
||||
cache_path().as_path().to_string_lossy()
|
||||
),
|
||||
Err(e) => {
|
||||
log::error!("Error creating enviroment file - {}", &e);
|
||||
@@ -116,7 +106,7 @@ async fn main() -> Result<(), rocket::Error> {
|
||||
}
|
||||
}
|
||||
|
||||
match FILECACHE.add(&keep_file) {
|
||||
match filecache().add(&keep_file) {
|
||||
Ok(_) => {
|
||||
log::info!("added {} to cache", &keep_file.as_path().to_string_lossy())
|
||||
}
|
||||
@@ -126,10 +116,27 @@ async fn main() -> Result<(), rocket::Error> {
|
||||
CacheEntryError::FileExists => todo!(),
|
||||
},
|
||||
}
|
||||
})
|
||||
}))
|
||||
.ignite().await?
|
||||
.launch().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
()
|
||||
}
|
||||
|
||||
#[launch]
|
||||
async fn rfile_rocket() -> _ {
|
||||
let rocket = rocket::build()
|
||||
.mount(
|
||||
"/",
|
||||
rocket::routes![
|
||||
routes::index,
|
||||
routes::download_file,
|
||||
routes::query_file,
|
||||
routes::upload_file
|
||||
],
|
||||
)
|
||||
.attach(
|
||||
AdHoc::on_liftoff("Application setup", |_| Box::pin(async move {
|
||||
cache_setup();
|
||||
}))
|
||||
);
|
||||
|
||||
rocket
|
||||
}
|
||||
@@ -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
|
||||
));
|
||||
|
||||
Reference in New Issue
Block a user