diff --git a/src/relay/server.rs b/src/relay/server.rs index f81905e..20c01d6 100644 --- a/src/relay/server.rs +++ b/src/relay/server.rs @@ -16,7 +16,7 @@ /// and running the application. If the server fails to bind to the specified /// host and port, it logs an error and exits. use axum::{ - extract::{ws::WebSocket, Json, State, WebSocketUpgrade}, + extract::{ws::WebSocket, Json, Path, State, WebSocketUpgrade}, http::StatusCode, response::IntoResponse, routing::{get, post}, @@ -98,6 +98,7 @@ pub async fn start_ws(port: Option<&i32>, listen_addr: Option<&String>) { let app = Router::new() .route("/ws", get(ws_handler)) .route("/upload", post(upload_info)) + .route("/download/:name", get(download_info)) .with_state(server) .layer( TraceLayer::new_for_http() @@ -295,3 +296,27 @@ pub async fn upload_info( (StatusCode::CREATED, Json(t_request)) } + +pub async fn download_info( + State(shared_state): State>>, + Path(name): Path, +) -> impl IntoResponse { + let data = shared_state.write().await; + match data.transfers.iter().find(|request| request.name == name) { + Some(request) => { + debug!("Found transfer name."); + (StatusCode::OK, Json(request.clone())) + } + None => { + warn!("couldn't find transfer-name: {}", name); + ( + StatusCode::NOT_FOUND, + Json(Transfer { + name: "".to_string(), + ip: "".to_string(), + room_id: "".to_string(), + }), + ) + } + } +} diff --git a/src/shuttle.rs b/src/shuttle.rs index 75772df..f35dc1e 100644 --- a/src/shuttle.rs +++ b/src/shuttle.rs @@ -1,4 +1,5 @@ use crate::relay::appstate::AppState; +use crate::relay::server::download_info; use crate::relay::server::upload_info; use crate::relay::server::ws_handler; use axum::{ @@ -22,6 +23,7 @@ async fn axum() -> ShuttleAxum { let app = Router::new() .route("/ws", get(ws_handler)) .route("/upload", post(upload_info)) + .route("/download/:name", get(download_info)) .with_state(appstate) .layer(SecureClientIpSource::ConnectInfo.into_extension());