From b59be905d88a02ba35984dacadc0387658b6c93e Mon Sep 17 00:00:00 2001 From: Andreas Neukoetter Date: Thu, 14 Apr 2022 20:20:07 +0200 Subject: [PATCH] + Implement missing variable functions for Axum. --- TODO.md | 2 +- src/http_api_axum.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/TODO.md b/TODO.md index 6525585..f218103 100644 --- a/TODO.md +++ b/TODO.md @@ -4,7 +4,7 @@ TODO: This file is not 100% up to date. Sorry. ## In Progress - [ ] Remove actix, and use axum instead - - [ ] Implement API in Axum + - [x] Implement API in Axum - [ ] Remove Actix-web - [ ] Remove Actix diff --git a/src/http_api_axum.rs b/src/http_api_axum.rs index 4116992..91340bf 100644 --- a/src/http_api_axum.rs +++ b/src/http_api_axum.rs @@ -54,7 +54,22 @@ impl HttpApiAxum { .route("/page/name/:page_name", get(goto_page_name)) .route("/show/name/:name", get(show_by_name)) .route("/hide/name/:name", get(hide_by_name)) + .route("/selectNextVariable", get(select_next_variable)) + .route( + "/selectNextVariableWithPrefix/:prefix", + get(select_next_variable_with_prefix), + ) + .route( + "/incSelectedVariable/:value", + get(inc_selected_variable), + ) + .route( + "/decSelectedVariable/:value", + get(dec_selected_variable), + ) .route("/setVariable/:name/:value", get(set_variable)) + .route("/incVariable/:name/:delta", get(inc_variable)) + .route("/decVariable/:name/:delta", get(dec_variable)) .layer(Extension(http_state)) ; let addr = SocketAddr::from(([127, 0, 0, 1], 8080)); @@ -195,6 +210,24 @@ fn send_message_and_handle_response( state: &Arc>, m } } + +async fn select_next_variable( + Extension(state): Extension>>, +) -> impl IntoResponse { + debug!("select_next_variable" ); + let (sender, receiver) = mpsc::channel(); + send_message_and_handle_response( &state, Message::SelectNextVariable(sender, None ), receiver ) +} + +async fn select_next_variable_with_prefix( + Extension(state): Extension>>, + Path( prefix ): Path, +) -> impl IntoResponse { + debug!("select_next_variable_with_prefix {}", &prefix ); + let (sender, receiver) = mpsc::channel(); + send_message_and_handle_response( &state, Message::SelectNextVariable(sender, Some(prefix.clone()) ), receiver ) +} + async fn set_variable( Extension(state): Extension>>, Path((name,value)): Path<(String,String)>, @@ -204,5 +237,38 @@ async fn set_variable( send_message_and_handle_response( &state, Message::SetVariable(sender, name.clone(), value.clone() ), receiver ) } +async fn inc_variable( + Extension(state): Extension>>, + Path((name,delta)): Path<(String,i32)>, +) -> impl IntoResponse { + debug!("inc_variable {} +{}", &name, &delta ); + let (sender, receiver) = mpsc::channel(); + send_message_and_handle_response( &state, Message::IncrementVariable(sender, name.clone(), delta ), receiver ) +} +async fn dec_variable( + Extension(state): Extension>>, + Path((name,delta)): Path<(String,i32)>, +) -> impl IntoResponse { + debug!("dec_variable {} -{}", &name, &delta ); + let (sender, receiver) = mpsc::channel(); + send_message_and_handle_response( &state, Message::IncrementVariable(sender, name.clone(), -delta ), receiver ) +} +async fn inc_selected_variable( + Extension(state): Extension>>, + Path(delta): Path, +) -> impl IntoResponse { + debug!("inc_selected_variable +{}", &delta ); + let (sender, receiver) = mpsc::channel(); + send_message_and_handle_response( &state, Message::IncrementSelectedVariable(sender, delta ), receiver ) +} + +async fn dec_selected_variable( + Extension(state): Extension>>, + Path(delta): Path, +) -> impl IntoResponse { + debug!("dec_selected_variable -{}", &delta ); + let (sender, receiver) = mpsc::channel(); + send_message_and_handle_response( &state, Message::IncrementSelectedVariable(sender, -delta ), receiver ) +}