From fc802d92522aafa94f43def35d55ea92e5ffaaf5 Mon Sep 17 00:00:00 2001 From: Spencer Ferris <3319370+spencewenski@users.noreply.github.com> Date: Tue, 8 Oct 2024 21:22:01 -0700 Subject: [PATCH] docs: Add doc comment explaining how NormalizePathLayer works (#393) --- src/service/http/initializer/normalize_path.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/service/http/initializer/normalize_path.rs b/src/service/http/initializer/normalize_path.rs index 566cedb9..49151071 100644 --- a/src/service/http/initializer/normalize_path.rs +++ b/src/service/http/initializer/normalize_path.rs @@ -47,6 +47,17 @@ where .priority } + /// Add the [`NormalizePathLayer`] to handle a trailing `/` at the end of URIs. + /// + /// Normally, adding a layer via the axum [`Router::layer`] method causes the layer to run + /// after routing has already completed. This means the [`NormalizePathLayer`] would not + /// normalize the uri for the purposes of routing, which defeats the point of the layer. + /// The workaround is to wrap the entire router with [`NormalizePathLayer`], which is why this + /// middleware is applied in an [`Initializer`] instead of as a normal + /// [`crate::service::http::middleware::Middleware`] -- this way, the [`NormalizePathLayer`] + /// is applied after all the routes and normal middleware have been applied. + /// + /// See: fn before_serve(&self, router: Router, _state: &S) -> RoadsterResult { let router = NormalizePathLayer::trim_trailing_slash().layer(router); let router = Router::new().nest_service("/", router);