From 38fc8ce3837911af0871381b92fdead9e6da5869 Mon Sep 17 00:00:00 2001 From: Martin Berg Alstad Date: Sat, 22 Jun 2024 20:23:28 +0200 Subject: [PATCH] Fixed removing trailing slashes --- Cargo.lock | 2 ++ Cargo.toml | 3 ++- src/main.rs | 20 ++++++++++++-------- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6e54fae..19ce770 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -565,6 +565,7 @@ dependencies = [ "nom", "serde", "tokio", + "tower", "tower-http", "tracing", "tracing-subscriber", @@ -674,6 +675,7 @@ dependencies = [ "http-body", "http-body-util", "pin-project-lite", + "tower", "tower-layer", "tower-service", "tracing", diff --git a/Cargo.toml b/Cargo.toml index 7eb3caf..10600bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,8 @@ tokio = { version = "1.38.0", features = ["macros", "rt-multi-thread"] } serde = { version = "1.0.203", features = ["derive", "rc"] } # API axum = { version = "0.7.5", features = ["macros"] } -tower-http = { version = "0.5.2", features = ["cors", "trace", "normalize-path"] } +tower-http = { version = "0.5.2", features = ["cors", "trace", "normalize-path", "tower"] } +tower = "0.4.13" # Logging tracing = "0.1.40" tracing-subscriber = "0.3.18" diff --git a/src/main.rs b/src/main.rs index 54cb131..b541660 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ use std::net::SocketAddr; +use axum::{ServiceExt}; +use axum::extract::Request; use lib::{create_app, join_routes}; use tokio::net::TcpListener; @@ -6,6 +8,8 @@ use tower_http::cors::{Any, CorsLayer}; use tower_http::normalize_path::NormalizePathLayer; use tower_http::trace; use tower_http::trace::TraceLayer; +use tower::Layer; + use tracing::Level; use crate::routing::routes::*; @@ -34,15 +38,15 @@ async fn main() { table::router() ].fallback(index::not_found); - let app = create_app!(routes, - CorsLayer::new().allow_origin(Any), - NormalizePathLayer::trim_trailing_slash(), - TraceLayer::new_for_http() - .make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO)) - .on_response(trace::DefaultOnResponse::new().level(Level::INFO)) - ); + let app = NormalizePathLayer::trim_trailing_slash() + .layer(create_app!(routes, + CorsLayer::new().allow_origin(Any), + TraceLayer::new_for_http() + .make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO)) + .on_response(trace::DefaultOnResponse::new().level(Level::INFO)), + )); tracing::info!("Starting server on: {addr}"); - axum::serve(listener, app.into_make_service()).await.unwrap(); + axum::serve(listener, ServiceExt::::into_make_service(app)).await.unwrap(); }