Compare commits

..

No commits in common. "74618cdf841646388614bd6cf0249499a87e31b1" and "ecf48cec7efddbc35dc42540d23c50935a561204" have entirely different histories.

3 changed files with 8 additions and 37 deletions

View File

@ -83,11 +83,6 @@ impl StorageService {
Ok(buffer) 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> { pub async fn cleanup_expired_files_once(&self) -> Result<(), String> {
let mut entries = read_dir(&self.storage_dir) let mut entries = read_dir(&self.storage_dir)
.await .await
@ -258,37 +253,11 @@ 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> { pub fn create_app(no_ttl_permanent: bool, storage_dir: impl Into<String>) -> tide::Server<StorageService> {
let svc = StorageService::new(storage_dir); let svc = StorageService::new(storage_dir);
let mut app = tide::with_state(svc); let mut app = tide::with_state(svc);
app.at("/health").get(handle_health); app.at("/health").get(handle_health);
app.at("/store/:key").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.at("/retrieve/:key").get(handle_retrieve);
app.at("/test/:key").get(handle_test);
app app
} }

View File

@ -5,7 +5,7 @@ use sdk_storage::{StorageService, create_app};
use tide::log; use tide::log;
const STORAGE_DIR: &str = "./storage"; const STORAGE_DIR: &str = "./storage";
const PORT: u16 = 8081; const PORT: u16 = 8080;
const DEFAULT_TTL: u64 = 86400; const DEFAULT_TTL: u64 = 86400;
@ -38,8 +38,10 @@ async fn main() -> tide::Result<()> {
} }
}); });
let app = create_app(no_ttl_permanent, STORAGE_DIR); let mut 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?; app.listen(format!("0.0.0.0:{}", PORT)).await?;
println!("Server running at http://0.0.0.0:{}", PORT);
Ok(()) 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 // app with permanent=false so default TTL applies when missing
let td = TempDir::new().unwrap(); let td = TempDir::new().unwrap();
let storage = td.path().to_string_lossy().to_string(); let storage = td.path().to_string_lossy().to_string();
let app = create_app(false, storage); let mut app = create_app(false, storage);
let listener = async_std::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); let listener = async_std::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap(); let addr = listener.local_addr().unwrap();
async_std::task::spawn(async move { app.listen(listener).await.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() { async fn http_retrieve_success_and_invalid_and_notfound() {
let td = TempDir::new().unwrap(); let td = TempDir::new().unwrap();
let storage = td.path().to_string_lossy().to_string(); let storage = td.path().to_string_lossy().to_string();
let app = create_app(true, storage.clone()); let mut app = create_app(true, storage.clone());
let listener = async_std::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); let listener = async_std::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap(); let addr = listener.local_addr().unwrap();
async_std::task::spawn(async move { app.listen(listener).await.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() { async fn http_health_ok() {
let td = TempDir::new().unwrap(); let td = TempDir::new().unwrap();
let storage = td.path().to_string_lossy().to_string(); let storage = td.path().to_string_lossy().to_string();
let app = create_app(true, storage); let mut app = create_app(true, storage);
let listener = async_std::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); let listener = async_std::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap(); let addr = listener.local_addr().unwrap();
async_std::task::spawn(async move { app.listen(listener).await.unwrap() }); async_std::task::spawn(async move { app.listen(listener).await.unwrap() });