|
| 1 | +use dapr::{ |
| 2 | + appcallback::*, |
| 3 | + dapr::dapr::proto::runtime::v1::app_callback_server::{AppCallback, AppCallbackServer}, |
| 4 | +}; |
| 5 | +use prost::Message; |
| 6 | +use tonic::{transport::Server, Request, Response, Status}; |
| 7 | + |
| 8 | +use hello_world::{HelloReply, HelloRequest}; |
| 9 | + |
| 10 | +pub mod hello_world { |
| 11 | + tonic::include_proto!("helloworld"); // The string specified here must match the proto package name |
| 12 | +} |
| 13 | + |
| 14 | +pub struct AppCallbackService {} |
| 15 | + |
| 16 | +#[tonic::async_trait] |
| 17 | +impl AppCallback for AppCallbackService { |
| 18 | + /// Invokes service method with InvokeRequest. |
| 19 | + async fn on_invoke( |
| 20 | + &self, |
| 21 | + request: Request<InvokeRequest>, |
| 22 | + ) -> Result<Response<InvokeResponse>, Status> { |
| 23 | + let r = request.into_inner(); |
| 24 | + |
| 25 | + let method = &r.method; |
| 26 | + println!("Method: {method}"); |
| 27 | + let data = &r.data; |
| 28 | + |
| 29 | + if let Some(any) = data { |
| 30 | + let data = &any.value; |
| 31 | + let resp = HelloRequest::decode(&data[..]).unwrap(); |
| 32 | + println!("Name: {:#?}", &resp.name); |
| 33 | + |
| 34 | + let response = HelloReply { |
| 35 | + message: "Hello World!".to_string(), |
| 36 | + }; |
| 37 | + let data = response.encode_to_vec(); |
| 38 | + |
| 39 | + let data = prost_types::Any { |
| 40 | + type_url: "".to_string(), |
| 41 | + value: data, |
| 42 | + }; |
| 43 | + |
| 44 | + let invoke_response = InvokeResponse { |
| 45 | + content_type: "application/json".to_string(), |
| 46 | + data: Some(data), |
| 47 | + }; |
| 48 | + |
| 49 | + return Ok(Response::new(invoke_response)); |
| 50 | + }; |
| 51 | + |
| 52 | + Ok(Response::new(InvokeResponse::default())) |
| 53 | + } |
| 54 | + |
| 55 | + /// Lists all topics subscribed by this app. |
| 56 | + /// |
| 57 | + /// NOTE: Dapr runtime will call this method to get |
| 58 | + /// the list of topics the app wants to subscribe to. |
| 59 | + /// In this example, the app is subscribing to topic `A`. |
| 60 | + async fn list_topic_subscriptions( |
| 61 | + &self, |
| 62 | + _request: Request<()>, |
| 63 | + ) -> Result<Response<ListTopicSubscriptionsResponse>, Status> { |
| 64 | + let list_subscriptions = ListTopicSubscriptionsResponse::default(); |
| 65 | + Ok(Response::new(list_subscriptions)) |
| 66 | + } |
| 67 | + |
| 68 | + /// Subscribes events from Pubsub. |
| 69 | + async fn on_topic_event( |
| 70 | + &self, |
| 71 | + _request: Request<TopicEventRequest>, |
| 72 | + ) -> Result<Response<TopicEventResponse>, Status> { |
| 73 | + Ok(Response::new(TopicEventResponse::default())) |
| 74 | + } |
| 75 | + |
| 76 | + /// Lists all input bindings subscribed by this app. |
| 77 | + async fn list_input_bindings( |
| 78 | + &self, |
| 79 | + _request: Request<()>, |
| 80 | + ) -> Result<Response<ListInputBindingsResponse>, Status> { |
| 81 | + Ok(Response::new(ListInputBindingsResponse::default())) |
| 82 | + } |
| 83 | + |
| 84 | + /// Listens events from the input bindings. |
| 85 | + async fn on_binding_event( |
| 86 | + &self, |
| 87 | + _request: Request<BindingEventRequest>, |
| 88 | + ) -> Result<Response<BindingEventResponse>, Status> { |
| 89 | + Ok(Response::new(BindingEventResponse::default())) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +#[tokio::main] |
| 94 | +async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 95 | + let server_address = "[::]:50052".parse().unwrap(); |
| 96 | + |
| 97 | + let callback_service = AppCallbackService {}; |
| 98 | + |
| 99 | + println!("AppCallback server listening on: {}", server_address); |
| 100 | + // Create a gRPC server with the callback_service. |
| 101 | + Server::builder() |
| 102 | + .add_service(AppCallbackServer::new(callback_service)) |
| 103 | + .serve(server_address) |
| 104 | + .await?; |
| 105 | + |
| 106 | + Ok(()) |
| 107 | +} |
0 commit comments