From e790c3b0c4c30281346faecd655db7a1fa530265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Mon, 27 Nov 2023 13:03:40 -0300 Subject: [PATCH 1/3] ci: test: Disable test with SITL MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- .github/workflows/test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 027e0fc..e8fcd52 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -24,6 +24,7 @@ jobs: - name: Build run: cargo build --verbose - name: Run SITL & MAVLink2Rest + if: ${{ false }} # Disable step until TCP connection is fixed timeout-minutes: 5 run: | pip install --user aiohttp asyncio requests From 2ccf5e6e42dd9d69096577348e582e686efbf10d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Mon, 27 Nov 2023 13:54:39 -0300 Subject: [PATCH 2/3] cli: Add default-api-version MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/cli.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/cli.rs b/src/cli.rs index b3b39c2..2be554e 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -34,6 +34,16 @@ pub fn server_address() -> &'static str { return MANAGER.as_ref().clap_matches.value_of("server").unwrap(); } +pub fn default_api_version() -> u8 { + return MANAGER + .as_ref() + .clap_matches + .value_of("default-api-version") + .unwrap() + .parse::() + .unwrap(); +} + pub fn mavlink_version() -> u8 { return MANAGER .as_ref() @@ -119,6 +129,15 @@ fn get_clap_matches<'a>() -> clap::ArgMatches<'a> { .takes_value(true) .default_value("0"), ) + .arg( + clap::Arg::with_name("default-api-version") + .long("default-api-version") + .value_name("DEFAULT_API_VERSION") + .help("Sets the default version used by the REST API, this will remove the prefix used by its path.") + .takes_value(true) + .possible_values(&["1"]) + .default_value("1"), + ) .arg( clap::Arg::with_name("verbose") .short("v") @@ -140,5 +159,6 @@ mod tests { assert_eq!(mavlink_connection_string(), "udpin:0.0.0.0:14550"); assert_eq!(server_address(), "0.0.0.0:8088"); assert_eq!(mavlink_version(), 2); + assert_eq!(default_api_version(), 1); } } From b0f459aad63d071a26a60e6cb6e878a02c86d5e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Jos=C3=A9=20Pereira?= Date: Mon, 27 Nov 2023 13:56:04 -0300 Subject: [PATCH 3/3] server: Add default endpoint creation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Patrick José Pereira --- src/server.rs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/server.rs b/src/server.rs index 7b72f5e..9bc89a4 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,7 +1,7 @@ use super::endpoints; use super::mavlink_vehicle::MAVLinkVehicleArcMutex; -use paperclip::actix::{web, OpenApiExt}; +use paperclip::actix::{web, web::Scope, OpenApiExt}; use actix_cors::Cors; use actix_web::{ @@ -10,6 +10,7 @@ use actix_web::{ App, HttpRequest, HttpServer, }; +use crate::cli; use log::*; fn json_error_handler(error: JsonPayloadError, _: &HttpRequest) -> actix_web::Error { @@ -20,6 +21,15 @@ fn json_error_handler(error: JsonPayloadError, _: &HttpRequest) -> actix_web::Er } } +fn add_v1_paths(scope: Scope) -> Scope { + scope + .route("/helper/mavlink", web::get().to(endpoints::helper_mavlink)) + .route("/mavlink", web::get().to(endpoints::mavlink)) + .route("/mavlink", web::post().to(endpoints::mavlink_post)) + .route(r"/mavlink/{path:.*}", web::get().to(endpoints::mavlink)) + .service(web::resource("/ws/mavlink").route(web::get().to(endpoints::websocket))) +} + // Start REST API server with the desired address pub fn run(server_address: &str, mavlink_vehicle: &MAVLinkVehicleArcMutex) { let server_address = server_address.to_string(); @@ -29,6 +39,11 @@ pub fn run(server_address: &str, mavlink_vehicle: &MAVLinkVehicleArcMutex) { // Start HTTP server thread let _ = System::new("http-server"); HttpServer::new(move || { + let v1 = add_v1_paths(web::scope("/v1")); + let default = match cli::default_api_version() { + 1 => add_v1_paths(web::scope("")), + _ => unreachable!("CLI should only allow supported values."), + }; App::new() .wrap(Cors::permissive()) // Record services and routes for paperclip OpenAPI plugin for Actix. @@ -45,16 +60,8 @@ pub fn run(server_address: &str, mavlink_vehicle: &MAVLinkVehicleArcMutex) { web::get().to(endpoints::root), ) .route("/info", web::get().to(endpoints::info)) - .service( - web::scope("/v1") - .route("/helper/mavlink", web::get().to(endpoints::helper_mavlink)) - .route("/mavlink", web::get().to(endpoints::mavlink)) - .route("/mavlink", web::post().to(endpoints::mavlink_post)) - .route(r"/mavlink/{path:.*}", web::get().to(endpoints::mavlink)) - .service( - web::resource("/ws/mavlink").route(web::get().to(endpoints::websocket)), - ), - ) + .service(default) + .service(v1) .build() }) .bind(server_address)