Skip to content

Commit 93d1899

Browse files
committed
fix: Breaking changes in Axum
- Swap to new `matchit` syntax for Router - Swap to `axum_extra::extract::Host` - Use `Result<Host, HostRejection>` instead of (no longer working tokio-rs/axum#3170) `Option<Host>` to swallow missing Host header
1 parent f175664 commit 93d1899

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

Cargo.lock

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ opentelemetry = { version = "0.27.1", default-features = false, features = ["tra
6060
opentelemetry_sdk = { version = "0.27.1", default-features = false, features = ["trace"], optional = true }
6161
tracing-core = {version = "0.1.32", optional = true }
6262
prost = {version = "0.13.4", optional = true }
63+
axum-extra = { version = "0.10.0", default-features = false }
6364

6465

6566

src/app.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use axum::{
99
routing::{get, post},
1010
Json, Router,
1111
};
12+
use axum_extra::extract::{rejection::HostRejection, Host};
1213
use http::{header::CACHE_CONTROL, HeaderValue, StatusCode};
1314
use serde::{ser::SerializeMap, Serialize, Serializer};
1415
use tracing::{debug, info_span, Instrument};
@@ -63,17 +64,17 @@ pub fn router(env: Env) -> Router {
6364
get(|| async { Redirect::to(env!("CARGO_PKG_HOMEPAGE")) })
6465
.layer(axum::middleware::from_fn(cache_control_middleware)),
6566
)
66-
.route("/:registry/:namespace/:package/", get(list_package))
67+
.route("/{registry}/{namespace}/{package}/", get(list_package))
6768
.route(
68-
"/:registry/:namespace/:package/json",
69+
"/{registry}/{namespace}/{package}/json",
6970
get(list_package_json),
7071
)
7172
.route(
72-
"/:registry/:namespace/:package/:filename",
73+
"/{registry}/{namespace}/{package}/{filename}",
7374
get(download_package).delete(delete_package_version),
7475
)
7576
.route(
76-
"/:registry/:namespace/",
77+
"/{registry}/{namespace}/",
7778
post(publish_package).layer(DefaultBodyLimit::max(env.body_limit)),
7879
);
7980
let router = match env.path {
@@ -110,7 +111,7 @@ async fn cache_control_middleware(
110111
/// Log incoming requests
111112
async fn accesslog_middleware(
112113
method: axum::http::Method,
113-
host: Option<axum::extract::Host>,
114+
host: Result<Host, HostRejection>,
114115
uri: axum::http::Uri,
115116
headers: axum::http::HeaderMap,
116117
request: axum::extract::Request,
@@ -125,7 +126,7 @@ async fn accesslog_middleware(
125126

126127
tracing::debug!("Accept: {:?}", headers);
127128
tracing::info!(
128-
host = host.map(|value| value.0),
129+
host = host.map(|value| value.0).unwrap_or("".to_string()),
129130
"type" = "request",
130131
status,
131132
method = method.to_string(),

0 commit comments

Comments
 (0)