Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web: Add mavlink post endpoint #76

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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