diff --git a/src/lib.rs b/src/lib.rs index a84f7f0..1780111 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -163,22 +163,23 @@ pub async fn handle_health(_req: Request) -> tide::Result, no_ttl_permanent: bool) -> tide::Result { - let data: StoreRequest = match req.body_json().await { - Ok(data) => data, - Err(e) => { - return Ok(Response::builder(StatusCode::BadRequest) - .body(format!("Invalid request: {}", e)) - .build()); - } - }; + // Extract key from URL parameter + let key: String = req.param("key")?.to_string(); - if data.key.len() != 64 || !data.key.chars().all(|c| c.is_ascii_hexdigit()) { + // Validate key format + if key.len() != 64 || !key.chars().all(|c| c.is_ascii_hexdigit()) { return Ok(Response::builder(StatusCode::BadRequest) .body("Invalid key: must be a 32 bytes hex string.".to_string()) .build()); } - let live_for: Option = if let Some(ttl) = data.ttl { + // Get TTL from query parameter (optional) + let ttl: Option = req.url().query_pairs() + .find(|(key, _)| key == "ttl") + .and_then(|(_, value)| value.parse().ok()); + log::info!("ttl: {:?}", ttl); + + let live_for: Option = if let Some(ttl) = ttl { if ttl < 60 { return Ok(Response::builder(StatusCode::BadRequest) .body(format!("Invalid ttl: must be at least {} seconds.", 60)) @@ -204,11 +205,12 @@ pub async fn handle_store(mut req: Request, no_ttl_permanent: bo None => None, }; - let value_bytes = match hex::decode(&data.value) { - Ok(value) => value, + // Read binary data directly from request body + let value_bytes = match req.body_bytes().await { + Ok(bytes) => bytes, Err(e) => { return Ok(Response::builder(StatusCode::BadRequest) - .body(format!("Invalid request: {}", e)) + .body(format!("Failed to read request body: {}", e)) .build()); } }; @@ -216,7 +218,7 @@ pub async fn handle_store(mut req: Request, no_ttl_permanent: bo log::info!("received {} bytes", value_bytes.len()); let svc = req.state(); - match svc.store_data(&data.key, &value_bytes, expires_at).await { + match svc.store_data(&key, &value_bytes, expires_at).await { Ok(()) => Ok(Response::builder(StatusCode::Ok) .body(serde_json::to_value(&ApiResponse { message: "Data stored successfully.".to_string(), @@ -242,9 +244,9 @@ pub async fn handle_retrieve(req: Request) -> tide::Result { - let encoded_value = hex::encode(value); Ok(Response::builder(StatusCode::Ok) - .body(serde_json::to_value(&RetrieveResponse { key, value: encoded_value })?) + .header("Content-Type", "application/octet-stream") + .body(value) .build()) } Err(e) => Ok(Response::builder(StatusCode::NotFound).body(e).build()), @@ -255,7 +257,7 @@ pub fn create_app(no_ttl_permanent: bool, storage_dir: impl Into) -> tid let svc = StorageService::new(storage_dir); let mut app = tide::with_state(svc); app.at("/health").get(handle_health); - app.at("/store").post(move |req| handle_store(req, no_ttl_permanent)); + app.at("/store/:key").post(move |req| handle_store(req, no_ttl_permanent)); app.at("/retrieve/:key").get(handle_retrieve); app } diff --git a/src/main.rs b/src/main.rs index c597cde..236fd23 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ use sdk_storage::{StorageService, create_app}; use tide::log; const STORAGE_DIR: &str = "./storage"; -const PORT: u16 = 8081; +const PORT: u16 = 8080; const DEFAULT_TTL: u64 = 86400;