diff --git a/src/lib/web/endpoints.rs b/src/lib/web/endpoints.rs index abf96ae..e970a63 100644 --- a/src/lib/web/endpoints.rs +++ b/src/lib/web/endpoints.rs @@ -1,5 +1,7 @@ +use std::net::SocketAddr; + use axum::{ - extract::Path, + extract::{connect_info::ConnectInfo, Path, State}, http::{header, StatusCode}, response::IntoResponse, Json, @@ -7,8 +9,10 @@ use axum::{ use include_dir::{include_dir, Dir}; use mime_guess::from_path; use serde::{Deserialize, Serialize}; +use tracing::*; use crate::stats; +use crate::web::AppState; static HTML_DIST: Dir = include_dir!("src/webpage/dist"); @@ -92,6 +96,17 @@ pub async fn mavlink(path: Option>) -> impl IntoResponse { crate::drivers::rest::data::messages(&path) } +pub async fn post_mavlink( + ConnectInfo(address): ConnectInfo, + State(state): State, + message: String, +) { + debug!("Got message from: {address:?}, {message}"); + if let Err(error) = state.message_tx.send(message) { + error!("Failed to send message to main loop: {error:?}"); + } +} + pub async fn message_id_from_name(name: Path) -> impl IntoResponse { use mavlink::{self, Message}; mavlink::ardupilotmega::MavMessage::message_id_from_name(&name.0.to_ascii_uppercase()) diff --git a/src/lib/web/mod.rs b/src/lib/web/mod.rs index a081e71..35c281b 100644 --- a/src/lib/web/mod.rs +++ b/src/lib/web/mod.rs @@ -2,6 +2,7 @@ mod endpoints; use std::{ collections::HashMap, + net::SocketAddr, sync::{Arc, Mutex}, }; @@ -12,7 +13,7 @@ use axum::{ }, http::StatusCode, response::Response, - routing::get, + routing::{get, post}, Router, }; use futures::{sink::SinkExt, stream::StreamExt}; @@ -49,6 +50,7 @@ fn default_router(state: AppState) -> Router { .route("/rest/ws", get(websocket_handler)) // We are matching all possible keys for the user .route("/rest/mavlink", get(endpoints::mavlink)) + .route("/rest/mavlink", post(endpoints::post_mavlink)) .route("/rest/mavlink/", get(endpoints::mavlink)) .route("/rest/mavlink/*path", get(endpoints::mavlink)) .route( @@ -369,9 +371,14 @@ pub async fn run(address: String) { } }; - if let Err(error) = axum::serve(listener, router.clone()) - .with_graceful_shutdown(shutdown_signal()) - .await + if let Err(error) = axum::serve( + listener, + router + .clone() + .into_make_service_with_connect_info::(), + ) + .with_graceful_shutdown(shutdown_signal()) + .await { error!("WebServer error: {error}"); continue;