Skip to content

Commit

Permalink
Trim trailing slashes (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
smrtrfszm authored Dec 8, 2024
1 parent b380634 commit 0919ccf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion iam/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 10 additions & 14 deletions iam/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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 {
Expand All @@ -41,11 +39,6 @@ async fn handle_error(err: BoxError) -> Response {
}

fn middlewares<S: SharedTrait>(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))
Expand All @@ -55,7 +48,7 @@ fn middlewares<S: SharedTrait>(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::<S>))
.propagate_x_request_id()
Expand All @@ -69,14 +62,17 @@ pub async fn run() -> Result<(), Box<dyn Error>> {
let shared = shared::create_shared().await;

let app = middlewares(shared, handlers::routes::<Shared>());
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::<Request>::into_make_service(app))
.with_graceful_shutdown(TerminateSignal::new())
.await?,
)
}

#[cfg(test)]
Expand Down

0 comments on commit 0919ccf

Please sign in to comment.