Skip to content

Commit a97fa1b

Browse files
authored
Merge pull request #640 from swimos/dependabot/cargo/axum-0.7.5
Update axum requirement from 0.6.20 to 0.7.5
2 parents cabecec + 174f2d9 commit a97fa1b

File tree

5 files changed

+36
-22
lines changed

5 files changed

+36
-22
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ duration-str = "0.11.2"
124124
quick-xml = "0.34.0"
125125
csv = "1.2"
126126
serde-xml-rs = "0.6"
127-
axum = "0.6.20"
127+
axum = "0.7.5"
128128
hyper-staticfile = "0.9"
129129
httparse = "1.8"
130130
sha-1 = "0.10.1"

example_apps/transit/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2021"
66
[dependencies]
77
swimos = { path = "../../swimos", features = ["server", "agent"] }
88
swimos_form = { path = "../../api/swimos_form" }
9+
swimos_utilities = { path = "../../swimos_utilities", features = ["trigger"] }
910
tokio = { workspace = true, features = ["rt-multi-thread", "macros", "sync", "io-util", "fs"] }
1011
tokio-stream = { workspace = true, features = ["io-util"] }
1112
tokio-util = { workspace = true, features = ["io"] }

example_apps/transit/src/bin/ui.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::{error::Error, pin::pin, sync::Arc, time::Duration};
15+
use std::future::IntoFuture;
16+
use std::{error::Error, pin::pin, time::Duration};
1617

1718
use clap::Parser;
1819
use futures::future::{select, Either};
19-
use tokio::sync::Notify;
20+
use tokio::net::TcpListener;
21+
22+
use swimos_utilities::trigger::trigger;
2023
use transit::ui::ui_server_router;
2124

2225
const SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(1);
@@ -32,19 +35,24 @@ async fn ui_server(
3235
shutdown_timeout: Duration,
3336
) -> Result<(), Box<dyn Error + Send + Sync>> {
3437
let app = ui_server_router(port);
35-
let server = axum::Server::try_bind(&"0.0.0.0:0".parse()?)?.serve(app.into_make_service());
36-
let ui_addr = server.local_addr();
38+
let (stop_tx, stop_rx) = trigger();
39+
40+
let listener = TcpListener::bind("0.0.0.0:0").await?;
41+
let ui_addr = listener.local_addr()?;
3742
println!("UI bound to: {}", ui_addr);
38-
let stop_tx = Arc::new(Notify::new());
39-
let stop_rx = stop_tx.clone();
40-
let server_task = pin!(server.with_graceful_shutdown(stop_rx.notified()));
43+
44+
let server =
45+
axum::serve(listener, app.into_make_service()).with_graceful_shutdown(async move {
46+
let _r = stop_rx.await;
47+
});
48+
let server_task = pin!(server.into_future());
4149
let shutdown_notified = pin!(async move {
4250
let _ = tokio::signal::ctrl_c().await;
4351
});
4452
match select(server_task, shutdown_notified).await {
4553
Either::Left((result, _)) => result?,
4654
Either::Right((_, server)) => {
47-
stop_tx.notify_one();
55+
assert!(stop_tx.trigger());
4856
tokio::time::timeout(shutdown_timeout, server).await??;
4957
}
5058
}

example_apps/transit/src/main.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use std::future::IntoFuture;
1516
use std::{error::Error, net::SocketAddr, pin::pin, sync::Arc, time::Duration};
1617

1718
use clap::Parser;
@@ -22,6 +23,8 @@ use swimos::{
2223
route::RouteUri,
2324
server::{Server, ServerBuilder},
2425
};
26+
use swimos_utilities::trigger::trigger;
27+
use tokio::net::TcpListener;
2528
use tokio::sync::{oneshot, Notify};
2629
use tracing_subscriber::filter::LevelFilter;
2730
use transit::start_agencies_and_wait;
@@ -74,20 +77,24 @@ async fn ui_server(
7477
) -> Result<(), Box<dyn Error + Send + Sync>> {
7578
if let Ok(addr) = swim_addr_rx.await {
7679
let app = ui_server_router(addr.port());
80+
let bind_to: SocketAddr = format!("0.0.0.0:{}", port.unwrap_or_default()).parse()?;
81+
let (stop_tx, stop_rx) = trigger();
7782

78-
let bind_to = format!("0.0.0.0:{}", port.unwrap_or_default()).parse()?;
79-
80-
let server = axum::Server::try_bind(&bind_to)?.serve(app.into_make_service());
81-
let ui_addr = server.local_addr();
83+
let listener = TcpListener::bind(bind_to).await?;
84+
let ui_addr = listener.local_addr()?;
8285
println!("UI bound to: {}", ui_addr);
83-
let stop_tx = Arc::new(Notify::new());
84-
let stop_rx = stop_tx.clone();
85-
let server_task = pin!(server.with_graceful_shutdown(stop_rx.notified()));
86+
87+
let server =
88+
axum::serve(listener, app.into_make_service()).with_graceful_shutdown(async move {
89+
let _ = stop_rx.await;
90+
});
91+
92+
let server_task = pin!(server.into_future());
8693
let shutdown_notified = pin!(shutdown_signal.notified());
8794
match select(server_task, shutdown_notified).await {
8895
Either::Left((result, _)) => result?,
8996
Either::Right((_, server)) => {
90-
stop_tx.notify_one();
97+
assert!(stop_tx.trigger());
9198
tokio::time::timeout(shutdown_timeout, server).await??;
9299
}
93100
}

example_apps/transit/src/ui.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use axum::body::Body;
1516
use axum::http::{header, HeaderValue};
16-
use axum::{
17-
body::StreamBody, extract::State, http::StatusCode, response::IntoResponse, routing::get,
18-
Router,
19-
};
17+
use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::get, Router};
2018
use futures::{TryFutureExt, TryStreamExt};
2119
use tokio::fs::File;
2220
use tokio::io::{AsyncBufReadExt, BufReader};
@@ -102,7 +100,7 @@ async fn load_file(path: &str) -> impl IntoResponse {
102100
.await
103101
{
104102
let headers = [(header::CONTENT_LENGTH, HeaderValue::from(len))];
105-
Ok((headers, StreamBody::new(ReaderStream::new(file))))
103+
Ok((headers, Body::from_stream(ReaderStream::new(file))))
106104
} else {
107105
Err((StatusCode::NOT_FOUND, format!("File not found: {}", target)))
108106
}

0 commit comments

Comments
 (0)