Fixed removing trailing slashes

This commit is contained in:
Martin Berg Alstad 2024-06-22 20:23:28 +02:00
parent 4b6608fd4f
commit 38fc8ce383
3 changed files with 16 additions and 9 deletions

2
Cargo.lock generated
View File

@ -565,6 +565,7 @@ dependencies = [
"nom", "nom",
"serde", "serde",
"tokio", "tokio",
"tower",
"tower-http", "tower-http",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
@ -674,6 +675,7 @@ dependencies = [
"http-body", "http-body",
"http-body-util", "http-body-util",
"pin-project-lite", "pin-project-lite",
"tower",
"tower-layer", "tower-layer",
"tower-service", "tower-service",
"tracing", "tracing",

View File

@ -13,7 +13,8 @@ tokio = { version = "1.38.0", features = ["macros", "rt-multi-thread"] }
serde = { version = "1.0.203", features = ["derive", "rc"] } serde = { version = "1.0.203", features = ["derive", "rc"] }
# API # API
axum = { version = "0.7.5", features = ["macros"] } 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 # Logging
tracing = "0.1.40" tracing = "0.1.40"
tracing-subscriber = "0.3.18" tracing-subscriber = "0.3.18"

View File

@ -1,4 +1,6 @@
use std::net::SocketAddr; use std::net::SocketAddr;
use axum::{ServiceExt};
use axum::extract::Request;
use lib::{create_app, join_routes}; use lib::{create_app, join_routes};
use tokio::net::TcpListener; use tokio::net::TcpListener;
@ -6,6 +8,8 @@ use tower_http::cors::{Any, CorsLayer};
use tower_http::normalize_path::NormalizePathLayer; use tower_http::normalize_path::NormalizePathLayer;
use tower_http::trace; use tower_http::trace;
use tower_http::trace::TraceLayer; use tower_http::trace::TraceLayer;
use tower::Layer;
use tracing::Level; use tracing::Level;
use crate::routing::routes::*; use crate::routing::routes::*;
@ -34,15 +38,15 @@ async fn main() {
table::router() table::router()
].fallback(index::not_found); ].fallback(index::not_found);
let app = create_app!(routes, let app = NormalizePathLayer::trim_trailing_slash()
CorsLayer::new().allow_origin(Any), .layer(create_app!(routes,
NormalizePathLayer::trim_trailing_slash(), CorsLayer::new().allow_origin(Any),
TraceLayer::new_for_http() TraceLayer::new_for_http()
.make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO)) .make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO))
.on_response(trace::DefaultOnResponse::new().level(Level::INFO)) .on_response(trace::DefaultOnResponse::new().level(Level::INFO)),
); ));
tracing::info!("Starting server on: {addr}"); tracing::info!("Starting server on: {addr}");
axum::serve(listener, app.into_make_service()).await.unwrap(); axum::serve(listener, ServiceExt::<Request>::into_make_service(app)).await.unwrap();
} }