From 718d62a726b735163c44d718b97cfbeb7d4b72de Mon Sep 17 00:00:00 2001 From: pmantica11 <151664502+pmantica11@users.noreply.github.com> Date: Tue, 13 Aug 2024 12:18:18 -0600 Subject: [PATCH] Add endpoint to fetch slot (#166) --- src/snapshot/snapshotter/main.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/snapshot/snapshotter/main.rs b/src/snapshot/snapshotter/main.rs index 63eef29c..ad7e8ca8 100644 --- a/src/snapshot/snapshotter/main.rs +++ b/src/snapshot/snapshotter/main.rs @@ -122,6 +122,32 @@ async fn stream_bytes( .body(Body::wrap_stream(byte_body)) } +async fn fetch_slot( + directory_adapter: Arc, +) -> Result, hyper::http::Error> { + let snapshot_files = get_snapshot_files_with_metadata(directory_adapter.as_ref()).await; + + match snapshot_files { + Ok(snapshot_files) => { + let last_snapshot = snapshot_files.last(); + match last_snapshot { + Some(snapshot) => Response::builder() + .status(StatusCode::OK) + .body(Body::from(snapshot.end_slot.to_string())), + None => Response::builder() + .status(StatusCode::NOT_FOUND) + .body(Body::from("No snapshots found")), + } + } + Err(e) => { + error!("Error fetching snapshot files: {:?}", e); + Response::builder() + .status(StatusCode::INTERNAL_SERVER_ERROR) + .body(Body::from("Internal Server Error")) + } + } +} + async fn handle_request( req: Request, directory_adapter: Arc, @@ -139,6 +165,7 @@ async fn handle_request( "/health" | "/readiness" => Response::builder() .status(StatusCode::OK) .body(Body::from("OK")), + "/slot" => fetch_slot(directory_adapter).await, _ => Response::builder() .status(StatusCode::NOT_FOUND) .body(Body::from("404 Not Found")),