Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
fix use rocket to parse get uid?rid
Browse files Browse the repository at this point in the history
  • Loading branch information
OscarZhou0107 committed May 2, 2024
1 parent 20d3daa commit 0afe71c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 25 deletions.
2 changes: 1 addition & 1 deletion server/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl DiskCache {
let duration = end_time - start_time;

debug!(
"Request id {} ask for {} started at {} and ended at {}, taking {:?}",
"[Request id {}] fetch file {} started at {} and ended at {}, taking {:?}",
rid.clone(),
uid_str.clone(),
start_time.to_rfc3339(),
Expand Down
32 changes: 8 additions & 24 deletions server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,25 @@ async fn cache_stats(cache: &State<Arc<ConcurrentDiskCache>>) -> String {
cache.get_stats().await
}

#[get("/s3/<uid..>")]
#[get("/s3/<uid>?<rid>")]
async fn get_file(
uid: PathBuf,
uid: String,
rid: Option<String>,
cache: &State<Arc<ConcurrentDiskCache>>,
s3_connectors: &State<Vec<Arc<dyn StorageConnector + Send + Sync>>>,
) -> cache::GetFileResult {
let full_uid_str = uid.to_string_lossy().to_string();
let parts: Vec<&str> = full_uid_str.split('?').collect();
let uid_str = parts[0].to_string();
let request_id = rid.unwrap_or_else(|| "0".to_string()); // Default to "0" if `rid` is not provided

// Default request ID
let mut request_id = "0".to_string();
debug!("Requested file: {}", uid);
debug!("Request ID: {}", request_id);

// Check if there's a query part for request ID
if parts.len() == 2 {
let request_part = parts[1];
let rid_key_value: Vec<&str> = request_part.split('=').collect();
if rid_key_value.len() == 2 && rid_key_value[0] == "rid" {
request_id = rid_key_value[1].to_string();
} else {
return cache::GetFileResult::InitFailed(format!(
"Invalid request ID format: {}",
request_part
));
}
}

debug!("Handling request ID: {}", request_id);
let index = hash(&uid_str) % s3_connectors.len(); // Use the converted string
let index = hash(&uid) % s3_connectors.len();
let s3_connector = &s3_connectors[index];

cache
.inner()
.clone()
.get_file(PathBuf::from(uid_str), request_id, s3_connector.clone()) // Use PathBuf from string
.get_file(PathBuf::from(uid), request_id, s3_connector.clone())
.await
}

Expand Down

0 comments on commit 0afe71c

Please sign in to comment.