From f1175e8b2402ad917eee6c442cfa97cbe723224b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Wed, 13 Nov 2024 11:05:04 -0300 Subject: [PATCH] web: Move mavlink endpoints to service MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/lib/web/mavlink_endpoints.rs | 17 ++++++++++++++++- src/lib/web/mod.rs | 13 ++----------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/lib/web/mavlink_endpoints.rs b/src/lib/web/mavlink_endpoints.rs index 2a992bf..f7eb0b6 100644 --- a/src/lib/web/mavlink_endpoints.rs +++ b/src/lib/web/mavlink_endpoints.rs @@ -8,7 +8,8 @@ use axum::{ }, http::StatusCode, response::{IntoResponse, Response}, - Json, + routing::{get, post}, + Json, Router, }; use futures::{sink::SinkExt, stream::StreamExt}; @@ -18,6 +19,20 @@ use uuid::Uuid; use crate::web::{broadcast_message_websockets, AppState}; +pub fn router() -> Router { + Router::new() + .route("/ws", get(websocket_handler)) + // We are matching all possible keys for the user + .route("/mavlink", get(mavlink)) + .route("/mavlink", post(post_mavlink)) + .route("/mavlink/", get(mavlink)) + .route("/mavlink/*path", get(mavlink)) + .route( + "/mavlink/message_id_from_name/*name", + get(message_id_from_name), + ) +} + pub async fn mavlink(path: Option>) -> impl IntoResponse { let path = match path { Some(path) => path.0.to_string(), diff --git a/src/lib/web/mod.rs b/src/lib/web/mod.rs index 7ba349a..c925544 100644 --- a/src/lib/web/mod.rs +++ b/src/lib/web/mod.rs @@ -14,7 +14,7 @@ use axum::{ }, http::StatusCode, response::Response, - routing::{get, post}, + routing::get, Router, }; use futures::{sink::SinkExt, stream::StreamExt}; @@ -49,16 +49,7 @@ fn default_router(state: AppState) -> Router { "/stats/messages/ws", get(hub_messages_stats_websocket_handler), ) - .route("/rest/ws", get(mavlink_endpoints::websocket_handler)) - // We are matching all possible keys for the user - .route("/rest/mavlink", get(mavlink_endpoints::mavlink)) - .route("/rest/mavlink", post(mavlink_endpoints::post_mavlink)) - .route("/rest/mavlink/", get(mavlink_endpoints::mavlink)) - .route("/rest/mavlink/*path", get(mavlink_endpoints::mavlink)) - .route( - "/rest/mavlink/message_id_from_name/*name", - get(mavlink_endpoints::message_id_from_name), - ) + .nest("/rest/", mavlink_endpoints::router()) .fallback(get(|| async { (StatusCode::NOT_FOUND, "Not found :(") })) .with_state(state) }