Compare commits

...

3 Commits

Author SHA1 Message Date
74618cdf84 Deleted mut on app variable
All checks were successful
Build and Push to Registry / build-and-push (push) Successful in 1m21s
2025-11-12 10:06:23 +01:00
432b28484f Improved way to test the data 2025-11-12 10:06:23 +01:00
dbb6aff09d Replace the port and fix the starting log 2025-11-12 10:06:23 +01:00
3 changed files with 37 additions and 8 deletions

View File

@ -83,6 +83,11 @@ impl StorageService {
Ok(buffer)
}
pub async fn data_exists(&self, key: &str) -> bool {
let file_path = format!("{}/{}/{}", self.storage_dir, &key[..2], &key[2..]);
Path::new(&file_path).exists().await
}
pub async fn cleanup_expired_files_once(&self) -> Result<(), String> {
let mut entries = read_dir(&self.storage_dir)
.await
@ -253,11 +258,37 @@ pub async fn handle_retrieve(req: Request<StorageService>) -> tide::Result<Respo
}
}
pub async fn handle_test(req: Request<StorageService>) -> tide::Result<Response> {
let key: String = req.param("key")?.to_string();
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 svc = req.state();
if svc.data_exists(&key).await {
Ok(Response::builder(StatusCode::Ok)
.body(serde_json::to_value(&ApiResponse {
message: "Data exists.".to_string(),
})?)
.build())
} else {
Ok(Response::builder(StatusCode::NotFound)
.body(serde_json::to_value(&ApiResponse {
message: "Key not found.".to_string(),
})?)
.build())
}
}
pub fn create_app(no_ttl_permanent: bool, storage_dir: impl Into<String>) -> tide::Server<StorageService> {
let svc = StorageService::new(storage_dir);
let mut app = tide::with_state(svc);
app.at("/health").get(handle_health);
app.at("/store/:key").post(move |req| handle_store(req, no_ttl_permanent));
app.at("/retrieve/:key").get(handle_retrieve);
app.at("/test/:key").get(handle_test);
app
}

View File

@ -5,7 +5,7 @@ use sdk_storage::{StorageService, create_app};
use tide::log;
const STORAGE_DIR: &str = "./storage";
const PORT: u16 = 8080;
const PORT: u16 = 8081;
const DEFAULT_TTL: u64 = 86400;
@ -38,10 +38,8 @@ async fn main() -> tide::Result<()> {
}
});
let mut app = create_app(no_ttl_permanent, STORAGE_DIR);
let app = create_app(no_ttl_permanent, STORAGE_DIR);
println!("Server starting on http://0.0.0.0:{}", PORT);
app.listen(format!("0.0.0.0:{}", PORT)).await?;
println!("Server running at http://0.0.0.0:{}", PORT);
Ok(())
}

View File

@ -55,7 +55,7 @@ async fn http_store_success_and_conflicts_and_invalids() {
// app with permanent=false so default TTL applies when missing
let td = TempDir::new().unwrap();
let storage = td.path().to_string_lossy().to_string();
let mut app = create_app(false, storage);
let app = create_app(false, storage);
let listener = async_std::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
async_std::task::spawn(async move { app.listen(listener).await.unwrap() });
@ -88,7 +88,7 @@ async fn http_store_success_and_conflicts_and_invalids() {
async fn http_retrieve_success_and_invalid_and_notfound() {
let td = TempDir::new().unwrap();
let storage = td.path().to_string_lossy().to_string();
let mut app = create_app(true, storage.clone());
let app = create_app(true, storage.clone());
let listener = async_std::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
async_std::task::spawn(async move { app.listen(listener).await.unwrap() });
@ -121,7 +121,7 @@ async fn http_retrieve_success_and_invalid_and_notfound() {
async fn http_health_ok() {
let td = TempDir::new().unwrap();
let storage = td.path().to_string_lossy().to_string();
let mut app = create_app(true, storage);
let app = create_app(true, storage);
let listener = async_std::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
async_std::task::spawn(async move { app.listen(listener).await.unwrap() });