Skip to content

Commit

Permalink
server: Add default endpoint creation
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick José Pereira <[email protected]>
  • Loading branch information
patrickelectric committed Nov 27, 2023
1 parent 6dc5084 commit 31f1a05
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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 {
Expand All @@ -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();
Expand All @@ -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.
Expand All @@ -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)
Expand Down

0 comments on commit 31f1a05

Please sign in to comment.