Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim trailing slashes #101

Merged
merged 1 commit into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading