Skip to content

Commit bef6e3b

Browse files
authored
Upgrade axum to 0.7.4 (#121)
* Upgrade Axum to 0.7.4 Signed-off-by: Cyril Scetbon <[email protected]> * Export Axum crate Signed-off-by: Cyril Scetbon <[email protected]> * Upgrade Axum in macros as well Signed-off-by: Cyril Scetbon <[email protected]> * Upgrade crates that depend on Axum Signed-off-by: Cyril Scetbon <[email protected]> * Use axum from dapr::server::actor Signed-off-by: Cyril Scetbon <[email protected]> * Remove trailing spaces and fix typos Signed-off-by: Cyril Scetbon <[email protected]> * Format file Signed-off-by: Cyril Scetbon <[email protected]> * Upgrade env_logger and toolchain Signed-off-by: Cyril Scetbon <[email protected]> * Trim spaces and remove useless quotes Signed-off-by: Cyril Scetbon <[email protected]> * Use named parameter Signed-off-by: Cyril Scetbon <[email protected]> * Run cargo fmt Signed-off-by: Cyril Scetbon <[email protected]> --------- Signed-off-by: Cyril Scetbon <[email protected]>
1 parent a9df2d9 commit bef6e3b

File tree

10 files changed

+60
-69
lines changed

10 files changed

+60
-69
lines changed

.github/workflows/ci.yml

+5-7
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ on:
1515
env:
1616
CARGO_TERM_COLOR: always
1717
CARGO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
18-
PROTOC_VERSION: '3.x'
19-
RUST_TOOLCHAIN: '1.70.0'
18+
PROTOC_VERSION: 3.x
19+
RUST_TOOLCHAIN: 1.76.0
2020

2121
jobs:
2222
lint:
23-
name: Lint
23+
name: Lint
2424
runs-on: ubuntu-latest
2525

2626
steps:
@@ -42,7 +42,7 @@ jobs:
4242

4343

4444
build:
45-
name: Build
45+
name: Build
4646
runs-on: ubuntu-latest
4747

4848
steps:
@@ -63,7 +63,7 @@ jobs:
6363
run: cargo build --examples
6464
- name: Run Tests
6565
run: cargo test --all-targets
66-
66+
6767
publish:
6868
name: Publish
6969
runs-on: ubuntu-latest
@@ -86,5 +86,3 @@ jobs:
8686
run: cargo publish --manifest-path macros/Cargo.toml --token ${{ env.CARGO_TOKEN }}
8787
- name: cargo publish
8888
run: cargo publish --token ${{ env.CARGO_TOKEN }}
89-
90-

Cargo.toml

+7-8
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,27 @@ description = "Rust SDK for dapr"
99
readme = "README.md"
1010
keywords = ["microservices", "dapr"]
1111

12-
1312
[dependencies]
1413
dapr-macros = {version="0.14.0", path = "macros" }
1514
futures = "0.3"
16-
tonic = "0.8"
17-
prost = "0.11"
15+
tonic = "0.11.0"
16+
prost = "0.12.3"
1817
bytes = "1"
19-
prost-types = "0.11"
18+
prost-types = "0.12.3"
2019
async-trait = "0.1"
21-
env_logger = "0.10"
20+
env_logger = "0.11.2"
2221
log = "0.4"
2322
serde = { version = "1.0", features = ["derive"] }
2423
serde_json = "1.0"
25-
axum = {version = "0.6.19", features = ["default", "headers"] }
24+
axum = "0.7.4"
2625
tokio = { version = "1.29", features = ["sync"] }
2726
chrono = "0.4.24"
2827

2928
[build-dependencies]
30-
tonic-build = "0.8"
29+
tonic-build = "0.11.0"
3130

3231
[dev-dependencies]
33-
axum-test = "12.1.0"
32+
axum-test = "14.3.0"
3433
once_cell = "1.18.0"
3534
tokio = { version = "1", features = ["full"] }
3635
uuid = { version = "1.4.0", features = ["v4"] }

examples/actors/README.md

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Actor Example
22

3-
This example demonstrates the Dapr actor framework. To author an actor,
3+
This example demonstrates the Dapr actor framework. To author an actor,
44

55
1. Create a struc decorated with the `#[dapr::actor]` macro to house your custom actor methods that map to [Axum handlers](https://docs.rs/axum/latest/axum/handler/index.html), use [Axum extractors](https://docs.rs/axum/latest/axum/extract/index.html) to access the incoming request and return an [`impl IntoResponse`](https://docs.rs/axum/latest/axum/response/trait.IntoResponse.html).
66
Use the `DaprJson` extractor to deserialize the request from Json coming from a Dapr sidecar.
@@ -10,24 +10,24 @@ Use the `DaprJson` extractor to deserialize the request from Json coming from a
1010
id: String,
1111
client: ActorContextClient
1212
}
13-
13+
1414
#[derive(Serialize, Deserialize)]
1515
pub struct MyRequest {
1616
pub name: String,
1717
}
18-
18+
1919
#[derive(Serialize, Deserialize)]
2020
pub struct MyResponse {
2121
pub available: bool,
22-
}
22+
}
2323

