diff --git a/src/cores/handlers/datamgr/route.rs b/src/cores/handlers/datamgr/route.rs index f5c0c6d7f15e3f2d3b0fd54951b7d3b6125267c3..10b4b1f7c6e9039d314b09774dbe7d3ec77ce2cb 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 a9a2bf003effbd343c73920614b1d78bd5689f43..e6c4321a9d117de3ff5e94f2a26c1db957e7dc60 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",