Skip to content

Commit f3e8195

Browse files
authored
fix(common): 404 on paths with trailing / (#206)
Seen affecting the "misc routes", such as `/version/` Fixes #205 Signed-off-by: Alexis Asseman <[email protected]>
1 parent 4f60713 commit f3e8195

File tree

2 files changed

+41
-40
lines changed

2 files changed

+41
-40
lines changed

common/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ build-info = "0.0.34"
4040
autometrics = { version = "1.0.1", features = ["prometheus-exporter"] }
4141
tracing = "0.1.40"
4242
tower_governor = "0.3.2"
43-
tower-http = { version = "0.5.2", features = ["trace", "cors"] }
43+
tower-http = { version = "0.5.2", features = ["trace", "cors", "normalize-path"] }
4444
tokio-util = "0.7.10"
4545
bigdecimal = "0.4.2"
4646
thegraph-core = { version = "0.4.1", features = ["subgraph-client"] }

common/src/indexer_service/http/indexer_service.rs

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ use alloy_sol_types::eip712_domain;
1010
use anyhow;
1111
use autometrics::prometheus_exporter;
1212
use axum::extract::MatchedPath;
13+
use axum::extract::Request as ExtractRequest;
1314
use axum::http::{Method, Request};
14-
use axum::serve;
1515
use axum::{
1616
async_trait,
1717
response::{IntoResponse, Response},
1818
routing::{get, post},
1919
Extension, Json, Router,
2020
};
21+
use axum::{serve, ServiceExt};
2122
use build_info::BuildInfo;
2223
use eventuals::Eventual;
2324
use reqwest::StatusCode;
@@ -30,9 +31,7 @@ use thiserror::Error;
3031
use tokio::net::TcpListener;
3132
use tokio::signal;
3233
use tower_governor::{governor::GovernorConfigBuilder, GovernorLayer};
33-
use tower_http::cors;
34-
use tower_http::cors::CorsLayer;
35-
use tower_http::trace::TraceLayer;
34+
use tower_http::{cors, cors::CorsLayer, normalize_path::NormalizePath, trace::TraceLayer};
3635
use tracing::{info, info_span};
3736

3837
use crate::{
@@ -385,40 +384,42 @@ impl IndexerService {
385384
)
386385
.with_state(state.clone());
387386

388-
let router = misc_routes
389-
.merge(data_routes)
390-
.merge(options.extra_routes)
391-
.layer(
392-
CorsLayer::new()
393-
.allow_origin(cors::Any)
394-
.allow_headers(cors::Any)
395-
.allow_methods([Method::OPTIONS, Method::POST, Method::GET]),
396-
)
397-
.layer(
398-
TraceLayer::new_for_http()
399-
.make_span_with(|req: &Request<_>| {
400-
let method = req.method();
401-
let uri = req.uri();
402-
let matched_path = req
403-
.extensions()
404-
.get::<MatchedPath>()
405-
.map(MatchedPath::as_str);
406-
407-
info_span!(
408-
"http_request",
409-
%method,
410-
%uri,
411-
matched_path,
412-
)
413-
})
414-
// we disable failures here because we doing our own error logging
415-
.on_failure(
416-
|_error: tower_http::classify::ServerErrorsFailureClass,
417-
_latency: Duration,
418-
_span: &tracing::Span| {},
419-
),
420-
)
421-
.with_state(state);
387+
let router = NormalizePath::trim_trailing_slash(
388+
misc_routes
389+
.merge(data_routes)
390+
.merge(options.extra_routes)
391+
.layer(
392+
CorsLayer::new()
393+
.allow_origin(cors::Any)
394+
.allow_headers(cors::Any)
395+
.allow_methods([Method::OPTIONS, Method::POST, Method::GET]),
396+
)
397+
.layer(
398+
TraceLayer::new_for_http()
399+
.make_span_with(|req: &Request<_>| {
400+
let method = req.method();
401+
let uri = req.uri();
402+
let matched_path = req
403+
.extensions()
404+
.get::<MatchedPath>()
405+
.map(MatchedPath::as_str);
406+
407+
info_span!(
408+
"http_request",
409+
%method,
410+
%uri,
411+
matched_path,
412+
)
413+
})
414+
// we disable failures here because we doing our own error logging
415+
.on_failure(
416+
|_error: tower_http::classify::ServerErrorsFailureClass,
417+
_latency: Duration,
418+
_span: &tracing::Span| {},
419+
),
420+
)
421+
.with_state(state),
422+
);
422423

423424
Self::serve_metrics(options.config.server.metrics_host_and_port);
424425

@@ -432,7 +433,7 @@ impl IndexerService {
432433

433434
Ok(serve(
434435
listener,
435-
router.into_make_service_with_connect_info::<SocketAddr>(),
436+
ServiceExt::<ExtractRequest>::into_make_service_with_connect_info::<SocketAddr>(router),
436437
)
437438
.with_graceful_shutdown(shutdown_signal())
438439
.await?)

0 commit comments

Comments
 (0)