diff --git a/lambda-http/Cargo.toml b/lambda-http/Cargo.toml index ac1b45b9..cb1f7a97 100644 --- a/lambda-http/Cargo.toml +++ b/lambda-http/Cargo.toml @@ -30,3 +30,4 @@ serde_urlencoded = "0.7.0" log = "^0.4" maplit = "1.0" tokio = { version = "1.0", features = ["macros"] } +tower-http = { version = "0.2", features = ["cors"] } \ No newline at end of file diff --git a/lambda-http/examples/hello-cors.rs b/lambda-http/examples/hello-cors.rs new file mode 100644 index 00000000..79d2a986 --- /dev/null +++ b/lambda-http/examples/hello-cors.rs @@ -0,0 +1,29 @@ +use http::Method; +use lambda_http::{service_fn, tower::ServiceBuilder, Body, Error, IntoResponse, Request, RequestExt, Response}; +use tower_http::cors::{Any, CorsLayer}; + +#[tokio::main] +async fn main() -> Result<(), Error> { + // Define a layer to inject CORS headers + let cors_layer = CorsLayer::new() + .allow_methods(vec![Method::GET, Method::POST]) + .allow_origin(Any); + + let handler = ServiceBuilder::new() + // Add the CORS layer to the service + .layer(cors_layer) + .service(service_fn(func)); + + lambda_http::run(handler).await?; + Ok(()) +} + +async fn func(event: Request) -> Result, Error> { + Ok(match event.query_string_parameters().get("first_name") { + Some(first_name) => format!("Hello, {}!", first_name).into_response(), + _ => Response::builder() + .status(400) + .body("Empty first name".into()) + .expect("failed to render response"), + }) +} diff --git a/lambda-integration-tests/src/bin/logs-trait.rs b/lambda-integration-tests/src/bin/logs-trait.rs index 871a5019..a9bbe7d5 100644 --- a/lambda-integration-tests/src/bin/logs-trait.rs +++ b/lambda-integration-tests/src/bin/logs-trait.rs @@ -34,6 +34,7 @@ impl MyLogsProcessor { impl Service> for MyLogsProcessor { type Response = (); type Error = Error; + #[allow(clippy::type_complexity)] type Future = Pin> + Send>>; fn poll_ready(&mut self, _cx: &mut core::task::Context<'_>) -> core::task::Poll> { diff --git a/lambda-runtime/Cargo.toml b/lambda-runtime/Cargo.toml index b9b7c490..cd12e046 100644 --- a/lambda-runtime/Cargo.toml +++ b/lambda-runtime/Cargo.toml @@ -23,7 +23,7 @@ bytes = "1.0" http = "0.2" async-stream = "0.3" tracing = { version = "0.1", features = ["log"] } -tower = { version = "0.4", features = ["util"] } +tower = { version = "0.4", features = ["full"] } tokio-stream = "0.1.2" lambda_runtime_api_client = { version = "0.5", path = "../lambda-runtime-api-client" }