Skip to content

Commit f3e6c72

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 e580ce8 commit f3e6c72

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-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

+8-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use axum::{
77
routing::{get, post},
88
Json, Router,
99
};
10+
use axum_extra::extract::{rejection::HostRejection, Host};
1011
use http::{header::CACHE_CONTROL, HeaderValue, StatusCode};
1112
use serde::{ser::SerializeMap, Serialize, Serializer};
1213
use tracing::{info_span, Instrument};
@@ -60,17 +61,17 @@ pub fn router(subpath: Option<String>, body_limit: usize) -> Router {
6061
get(|| async { Redirect::to(env!("CARGO_PKG_HOMEPAGE")) })
6162
.layer(axum::middleware::from_fn(cache_control_middleware)),
6263
)
63-
.route("/:registry/:namespace/:package/", get(list_package))
64+
.route("/{registry}/{namespace}/{package}/", get(list_package))
6465
.route(
65-
"/:registry/:namespace/:package/json",
66+
"/{registry}/{namespace}/{package}/json",
6667
get(list_package_json),
6768
)
6869
.route(
69-
"/:registry/:namespace/:package/:filename",
70+
"/{registry}/{namespace}/{package}/{filename}",
7071
get(download_package).delete(delete_package_version),
7172
)
7273
.route(
73-
"/:registry/:namespace/",
74+
"/{registry}/{namespace}/",
7475
post(publish_package).layer(DefaultBodyLimit::max(body_limit)),
7576
);
7677
let router = match subpath {
@@ -104,7 +105,7 @@ async fn cache_control_middleware(
104105
/// Log incoming requests
105106
async fn accesslog_middleware(
106107
method: axum::http::Method,
107-
host: Option<axum::extract::Host>,
108+
host: Result<Host, HostRejection>,
108109
uri: axum::http::Uri,
109110
headers: axum::http::HeaderMap,
110111
request: axum::extract::Request,
@@ -116,8 +117,9 @@ async fn accesslog_middleware(
116117
let user_agent = headers
117118
.get("user-agent")
118119
.map(|ua| ua.to_str().unwrap_or(""));
120+
119121
tracing::info!(
120-
host = host.map(|value| value.0),
122+
host = host.map(|value| value.0).unwrap_or("".to_string()),
121123
"type" = "request",
122124
status,
123125
method = method.to_string(),

0 commit comments

Comments
 (0)