From 2fe3285ce750a03298f05ed9465bb6d03f028ed0 Mon Sep 17 00:00:00 2001 From: xeon Date: Mon, 15 Apr 2024 15:52:20 +0300 Subject: [PATCH] Fix for trailing slash in SDK requests --- Cargo.toml | 2 ++ sdkserver/Cargo.toml | 2 ++ sdkserver/src/main.rs | 14 ++++++++++---- sdkserver/src/services/auth.rs | 2 +- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4c2f15c..a9ccb77 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,8 @@ lazy_static = "1.4.0" axum = "0.7.4" axum-server = "0.6.0" +tower = "0.4.13" +tower-http = { version = "0.5.2", features = ["normalize-path"] } dirs = "5.0.1" dotenv = "0.15.0" diff --git a/sdkserver/Cargo.toml b/sdkserver/Cargo.toml index 4354435..0e0cc51 100644 --- a/sdkserver/Cargo.toml +++ b/sdkserver/Cargo.toml @@ -31,3 +31,5 @@ ansi_term.workspace = true prost.workspace = true rbase64.workspace = true proto.workspace = true +tower.workspace = true +tower-http.workspace = true diff --git a/sdkserver/src/main.rs b/sdkserver/src/main.rs index 765c716..d2d1c50 100644 --- a/sdkserver/src/main.rs +++ b/sdkserver/src/main.rs @@ -1,8 +1,12 @@ use anyhow::Result; +use axum::extract::Request; use axum::routing::{get, post}; -use axum::Router; +use axum::{Router, ServiceExt}; use logging::init_tracing; use services::{auth, dispatch, errors}; +use tokio::net::TcpListener; +use tower::Layer; +use tower_http::normalize_path::NormalizePathLayer; use tracing::Level; mod config; @@ -18,7 +22,7 @@ async fn main() -> Result<()> { let span = tracing::span!(Level::DEBUG, "main"); let _ = span.enter(); - let router = Router::new() + let app = Router::new() .route( dispatch::QUERY_DISPATCH_ENDPOINT, get(dispatch::query_dispatch), @@ -42,11 +46,13 @@ async fn main() -> Result<()> { ) .fallback(errors::not_found); + let app = NormalizePathLayer::trim_trailing_slash().layer(app); + let addr = format!("0.0.0.0:{PORT}"); - let server = axum_server::bind(addr.parse()?); + let server = TcpListener::bind(&addr).await?; tracing::info!("sdkserver is listening at {addr}"); - server.serve(router.into_make_service()).await?; + axum::serve(server, ServiceExt::::into_make_service(app)).await?; Ok(()) } diff --git a/sdkserver/src/services/auth.rs b/sdkserver/src/services/auth.rs index 5a0cc3c..463c48c 100644 --- a/sdkserver/src/services/auth.rs +++ b/sdkserver/src/services/auth.rs @@ -1,7 +1,7 @@ use axum::Json; use serde_json::json; -pub const LOGIN_WITH_PASSWORD_ENDPOINT: &str = "/:product_name/mdk/shield/api/login/"; +pub const LOGIN_WITH_PASSWORD_ENDPOINT: &str = "/:product_name/mdk/shield/api/login"; pub const LOGIN_WITH_SESSION_TOKEN_ENDPOINT: &str = "/:product_name/mdk/shield/api/verify"; pub const GRANTER_LOGIN_VERIFICATION_ENDPOINT: &str = "/:product_name/combo/granter/login/v2/login"; pub const RISKY_API_CHECK_ENDPOINT: &str = "/account/risky/api/check";