From a4091df1cbbd4c1c6bb51db581571665fa2c185a Mon Sep 17 00:00:00 2001 From: Super User Date: Tue, 18 Mar 2025 21:31:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DPublish=E5=8F=AA=E8=83=BD?= =?UTF-8?q?=E5=8F=91=E9=80=81json=E6=95=B0=E6=8D=AE=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cores/handlers/datamgr/route.rs | 52 +++++++++++++++++++++++++++-- src/cores/servers/actix_web/mod.rs | 3 +- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/cores/handlers/datamgr/route.rs b/src/cores/handlers/datamgr/route.rs index f5c0c6d..10b4b1f 100644 --- a/src/cores/handlers/datamgr/route.rs +++ b/src/cores/handlers/datamgr/route.rs @@ -781,7 +781,7 @@ pub async fn order_result_handler( } } -pub async fn publish_handler( +/*pub async fn publish_handler( _: Arc, server_request: ServerRequest, ) -> ServerResult { @@ -817,7 +817,7 @@ pub async fn publish_handler( } } -/*extern "C" fn rust_callback( +extern "C" fn rust_callback( topic: *const ::std::os::raw::c_char, uuid: *const ::std::os::raw::c_char, size: ::std::os::raw::c_int, @@ -887,6 +887,54 @@ pub async fn subscribe_handler( Ok(ReceiverStream::new(rx)) }*/ +pub async fn publish_handler( + _: Arc, + server_request: ServerRequest, +) -> ServerResponse { + + let headers = server_request.headers; + + let body = server_request.body.as_ref().unwrap().as_slice(); + let data_content = body.to_vec(); + + let bad_request = |body_str| { + ServerRawResponse::bad_request() + .body(Vec::from(body_str)) + .build() + .into() + }; + + let header_cloud_topic = match headers.get("x-cloud-topic") { + Some(value) => value.to_string(), + None => return bad_request("Missing x-cloud-topic header"), + }; + + let cloud_topic = match CString::new(header_cloud_topic) { + Ok(cstr) => cstr, + Err(_) => return bad_request("Invalid cloud topic"), + }; + + unsafe { + let ret = datamgr_api::Publish( + DATA_PLUGIN_MANAGER, + cloud_topic.as_ptr(), + data_content.len() as i32, + data_content.as_ptr() as *const i8, + ); + if ret == 1 { + ServerRawResponse::ok() + .body(Vec::from("Publish data success")) + .build() + .into() + } else { + ServerRawResponse::internal_error() + .body(Vec::from("Publish data failed")) + .build() + .into() + } + } +} + extern "C" fn rust_callback( topic: *const ::std::os::raw::c_char, uuid: *const ::std::os::raw::c_char, diff --git a/src/cores/servers/actix_web/mod.rs b/src/cores/servers/actix_web/mod.rs index a9a2bf0..e6c4321 100644 --- a/src/cores/servers/actix_web/mod.rs +++ b/src/cores/servers/actix_web/mod.rs @@ -659,8 +659,7 @@ pub mod datamgr { "DataMgr" ); - define_json_response_method!( - body_required + define_raw_response_method!( publish, post, "/Publish", -- Gitee