|
1 |
| -use tonic::{transport::Server, Request, Response, Status}; |
| 1 | +use dapr_macros::topic; |
| 2 | +use tonic::transport::Server; |
2 | 3 |
|
| 4 | +use dapr::appcallback::AppCallbackService; |
| 5 | +use dapr::serde::{Deserialize, Serialize}; |
3 | 6 | use dapr::{
|
4 |
| - appcallback::*, |
5 |
| - dapr::dapr::proto::runtime::v1::app_callback_server::{AppCallback, AppCallbackServer}, |
| 7 | + appcallback::*, dapr::dapr::proto::runtime::v1::app_callback_server::AppCallbackServer, |
6 | 8 | };
|
7 | 9 |
|
8 |
| -#[derive(Default)] |
9 |
| -pub struct AppCallbackService {} |
10 |
| - |
11 |
| -#[tonic::async_trait] |
12 |
| -impl AppCallback for AppCallbackService { |
13 |
| - /// Invokes service method with InvokeRequest. |
14 |
| - async fn on_invoke( |
15 |
| - &self, |
16 |
| - _request: Request<InvokeRequest>, |
17 |
| - ) -> Result<Response<InvokeResponse>, Status> { |
18 |
| - Ok(Response::new(InvokeResponse::default())) |
19 |
| - } |
20 |
| - |
21 |
| - /// Lists all topics subscribed by this app. |
22 |
| - /// |
23 |
| - /// NOTE: Dapr runtime will call this method to get |
24 |
| - /// the list of topics the app wants to subscribe to. |
25 |
| - /// In this example, the app is subscribing to topic `A`. |
26 |
| - async fn list_topic_subscriptions( |
27 |
| - &self, |
28 |
| - _request: Request<()>, |
29 |
| - ) -> Result<Response<ListTopicSubscriptionsResponse>, Status> { |
30 |
| - let topic = "A".to_string(); |
31 |
| - let pubsub_name = "pubsub".to_string(); |
32 |
| - |
33 |
| - let list_subscriptions = ListTopicSubscriptionsResponse::topic(pubsub_name, topic); |
34 |
| - |
35 |
| - Ok(Response::new(list_subscriptions)) |
36 |
| - } |
37 |
| - |
38 |
| - /// Subscribes events from Pubsub. |
39 |
| - async fn on_topic_event( |
40 |
| - &self, |
41 |
| - request: Request<TopicEventRequest>, |
42 |
| - ) -> Result<Response<TopicEventResponse>, Status> { |
43 |
| - let r = request.into_inner(); |
44 |
| - let data = &r.data; |
45 |
| - let data_content_type = &r.data_content_type; |
46 |
| - |
47 |
| - let message = String::from_utf8_lossy(data); |
48 |
| - println!("Message: {}", &message); |
49 |
| - println!("Content-Type: {}", &data_content_type); |
| 10 | +#[derive(Serialize, Deserialize, Debug)] |
| 11 | +struct Order { |
| 12 | + pub order_number: i32, |
| 13 | + pub order_details: String, |
| 14 | +} |
50 | 15 |
|
51 |
| - Ok(Response::new(TopicEventResponse::default())) |
52 |
| - } |
| 16 | +#[derive(Serialize, Deserialize, Debug)] |
| 17 | +struct Refund { |
| 18 | + pub order_number: i32, |
| 19 | + pub refund_amount: i32, |
| 20 | +} |
53 | 21 |
|
54 |
| - /// Lists all input bindings subscribed by this app. |
55 |
| - async fn list_input_bindings( |
56 |
| - &self, |
57 |
| - _request: Request<()>, |
58 |
| - ) -> Result<Response<ListInputBindingsResponse>, Status> { |
59 |
| - Ok(Response::new(ListInputBindingsResponse::default())) |
60 |
| - } |
| 22 | +#[topic(pub_sub_name = "pubsub", topic = "A")] |
| 23 | +async fn handle_a_event(order: Order) { |
| 24 | + println!("Topic A - {:#?}", order) |
| 25 | +} |
61 | 26 |
|
62 |
| - /// Listens events from the input bindings. |
63 |
| - async fn on_binding_event( |
64 |
| - &self, |
65 |
| - _request: Request<BindingEventRequest>, |
66 |
| - ) -> Result<Response<BindingEventResponse>, Status> { |
67 |
| - Ok(Response::new(BindingEventResponse::default())) |
68 |
| - } |
| 27 | +#[topic(pub_sub_name = "pubsub", topic = "B")] |
| 28 | +async fn handle_b_event(refund: Refund) { |
| 29 | + println!("Topic B - {:#?}", refund) |
69 | 30 | }
|
70 | 31 |
|
71 | 32 | #[tokio::main]
|
72 | 33 | async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
73 |
| - let addr = "[::]:50051".parse().unwrap(); |
| 34 | + let addr = "127.0.0.1:50051".parse().unwrap(); |
| 35 | + |
| 36 | + let mut callback_service = AppCallbackService::new(); |
| 37 | + |
| 38 | + callback_service.add_handler(HandleAEvent::default().get_handler()); |
74 | 39 |
|
75 |
| - let callback_service = AppCallbackService::default(); |
| 40 | + callback_service.add_handler(HandleBEvent::default().get_handler()); |
76 | 41 |
|
77 | 42 | println!("AppCallback server listening on: {}", addr);
|
78 | 43 |
|
|
0 commit comments