2424
impl MyActor {
25-
fn do_stuff(&self, DaprJson(data): DaprJson<MyRequest>) -> Json<MyResponse> {
26-
println!("doing stuff with {}", data.name);
27-
Json(MyResponse {
28-
available: true
25+
fn do_stuff(&self, DaprJson(data): DaprJson<MyRequest>) -> Json<MyResponse> {
26+
println!("doing stuff with {}", data.name);
27+
Json(MyResponse {
28+
available: true
2929
})
30-
}
30+
}
3131
}
3232
```
3333

@@ -40,14 +40,14 @@ Use the `DaprJson` extractor to deserialize the request from Json coming from a
4040
1. Implement the `Actor` trait. This trait exposes the following methods:
4141
- `on_activate` - Called when an actor is activated on a host
4242
- `on_deactivate` - Called when an actor is deactivated on a host
43-
- `on_reminder` - Called when a reminder is recieved from the Dapr sidecar
44-
- `on_timer` - Called when a timer is recieved from the Dapr sidecar
43+
- `on_reminder` - Called when a reminder is received from the Dapr sidecar
44+
- `on_timer` - Called when a timer is received from the Dapr sidecar
4545

4646

4747
```rust
4848
#[async_trait]
4949
impl Actor for MyActor {
50-
50+
5151
async fn on_activate(&self) -> Result<(), ActorError> {
5252
println!("on_activate {}", self.id);
5353
Ok(())
@@ -60,18 +60,18 @@ Use the `DaprJson` extractor to deserialize the request from Json coming from a
6060
}
6161
```
6262

63-
1. An actor host requires an Http server to recieve callbacks from the Dapr sidecar. The `DaprHttpServer` object implements this functionality and also encapsulates the actor runtime to service any hosted actors. Use the `register_actor` method to register an actor type to be serviced, this method takes an `ActorTypeRegistration` which specifies
63+
1. An actor host requires an Http server to receive callbacks from the Dapr sidecar. The `DaprHttpServer` object implements this functionality and also encapsulates the actor runtime to service any hosted actors. Use the `register_actor` method to register an actor type to be serviced, this method takes an `ActorTypeRegistration` which specifies
6464
- The actor type name (used by Actor clients), and concrete struct
6565
- A factory to construct a new instance of that actor type when one is required to be activated by the runtime. The parameters passed to the factory will be the actor type, actor ID, and a Dapr client for managing state, timers and reminders for the actor.
6666
- The methods that you would like to expose to external clients.
6767

6868
```rust
6969
let mut dapr_server = dapr::server::DaprHttpServer::new();
7070

71-
dapr_server.register_actor(ActorTypeRegistration::new::<MyActor>("MyActor",
71+
dapr_server.register_actor(ActorTypeRegistration::new::<MyActor>("MyActor",
7272
Box::new(|actor_type, id, client| Arc::new(MyActor{
73-
actor_type,
74-
id,
73+
actor_type,
74+
id,
7575
client
7676
})))
7777
.register_method("do_stuff", MyActor::do_stuff)

examples/actors/server.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use async_trait::async_trait;
2-
use axum::Json;
32
use dapr::server::{
43
actor::{
5-
context_client::ActorContextClient, runtime::ActorTypeRegistration, Actor, ActorError,
4+
axum::Json, context_client::ActorContextClient, runtime::ActorTypeRegistration, Actor,
5+
ActorError,
66
},
77
utils::DaprJson,
88
};

macros/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ proc-macro = true
99
[dependencies]
1010
async-trait = "0.1"
1111
log = "0.4"
12-
axum = "0.6.19"
12+
axum = "0.7.4"
1313
syn = {version="2.0.29",features=["full"]}
1414
quote = "1.0.8"

macros/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ pub fn actor(_attr: TokenStream, item: TokenStream) -> TokenStream {
1515

1616
let mut result = TokenStream::from(quote!(
1717
#[async_trait::async_trait]
18-
impl axum::extract::FromRequestParts<dapr::server::actor::runtime::ActorState> for &#actor_struct_name {
18+
impl dapr::server::actor::axum::extract::FromRequestParts<dapr::server::actor::runtime::ActorState> for &#actor_struct_name {
1919
type Rejection = dapr::server::actor::ActorRejection;
2020

2121
async fn from_request_parts(
22-
parts: &mut axum::http::request::Parts,
22+
parts: &mut dapr::server::actor::axum::http::request::Parts,
2323
state: &dapr::server::actor::runtime::ActorState,
2424
) -> Result<Self, Self::Rejection> {
25-
let path = match axum::extract::Path::<dapr::server::actor::ActorPath>::from_request_parts(parts, state).await {
25+
let path = match dapr::server::actor::axum::extract::Path::<dapr::server::actor::ActorPath>::from_request_parts(parts, state).await {
2626
Ok(path) => path,
2727
Err(e) => {
2828
log::error!("Error getting path: {}", e);

src/server/actor/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use std::{error::Error, fmt::Display, sync::Arc};
55

66
use self::context_client::ActorContextClient;
77

8+
pub use axum;
9+
810
pub mod context_client;
911
pub mod runtime;
1012

src/server/actor/tests.rs

+16-22
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
use std::{
2-
collections::HashMap,
3-
net::{SocketAddr, TcpListener},
4-
sync::Arc,
5-
};
1+
use std::{collections::HashMap, sync::Arc};
62

73
use async_trait::async_trait;
84
use axum::{Json, Router};
@@ -15,7 +11,7 @@ use dapr_macros::actor;
1511
use once_cell::sync::Lazy;
1612
use serde::{Deserialize, Serialize};
1713
use serde_json::json;
18-
use tokio::sync::Mutex;
14+
use tokio::{net::TcpListener, sync::Mutex};
1915
use uuid::Uuid;
2016

2117
#[derive(Serialize, Deserialize, Debug, PartialEq)]
@@ -68,13 +64,13 @@ impl MyActor {
6864

6965
#[tokio::test]
7066
async fn test_actor_invoke() {
71-
let dapr_port = get_available_port().unwrap();
67+
let dapr_port = get_available_port().await.unwrap();
7268

7369
let fake_sidecar = tokio::spawn(async move {
7470
let sidecar = Router::new();
75-
_ = axum::Server::bind(&SocketAddr::from(([127, 0, 0, 1], dapr_port)))
76-
.serve(sidecar.into_make_service())
77-
.await;
71+
let address = format!("127.0.0.1:{dapr_port}");
72+
let listener = TcpListener::bind(address).await.unwrap();
73+
_ = axum::serve(listener, sidecar.into_make_service()).await;
7874
});
7975
tokio::task::yield_now().await;
8076

@@ -140,13 +136,13 @@ async fn test_actor_invoke() {
140136

141137
#[tokio::test]
142138
async fn test_actor_deactivate() {
143-
let dapr_port = get_available_port().unwrap();
139+
let dapr_port = get_available_port().await.unwrap();
144140

145141
let fake_sidecar = tokio::spawn(async move {
146142
let sidecar = Router::new();
147-
_ = axum::Server::bind(&SocketAddr::from(([127, 0, 0, 1], dapr_port)))
148-
.serve(sidecar.into_make_service())
149-
.await;
143+
let address = format!("127.0.0.1:{dapr_port}");
144+
let listener = TcpListener::bind(address).await.unwrap();
145+
_ = axum::serve(listener, sidecar.into_make_service()).await;
150146
});
151147
tokio::task::yield_now().await;
152148

@@ -246,13 +242,11 @@ impl TestState {
246242

247243
static TEST_STATE: Lazy<TestState> = Lazy::new(TestState::new);
248244

249-
fn get_available_port() -> Option<u16> {
250-
(8000..9000).find(|port| port_is_available(*port))
251-
}
252-
253-
fn port_is_available(port: u16) -> bool {
254-
match TcpListener::bind(("127.0.0.1", port)) {
255-
Ok(_) => true,
256-
Err(_) => false,
245+
async fn get_available_port() -> Option<u16> {
246+
for port in 8000..9000 {
247+
if TcpListener::bind(format!("127.0.0.1:{port}")).await.is_ok() {
248+
return Some(port);
249+
}
257250
}
251+
None
258252
}

src/server/http.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use axum::{
66
Json, Router,
77
};
88
use futures::{Future, FutureExt};
9-
use std::{net::SocketAddr, pin::Pin, sync::Arc};
9+
use std::{pin::Pin, sync::Arc};
10+
use tokio::net::TcpListener;
1011

1112
use super::super::client::TonicClient;
1213
use super::actor::runtime::{ActorRuntime, ActorTypeRegistration};
@@ -136,9 +137,10 @@ impl DaprHttpServer {
136137
.parse()
137138
.unwrap_or(8080);
138139

139-
let addr = SocketAddr::from(([127, 0, 0, 1], port.unwrap_or(default_port)));
140+
let address = format!("127.0.0.1:{}", port.unwrap_or(default_port));
141+
let listener = TcpListener::bind(address).await.unwrap();
140142

141-
let server = axum::Server::bind(&addr).serve(app.into_make_service());
143+
let server = axum::serve(listener, app.into_make_service());
142144

143145
let final_result = match self.shutdown_signal.take() {
144146
Some(signal) => {

src/server/utils.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use async_trait::async_trait;
22
use axum::{
3-
body::HttpBody,
3+
body::Body,
44
extract::FromRequest,
55
http::{Request, StatusCode},
66
response::IntoResponse,
7-
BoxError,
87
};
98
use serde::de::DeserializeOwned;
109

@@ -18,17 +17,14 @@ pub enum JsonRejection {
1817
}
1918

2019
#[async_trait]
21-
impl<T, S, B> FromRequest<S, B> for DaprJson<T>
20+
impl<T, S> FromRequest<S> for DaprJson<T>
2221
where
2322
T: DeserializeOwned,
24-
B: HttpBody + Send + 'static,
25-
B::Data: Send,
26-
B::Error: Into<BoxError>,
2723
S: Send + Sync,
2824
{
2925
type Rejection = JsonRejection;
3026

31-
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
27+
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
3228
let bytes = match axum::body::Bytes::from_request(req, state).await {
3329
Ok(bytes) => bytes,
3430
Err(e) => {

0 commit comments

Comments
 (0)