Skip to content

Commit

Permalink
web: Add mavlink post endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick José Pereira <[email protected]>
  • Loading branch information
patrickelectric authored and joaoantoniocardoso committed Nov 8, 2024
1 parent cc1271a commit dcde1f9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
17 changes: 16 additions & 1 deletion src/lib/web/endpoints.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use std::net::SocketAddr;

use axum::{
extract::Path,
extract::{connect_info::ConnectInfo, Path, State},
http::{header, StatusCode},
response::IntoResponse,
Json,
};
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");

Expand Down Expand Up @@ -92,6 +96,17 @@ pub async fn mavlink(path: Option<Path<String>>) -> impl IntoResponse {
crate::drivers::rest::data::messages(&path)
}

pub async fn post_mavlink(
ConnectInfo(address): ConnectInfo<SocketAddr>,
State(state): State<AppState>,
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<String>) -> impl IntoResponse {
use mavlink::{self, Message};
mavlink::ardupilotmega::MavMessage::message_id_from_name(&name.0.to_ascii_uppercase())
Expand Down
15 changes: 11 additions & 4 deletions src/lib/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod endpoints;

use std::{
collections::HashMap,
net::SocketAddr,
sync::{Arc, Mutex},
};

Expand All @@ -12,7 +13,7 @@ use axum::{
},
http::StatusCode,
response::Response,
routing::get,
routing::{get, post},
Router,
};
use futures::{sink::SinkExt, stream::StreamExt};
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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::<SocketAddr>(),
)
.with_graceful_shutdown(shutdown_signal())
.await
{
error!("WebServer error: {error}");
continue;
Expand Down

0 comments on commit dcde1f9

Please sign in to comment.