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.
|
# This file is automatically @generated by Cargo.
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 3
|
version = 4
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "addr2line"
|
name = "addr2line"
|
||||||
@@ -1333,7 +1333,6 @@ dependencies = [
|
|||||||
"data-encoding",
|
"data-encoding",
|
||||||
"flate2",
|
"flate2",
|
||||||
"futures",
|
"futures",
|
||||||
"lazy_static",
|
|
||||||
"log",
|
"log",
|
||||||
"memmap2",
|
"memmap2",
|
||||||
"mime",
|
"mime",
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ tar = "~0.4"
|
|||||||
|
|
||||||
# and some stuff I don't know why it isn't in std
|
# and some stuff I don't know why it isn't in std
|
||||||
rand = { version = "~0.8", features = ["std", "alloc", "getrandom", "std_rng", "log"] }
|
rand = { version = "~0.8", features = ["std", "alloc", "getrandom", "std_rng", "log"] }
|
||||||
lazy_static = "~1.4"
|
|
||||||
regex = "~1.11"
|
regex = "~1.11"
|
||||||
chrono = "~0.4"
|
chrono = "~0.4"
|
||||||
memmap2 = "~0.9"
|
memmap2 = "~0.9"
|
||||||
@@ -244,13 +244,13 @@ impl FileCache {
|
|||||||
// do nothing, this is sent when a file is being removed
|
// do nothing, this is sent when a file is being removed
|
||||||
}
|
}
|
||||||
DebouncedEvent::Create(p) => {
|
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()),
|
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),
|
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) => {
|
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()),
|
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),
|
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;
|
mod routes;
|
||||||
|
|
||||||
use std::{fs, path::PathBuf};
|
use std::{fs, path::PathBuf};
|
||||||
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
use filecache::*;
|
use filecache::*;
|
||||||
use lazy_static::lazy_static;
|
|
||||||
use rocket::fairing::AdHoc;
|
use rocket::fairing::AdHoc;
|
||||||
|
|
||||||
static CACHE_DIR: &str = "CACHE/";
|
static CACHE_DIR: &str = "CACHE/";
|
||||||
|
|
||||||
lazy_static! {
|
fn filecache() -> &'static FileCache {
|
||||||
static ref FILECACHE: FileCache = {
|
static FILECACHE: OnceLock<FileCache> = OnceLock::new();
|
||||||
|
FILECACHE.get_or_init(
|
||||||
|
|| {
|
||||||
let mut cwd = std::env::current_dir().unwrap();
|
let mut cwd = std::env::current_dir().unwrap();
|
||||||
cwd.push(CACHE_DIR);
|
cwd.push(CACHE_DIR);
|
||||||
|
|
||||||
FileCache::new(&cwd)
|
FileCache::new(&cwd)
|
||||||
};
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
fn cache_path() -> &'static PathBuf {
|
||||||
static ref CACHE_PATH: PathBuf = {
|
static CACHE_PATH: OnceLock<PathBuf> = OnceLock::new();
|
||||||
|
CACHE_PATH.get_or_init(
|
||||||
|
|| {
|
||||||
let mut cwd = std::env::current_dir().unwrap();
|
let mut cwd = std::env::current_dir().unwrap();
|
||||||
cwd.push(&CACHE_DIR);
|
cwd.push(&CACHE_DIR);
|
||||||
|
|
||||||
cwd
|
cwd
|
||||||
};
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::main]
|
fn cache_setup() -> () {
|
||||||
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);
|
|
||||||
|
|
||||||
let keep_file = {
|
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.push("rfile.meta");
|
||||||
|
|
||||||
marker_path
|
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!(
|
Ok(_) => log::info!(
|
||||||
"crated new cache directory @ {}",
|
"crated new cache directory @ {}",
|
||||||
&CACHE_PATH.as_path().to_string_lossy()
|
cache_path().as_path().to_string_lossy()
|
||||||
),
|
),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("Error creating cache directory - {}", &e);
|
log::error!("Error creating cache directory - {}", &e);
|
||||||
@@ -107,7 +97,7 @@ async fn main() -> Result<(), rocket::Error> {
|
|||||||
match fs::File::create(&keep_file) {
|
match fs::File::create(&keep_file) {
|
||||||
Ok(_) => log::info!(
|
Ok(_) => log::info!(
|
||||||
"crated new enviroment file @ {}",
|
"crated new enviroment file @ {}",
|
||||||
&CACHE_PATH.as_path().to_string_lossy()
|
cache_path().as_path().to_string_lossy()
|
||||||
),
|
),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("Error creating enviroment file - {}", &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(_) => {
|
Ok(_) => {
|
||||||
log::info!("added {} to cache", &keep_file.as_path().to_string_lossy())
|
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!(),
|
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>")]
|
#[get("/download/<file_hash>")]
|
||||||
pub async fn download_file(file_hash: String) -> Option<CachedFile> {
|
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,
|
Ok(fe) => fe,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
panic!()
|
panic!()
|
||||||
@@ -137,7 +137,7 @@ pub async fn upload_file(mut form: Form<UploadFile<'_>>) -> Result<String, Strin
|
|||||||
|
|
||||||
let filepath = PathBuf::from(format!(
|
let filepath = PathBuf::from(format!(
|
||||||
"{}{}.{}",
|
"{}{}.{}",
|
||||||
&crate::CACHE_PATH.as_path().display(),
|
&crate::cache_path().as_path().display(),
|
||||||
&filename,
|
&filename,
|
||||||
extension
|
extension
|
||||||
));
|
));
|
||||||
|
|||||||
Reference in New Issue
Block a user