From 178b7c4bd11cc7a7b8b8f52bb301b6647c92b007 Mon Sep 17 00:00:00 2001 From: nerfZael Date: Mon, 4 Sep 2023 00:53:07 +0200 Subject: [PATCH 1/2] added cors --- gateway/Cargo.lock | 25 +++++++++++++++++++ gateway/rust/gateway_service/Cargo.toml | 1 + .../rust/gateway_service/src/setup_routes.rs | 11 ++++++-- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/gateway/Cargo.lock b/gateway/Cargo.lock index 207ace5..6b7afb7 100644 --- a/gateway/Cargo.lock +++ b/gateway/Cargo.lock @@ -1124,6 +1124,7 @@ dependencies = [ "tokio", "tokio-stream", "tower", + "tower-http", "tower-service", "tracing-subscriber", ] @@ -1253,6 +1254,12 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "http-range-header" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "add0ab9360ddbd88cfeb3bd9574a1d85cfdfa14db10b3e21d3700dbc4328758f" + [[package]] name = "http-serde" version = "1.1.2" @@ -2988,6 +2995,24 @@ dependencies = [ "tracing", ] +[[package]] +name = "tower-http" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61c5bb1d698276a2443e5ecfabc1008bf15a36c12e6a7176e7bf089ea9131140" +dependencies = [ + "bitflags 2.3.3", + "bytes", + "futures-core", + "futures-util", + "http", + "http-body", + "http-range-header", + "pin-project-lite", + "tower-layer", + "tower-service", +] + [[package]] name = "tower-layer" version = "0.3.2" diff --git a/gateway/rust/gateway_service/Cargo.toml b/gateway/rust/gateway_service/Cargo.toml index 679ce30..8f05f73 100644 --- a/gateway/rust/gateway_service/Cargo.toml +++ b/gateway/rust/gateway_service/Cargo.toml @@ -30,6 +30,7 @@ tower-service = "0.3.2" dotenvy = "0.15.7" clap = { version = "4.3.10", features = ["derive"] } polywrap_core = "0.1.6-beta.7" +tower-http = { version = "0.4.3", features = ["cors"] } [dev-dependencies] mockall = "0.11.4" diff --git a/gateway/rust/gateway_service/src/setup_routes.rs b/gateway/rust/gateway_service/src/setup_routes.rs index 57dbd8d..9cabdb7 100644 --- a/gateway/rust/gateway_service/src/setup_routes.rs +++ b/gateway/rust/gateway_service/src/setup_routes.rs @@ -4,7 +4,9 @@ use axum::{ routing::{get, post}, Router, }; +use http::Method; use lambda_http::{run, Error as HttpError}; +use tower_http::cors::{Any, CorsLayer}; use crate::{ constants, @@ -51,6 +53,11 @@ pub async fn setup_routes() -> Result<(), HttpError> { } }; + let cors = CorsLayer::new() + .allow_headers(Any) + .allow_methods(vec![Method::GET, Method::POST, Method::OPTIONS, Method::HEAD, Method::PUT, Method::DELETE]) + .allow_origin(Any); + let app = Router::new() .route( &(route_prefix.clone() + "/"), @@ -71,7 +78,7 @@ pub async fn setup_routes() -> Result<(), HttpError> { .route( &(route_prefix + "/r/:user/:packageAndVersion"), post(routes::publish).with_state(deps), - ); + ).layer(cors); #[cfg(not(feature = "local"))] { @@ -80,7 +87,7 @@ pub async fn setup_routes() -> Result<(), HttpError> { #[cfg(feature = "local")] { - let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 3000)); + let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 3001)); axum::Server::bind(&addr) .serve(app.into_make_service()) .await From 55b2e4f304716d0cd25257583692d926db7c065a Mon Sep 17 00:00:00 2001 From: nerfZael Date: Mon, 4 Sep 2023 00:53:25 +0200 Subject: [PATCH 2/2] bumped version --- gateway/Cargo.lock | 2 +- gateway/rust/gateway_service/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gateway/Cargo.lock b/gateway/Cargo.lock index 6b7afb7..5a121b9 100644 --- a/gateway/Cargo.lock +++ b/gateway/Cargo.lock @@ -1096,7 +1096,7 @@ dependencies = [ [[package]] name = "gateway_service" -version = "0.1.4" +version = "0.1.5" dependencies = [ "async-trait", "aws-config", diff --git a/gateway/rust/gateway_service/Cargo.toml b/gateway/rust/gateway_service/Cargo.toml index 8f05f73..5972268 100644 --- a/gateway/rust/gateway_service/Cargo.toml +++ b/gateway/rust/gateway_service/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gateway_service" -version = "0.1.4" +version = "0.1.5" edition = "2021" [dependencies]