From 0919ccf4ce41892fdb471da73e38dc0ee077848b Mon Sep 17 00:00:00 2001 From: Szepesi Tibor Date: Sun, 8 Dec 2024 03:25:55 +0100 Subject: [PATCH] Trim trailing slashes (#101) --- iam/Cargo.toml | 2 +- iam/lib.rs | 24 ++++++++++-------------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/iam/Cargo.toml b/iam/Cargo.toml index b0b7007..004e202 100644 --- a/iam/Cargo.toml +++ b/iam/Cargo.toml @@ -28,7 +28,7 @@ async-trait.workspace = true futures-util = "0.3.30" tokio.workspace = true tower = { version = "0.4.13", features = ["timeout"] } -tower-http = { version = "0.5.2", features = ["add-extension", "auth", "compression-full", "cors", "decompression-full", "request-id", "sensitive-headers", "trace", "util"] } +tower-http = { version = "0.5.2", features = ["add-extension", "auth", "compression-full", "cors", "decompression-full", "request-id", "sensitive-headers", "trace", "util", "normalize-path"] } hyper = "1.4.1" http-body = "1.0.1" iam-common.workspace = true diff --git a/iam/lib.rs b/iam/lib.rs index b3020af..7c19be6 100644 --- a/iam/lib.rs +++ b/iam/lib.rs @@ -9,10 +9,11 @@ mod utils; use axum::{ error_handling::HandleErrorLayer, + extract::Request, http::{header::AUTHORIZATION, StatusCode}, middleware, response::{IntoResponse, Response}, - BoxError, Router, + BoxError, Router, ServiceExt, }; use middlewares::TraceRequestIdLayer; use shared::{Shared, SharedTrait}; @@ -25,10 +26,7 @@ use std::{ }; use tokio::net::TcpListener; use tower::{timeout::error::Elapsed, ServiceBuilder}; -use tower_http::{ - cors::{Any, CorsLayer}, - ServiceBuilderExt, -}; +use tower_http::{cors::CorsLayer, normalize_path::NormalizePath, ServiceBuilderExt}; use utils::MakeUuidRequestId; async fn handle_error(err: BoxError) -> Response { @@ -41,11 +39,6 @@ async fn handle_error(err: BoxError) -> Response { } fn middlewares(shared: S, router: Router) -> Router { - let cors_layer = CorsLayer::new() - .allow_origin(Any) - .allow_methods(Any) - .allow_headers(Any); - let middlewares = ServiceBuilder::new() .layer(HandleErrorLayer::new(handle_error)) .timeout(Duration::from_secs(10)) @@ -55,7 +48,7 @@ fn middlewares(shared: S, router: Router) -> Router { .layer(TraceRequestIdLayer) .compression() .decompression() - .layer(cors_layer) + .layer(CorsLayer::permissive()) .add_extension(shared) .layer(middleware::from_fn(auth::get_claims::)) .propagate_x_request_id() @@ -69,14 +62,17 @@ pub async fn run() -> Result<(), Box> { let shared = shared::create_shared().await; let app = middlewares(shared, handlers::routes::()); + let app = NormalizePath::trim_trailing_slash(app); tracing::info!("Listening on port {}", addr.port()); let listener = TcpListener::bind(&addr).await?; - Ok(axum::serve(listener, app) - .with_graceful_shutdown(TerminateSignal::new()) - .await?) + Ok( + axum::serve(listener, ServiceExt::::into_make_service(app)) + .with_graceful_shutdown(TerminateSignal::new()) + .await?, + ) } #[cfg(test)]