forked from dapr/rust-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.rs
330 lines (300 loc) · 9.99 KB
/
http.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
use axum::{
extract::{Path, State},
http::StatusCode,
response::IntoResponse,
routing::{delete, get, put},
Json, Router,
};
use futures::{Future, FutureExt};
use std::{pin::Pin, sync::Arc};
use tokio::net::TcpListener;
use super::super::client::TonicClient;
use super::actor::runtime::{ActorRuntime, ActorTypeRegistration};
/// The Dapr HTTP server.
///
/// Supports Http callbacks from the Dapr sidecar.
///
/// # Example:
/// ```ignore
/// # use std::sync::Arc;
/// # use dapr::server::actor::{context_client::ActorContextClient, Actor, ActorError, ActorFactory, runtime::ActorTypeRegistration};
/// # use dapr::server::utils::DaprJson;
/// # use dapr::actor;
/// # use axum::{Json, Router};
/// # use serde::{Deserialize, Serialize};
/// # #[actor]
/// # struct MyActor {
/// # id: String,
/// # client: ActorContextClient,
/// # }
/// #
/// # #[async_trait::async_trait]
/// # impl Actor for MyActor {
/// # async fn on_activate(&self) -> Result<(), ActorError> {
/// # todo!()
/// # }
/// # async fn on_deactivate(&self) -> Result<(), ActorError> {
/// # todo!()
/// # }
/// # async fn on_reminder(&self, reminder_name: &str, data: Vec<u8>) -> Result<(), ActorError> {
/// # todo!()
/// # }
/// # async fn on_timer(&self, timer_name: &str, data: Vec<u8>) -> Result<(), ActorError> {
/// # todo!()
/// # }
/// # }
/// ##[derive(Serialize, Deserialize)]
/// pub struct MyRequest {
/// pub name: String,
/// }
///
///##[derive(Serialize, Deserialize)]
///pub struct MyResponse {
/// pub available: bool,
///}
///
///impl MyActor {
/// fn do_stuff(&self, DaprJson(data): DaprJson<MyRequest>) -> Json<MyResponse> {
/// println!("doing stuff with {}", data.name);
/// Json(MyResponse {
/// available: true
/// })
/// }
///}
/// # async fn main_async() {
/// let mut dapr_server = dapr::server::DaprHttpServer::new().await;
///
/// dapr_server.register_actor(ActorTypeRegistration::new::<MyActor>("MyActor", Box::new(|_actor_type, actor_id, context| {
/// Arc::new(MyActor {
/// id: actor_id.to_string(),
/// client: context,
/// })}))
/// .register_method("do_stuff", MyActor::do_stuff))
/// .await;
///
/// dapr_server.start(None).await;
/// # }
/// ```
pub struct DaprHttpServer {
actor_runtime: Arc<ActorRuntime>,
shutdown_signal: Option<Pin<Box<dyn Future<Output = ()> + Send>>>,
}
impl DaprHttpServer {
/// Creates a new instance of the Dapr HTTP server with default options.
///
/// # Panics
///
/// This function panics if the Dapr Sidecar cannot be reached!
/// For a non-panicking version that allows you to handle any errors yourself, see:
/// [DaprHttpServer::try_new_with_dapr_port]
pub async fn new() -> Self {
let dapr_port: u16 = std::env::var("DAPR_GRPC_PORT")
.unwrap_or("3501".into())
.parse()
.unwrap();
Self::with_dapr_port(dapr_port).await
}
/// Creates a new instance of the Dapr HTTP server that connects to the Dapr sidecar on the
/// given dapr_port.
///
/// # Panics
///
/// This function panics if the Dapr Sidecar cannot be reached!
/// For a non-panicking version that allows you to handle any errors yourself, see:
/// [DaprHttpServer::try_new_with_dapr_port]
pub async fn with_dapr_port(dapr_port: u16) -> Self {
match Self::try_new_with_dapr_port(dapr_port).await {
Ok(c) => c,
Err(err) => panic!("failed to connect to dapr: {}", err),
}
}
/// Creates a new instance of the Dapr HTTP server that connects to the Dapr sidecar on the
/// given dapr_port.
///
/// In contrast to the other functions that create a DaprHttpServer, this function does
/// not panic, but instead returns a Result.
pub async fn try_new_with_dapr_port(
dapr_port: u16,
) -> Result<Self, Box<dyn std::error::Error>> {
let dapr_addr = format!("https://127.0.0.1:{}", dapr_port);
let cc = TonicClient::connect(dapr_addr).await?;
let rt = ActorRuntime::new(cc);
Ok(DaprHttpServer {
actor_runtime: Arc::new(rt),
shutdown_signal: None,
})
}
pub fn with_graceful_shutdown<F>(self, signal: F) -> Self
where
F: Future<Output = ()> + Send + 'static,
{
DaprHttpServer {
shutdown_signal: Some(signal.boxed()),
..self
}
}
/// Registers an actor type with the Dapr runtime.
///
/// # Arguments:
/// * `registration` - The [ActorTypeRegistration] struct, carries the methods that can be invoked on it and the factory to create instances of it.
pub async fn register_actor(&self, registration: ActorTypeRegistration) {
self.actor_runtime.register_actor(registration).await;
}
/// Starts the Dapr HTTP server.
///
/// # Arguments:
/// * `port` - The port to listen on. If not specified, the APP_PORT environment variable will be used. If that is not specified, 8080 will be used.
pub async fn start(&mut self, port: Option<u16>) -> Result<(), Box<dyn std::error::Error>> {
let app = self.build_router().await;
let default_port: u16 = std::env::var("APP_PORT")
.unwrap_or(String::from("8080"))
.parse()
.unwrap_or(8080);
let address = format!("127.0.0.1:{}", port.unwrap_or(default_port));
let listener = TcpListener::bind(address).await?;
let server = axum::serve(listener, app.into_make_service());
let final_result = match self.shutdown_signal.take() {
Some(signal) => {
server
.with_graceful_shutdown(async move {
signal.await;
})
.await
}
None => server.await,
};
self.actor_runtime.deactivate_all().await;
Ok(final_result?)
}
pub async fn build_test_router(&mut self) -> Router {
self.build_router().await
}
async fn build_router(&mut self) -> Router {
let rt = self.actor_runtime.clone();
let app = Router::new()
.route("/healthz", get(health_check))
.route(
"/dapr/config",
get(registered_actors).with_state(rt.clone()),
)
.route(
"/actors/:actor_type/:actor_id",
delete(deactivate_actor).with_state(rt.clone()),
)
.route(
"/actors/:actor_type/:actor_id/method/remind/:reminder_name",
put(invoke_reminder).with_state(rt.clone()),
)
.route(
"/actors/:actor_type/:actor_id/method/timer/:timer_name",
put(invoke_timer).with_state(rt.clone()),
);
self.actor_runtime
.configure_method_routes(app, rt.clone())
.await
}
}
async fn health_check() -> impl IntoResponse {
log::debug!("recieved health check request");
StatusCode::OK
}
async fn registered_actors(State(runtime): State<Arc<ActorRuntime>>) -> impl IntoResponse {
log::debug!("daprd requested registered actors");
let ra = runtime.list_registered_actors().await;
let result = super::models::RegisteredActorsResponse { entities: ra };
Json(result)
}
async fn deactivate_actor(
State(runtime): State<Arc<ActorRuntime>>,
Path((actor_type, actor_id)): Path<(String, String)>,
) -> impl IntoResponse {
match runtime.deactivate_actor(&actor_type, &actor_id).await {
Ok(_) => StatusCode::OK,
Err(err) => {
log::error!("invoke_actor: {:?}", err);
match err {
super::actor::ActorError::ActorNotFound => StatusCode::NOT_FOUND,
_ => {
log::error!("deactivate_actor: {:?}", err);
StatusCode::INTERNAL_SERVER_ERROR
}
}
}
}
}
async fn invoke_reminder(
State(runtime): State<Arc<ActorRuntime>>,
Path((actor_type, actor_id, reminder_name)): Path<(String, String, String)>,
Json(payload): Json<ReminderPayload>,
) -> impl IntoResponse {
log::debug!(
"invoke_reminder: {} {} {} {:?}",
actor_type,
actor_id,
reminder_name,
payload
);
match runtime
.invoke_reminder(
&actor_type,
&actor_id,
&reminder_name,
payload.data.unwrap_or_default().into_bytes(),
)
.await
{
Ok(_output) => StatusCode::OK,
Err(err) => {
log::error!("invoke_actor: {:?}", err);
match err {
super::actor::ActorError::ActorNotFound => StatusCode::NOT_FOUND,
_ => {
log::error!("invoke_reminder: {:?}", err);
StatusCode::INTERNAL_SERVER_ERROR
}
}
}
}
}
async fn invoke_timer(
State(runtime): State<Arc<ActorRuntime>>,
Path((actor_type, actor_id, timer_name)): Path<(String, String, String)>,
Json(payload): Json<TimerPayload>,
) -> impl IntoResponse {
log::debug!(
"invoke_timer: {} {} {}, {:?}",
actor_type,
actor_id,
timer_name,
payload
);
match runtime
.invoke_timer(
&actor_type,
&actor_id,
&timer_name,
payload.data.unwrap_or_default().into_bytes(),
)
.await
{
Ok(_output) => StatusCode::OK,
Err(err) => {
log::error!("invoke_actor: {:?}", err);
match err {
super::actor::ActorError::ActorNotFound => StatusCode::NOT_FOUND,
_ => {
log::error!("invoke_timer: {:?}", err);
StatusCode::INTERNAL_SERVER_ERROR
}
}
}
}
}
#[derive(serde::Deserialize, Debug)]
struct ReminderPayload {
data: Option<String>,
}
#[derive(serde::Deserialize, Debug)]
struct TimerPayload {
data: Option<String>,
}