switch to tokio mutexes, it compiles now

This commit is contained in:
minish 2022-12-29 00:03:14 -05:00 committed by minish
parent a2fccd1f1c
commit 9a22170dfa
6 changed files with 179 additions and 158 deletions

19
Cargo.lock generated
View File

@ -38,6 +38,7 @@ checksum = "08b108ad2665fa3f6e6a517c3d80ec3e77d224c47d605167aefaa5d7ef97fa48"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"axum-core", "axum-core",
"axum-macros",
"bitflags", "bitflags",
"bytes", "bytes",
"futures-util", "futures-util",
@ -80,6 +81,18 @@ dependencies = [
"tower-service", "tower-service",
] ]
[[package]]
name = "axum-macros"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4df0fc33ada14a338b799002f7e8657711422b25d4e16afb032708d6b185621"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.3.2" version = "1.3.2"
@ -225,6 +238,12 @@ dependencies = [
"ahash", "ahash",
] ]
[[package]]
name = "heck"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.1.19" version = "0.1.19"

View File

@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
axum = "0.6.1" axum = { version = "0.6.1", features = ["macros"] }
hyper = { version = "0.14", features = ["full"] } hyper = { version = "0.14", features = ["full"] }
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
tokio-util = { version = "0.7.4", features = ["full"] } tokio-util = { version = "0.7.4", features = ["full"] }

View File

@ -1,4 +1,6 @@
use std::sync::{Arc, Mutex}; use std::sync::Arc;
extern crate axum;
use axum::{ use axum::{
routing::{get, post}, routing::{get, post},
@ -6,6 +8,7 @@ use axum::{
}; };
use bytes::Bytes; use bytes::Bytes;
use memory_cache::MemoryCache; use memory_cache::MemoryCache;
use tokio::sync::Mutex;
mod state; mod state;
mod new; mod new;

View File

@ -44,6 +44,7 @@ fn gen_path(original_name: &String) -> PathBuf {
} }
} }
#[axum::debug_handler]
pub async fn new( pub async fn new(
State(state): State<Arc<crate::state::AppState>>, State(state): State<Arc<crate::state::AppState>>,
headers: HeaderMap, headers: HeaderMap,
@ -119,7 +120,7 @@ pub async fn new(
tx.send(chunk.clone()).await.unwrap(); tx.send(chunk.clone()).await.unwrap();
if use_cache { if use_cache {
println!("[upl] receiving data into cache"); println!("[upl] receiving data into buffer");
if data.len() + chunk.len() > data.capacity() { if data.len() + chunk.len() > data.capacity() {
println!("[upl] too much data! the client had an invalid content-length!"); println!("[upl] too much data! the client had an invalid content-length!");
@ -132,11 +133,11 @@ pub async fn new(
} }
} }
let mut cache = state.cache.lock().unwrap(); let mut cache = state.cache.lock().await;
if use_cache { if use_cache {
println!("[upl] caching upload!!"); println!("[upl] caching upload!!");
cache.insert(name, data.freeze(), Some(Duration::from_secs(30))); cache.insert(name, data.freeze(), Some(Duration::from_secs(120)));
} }
}); });

View File

@ -1,7 +1,6 @@
use std::sync::{Mutex, Arc};
use bytes::Bytes; use bytes::Bytes;
use memory_cache::MemoryCache; use memory_cache::MemoryCache;
use tokio::sync::Mutex;
pub struct AppState { pub struct AppState {
pub cache: Mutex<MemoryCache<String, Bytes>> pub cache: Mutex<MemoryCache<String, Bytes>>

View File

@ -7,7 +7,7 @@ use std::{
use axum::{ use axum::{
body::StreamBody, body::StreamBody,
extract::{Path, State}, extract::{Path, State},
response::{IntoResponse, Response}, response::{IntoResponse, Response}, debug_handler,
}; };
use bytes::{buf::Reader, Bytes}; use bytes::{buf::Reader, Bytes};
use hyper::StatusCode; use hyper::StatusCode;
@ -28,12 +28,11 @@ impl IntoResponse for ViewResponse {
} }
} */ } */
#[axum::debug_handler]
pub async fn view( pub async fn view(
State(state): State<Arc<crate::state::AppState>>, State(state): State<Arc<crate::state::AppState>>,
Path(original_path): Path<PathBuf>, Path(original_path): Path<PathBuf>,
) -> Response { ) -> Response {
println!("{:?}", original_path);
// (hopefully) prevent path traversal, just check for any non-file components // (hopefully) prevent path traversal, just check for any non-file components
if original_path if original_path
.components() .components()
@ -50,11 +49,11 @@ pub async fn view(
.unwrap_or_default() .unwrap_or_default()
.to_string(); .to_string();
let cache = state.cache.lock().unwrap(); let cache = state.cache.lock().await;
let cache_item = cache.get(&name); let cache_item = cache.get(&name);
if true /* cache_item.is_none() */ { if cache_item.is_none() {
let mut path = PathBuf::new(); let mut path = PathBuf::new();
path.push("uploads/"); path.push("uploads/");
path.push(name); path.push(name);