feature(relay): add new route to handle transfer anouncments

This commit is contained in:
Patryk Hegenberg 2024-04-28 15:54:18 +02:00
parent 9fd1fde794
commit 2418f80bd9
2 changed files with 32 additions and 3 deletions

View file

@ -16,9 +16,10 @@
/// 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, State, WebSocketUpgrade},
extract::{ws::WebSocket, Json, State, WebSocketUpgrade},
http::StatusCode,
response::IntoResponse,
routing::get,
routing::{get, post},
Router,
};
@ -34,6 +35,7 @@ use tracing::{debug, error, info, warn};
use crate::relay::appstate::AppState;
use crate::relay::client::Client;
use crate::relay::transfer::Transfer;
/// This function starts the WebSocket server.
///
@ -95,6 +97,7 @@ pub async fn start_ws(port: Option<&i32>, listen_addr: Option<&String>) {
// Set up the application routes.
let app = Router::new()
.route("/ws", get(ws_handler))
.route("/upload", post(upload_info))
.with_state(server)
.layer(
TraceLayer::new_for_http()
@ -272,3 +275,23 @@ async fn shutdown_signal() {
_ = terminate => {},
}
}
pub async fn upload_info(
State(shared_state): State<Arc<RwLock<AppState>>>,
// ConnectInfo(addr): ConnectInfo<SocketAddr>,
Json(payload): Json<Transfer>,
) -> impl IntoResponse {
// debug!("Got upload request from {}", addr.ip().to_string());
let mut data = shared_state.write().await;
let t_request = Transfer {
name: payload.name,
ip: payload.ip,
room_id: payload.room_id,
};
data.transfers.push(t_request.clone());
debug!("New TransferRequest created");
debug!("Actual AppState is {:#?}", *data);
(StatusCode::CREATED, Json(t_request))
}

View file

@ -1,6 +1,11 @@
use crate::relay::appstate::AppState;
use crate::relay::server::upload_info;
use crate::relay::server::ws_handler;
use axum::{routing::get, Router};
use axum::{
extract::connect_info::ConnectInfo,
routing::{get, post},
Extension, Router,
};
use axum_client_ip::SecureClientIpSource;
use shuttle_axum::ShuttleAxum;
@ -17,6 +22,7 @@ async fn axum() -> ShuttleAxum {
// Set up the application routes.
let app = Router::new()
.route("/ws", get(ws_handler))
.route("/upload", post(upload_info))
.with_state(appstate)
.layer(SecureClientIpSource::ConnectInfo.into_extension());