From 028002f0c956b55451c0566bcc7c3def1bed4446 Mon Sep 17 00:00:00 2001 From: Luca Palmieri <20745048+LukeMathWalker@users.noreply.github.com> Date: Tue, 29 Nov 2022 12:34:07 +0000 Subject: [PATCH 1/5] Introduce an `aws-lambda` feature. --- rust-runtime/aws-smithy-http-server/Cargo.toml | 3 ++- rust-runtime/aws-smithy-http-server/src/lib.rs | 3 ++- rust-runtime/aws-smithy-http-server/src/rejection.rs | 2 ++ rust-runtime/aws-smithy-http-server/src/routing/mod.rs | 4 ++++ 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/rust-runtime/aws-smithy-http-server/Cargo.toml b/rust-runtime/aws-smithy-http-server/Cargo.toml index 46d5c52237..796431347c 100644 --- a/rust-runtime/aws-smithy-http-server/Cargo.toml +++ b/rust-runtime/aws-smithy-http-server/Cargo.toml @@ -13,6 +13,7 @@ Server runtime for Smithy Rust Server Framework. publish = true [features] +aws-lambda = ["lambda_http"] unredacted-logging = [] [dependencies] @@ -26,7 +27,7 @@ futures-util = { version = "0.3", default-features = false } http = "0.2" http-body = "0.4" hyper = { version = "0.14.12", features = ["server", "http1", "http2", "tcp", "stream"] } -lambda_http = "0.7.1" +lambda_http = { version = "0.7.1", optional = true } mime = "0.3" nom = "7" pin-project-lite = "0.2" diff --git a/rust-runtime/aws-smithy-http-server/src/lib.rs b/rust-runtime/aws-smithy-http-server/src/lib.rs index 64f512a905..83923bee11 100644 --- a/rust-runtime/aws-smithy-http-server/src/lib.rs +++ b/rust-runtime/aws-smithy-http-server/src/lib.rs @@ -3,10 +3,11 @@ * SPDX-License-Identifier: Apache-2.0 */ +#![cfg_attr(docsrs, feature(doc_cfg))] + //! HTTP server runtime and utilities, loosely based on [axum]. //! //! [axum]: https://docs.rs/axum/latest/axum/ - #[macro_use] pub(crate) mod macros; diff --git a/rust-runtime/aws-smithy-http-server/src/rejection.rs b/rust-runtime/aws-smithy-http-server/src/rejection.rs index 4ad682e6b8..8c96c7254a 100644 --- a/rust-runtime/aws-smithy-http-server/src/rejection.rs +++ b/rust-runtime/aws-smithy-http-server/src/rejection.rs @@ -262,6 +262,8 @@ convert_to_request_rejection!(std::str::Utf8Error, InvalidUtf8); convert_to_request_rejection!(hyper::Error, HttpBody); // Required in order to accept Lambda HTTP requests using `Router`. +#[cfg(feature = "aws-lambda")] +#[cfg_attr(docsrs, doc(cfg(feature = "aws-lambda")))] convert_to_request_rejection!(lambda_http::Error, HttpBody); pub mod any_rejections { diff --git a/rust-runtime/aws-smithy-http-server/src/routing/mod.rs b/rust-runtime/aws-smithy-http-server/src/routing/mod.rs index 1cd3dd21e7..3872c3a90b 100644 --- a/rust-runtime/aws-smithy-http-server/src/routing/mod.rs +++ b/rust-runtime/aws-smithy-http-server/src/routing/mod.rs @@ -29,6 +29,8 @@ use tower_http::map_response_body::MapResponseBodyLayer; mod future; mod into_make_service; +#[cfg(feature = "aws-lambda")] +#[cfg_attr(docsrs, doc(cfg(feature = "aws-lambda")))] mod lambda_handler; #[doc(hidden)] @@ -38,6 +40,8 @@ mod route; pub(crate) mod tiny_map; +#[cfg(feature = "aws-lambda")] +#[cfg_attr(docsrs, doc(cfg(feature = "aws-lambda")))] pub use self::lambda_handler::LambdaHandler; pub use self::{future::RouterFuture, into_make_service::IntoMakeService, route::Route}; From b3f315dca64b2008068784f31e23d58543281db0 Mon Sep 17 00:00:00 2001 From: Luca Palmieri <20745048+LukeMathWalker@users.noreply.github.com> Date: Tue, 29 Nov 2022 12:45:49 +0000 Subject: [PATCH 2/5] Add CHANGELOG --- CHANGELOG.next.toml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/CHANGELOG.next.toml b/CHANGELOG.next.toml index 53cf679909..4213c9b683 100644 --- a/CHANGELOG.next.toml +++ b/CHANGELOG.next.toml @@ -486,3 +486,15 @@ message = "Make generated enum `values()` functions callable in const contexts." references = ["smithy-rs#2011"] meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "all" } author = "lsr0" + +[[smithy-rs]] +message = """ +All types that are exclusively relevant within the context of an AWS Lambda function are now gated behind the +`aws-lambda` feature flag. + +This will reduce the number of dependencies (and improve build times) for users that are running their Smithy services +in non-serverless environments (e.g. via `hyper`). +""" +references = ["smithy-rs#2035"] +meta = { "breaking" = true, "tada" = false, "bug" = false, "target" = "server" } +author = "LukeMathWalker" From 3a53ac492d63dddb158167ef079f4085f140d8c9 Mon Sep 17 00:00:00 2001 From: Luca Palmieri <20745048+LukeMathWalker@users.noreply.github.com> Date: Tue, 29 Nov 2022 12:54:10 +0000 Subject: [PATCH 3/5] Use the new and shiny feature syntax to avoid creating an implicit `lambda_http` feature. --- rust-runtime/aws-smithy-http-server/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-runtime/aws-smithy-http-server/Cargo.toml b/rust-runtime/aws-smithy-http-server/Cargo.toml index 796431347c..d3f7b47d97 100644 --- a/rust-runtime/aws-smithy-http-server/Cargo.toml +++ b/rust-runtime/aws-smithy-http-server/Cargo.toml @@ -13,7 +13,7 @@ Server runtime for Smithy Rust Server Framework. publish = true [features] -aws-lambda = ["lambda_http"] +aws-lambda = ["dep:lambda_http"] unredacted-logging = [] [dependencies] From eb3ded204cee6e53a2e09d1f56627d7d1ca84fde Mon Sep 17 00:00:00 2001 From: Luca Palmieri <20745048+LukeMathWalker@users.noreply.github.com> Date: Tue, 29 Nov 2022 14:26:44 +0000 Subject: [PATCH 4/5] Add `aws-lambda` feature to the Python server. Enable the `aws-lambda` feature in our example. Be explicit in providing an implementation of request rejection for Box. --- rust-runtime/aws-smithy-http-server-python/Cargo.toml | 5 ++++- rust-runtime/aws-smithy-http-server-python/src/lib.rs | 2 ++ rust-runtime/aws-smithy-http-server-python/src/server.rs | 6 +++++- .../examples/pokemon-service/Cargo.toml | 2 +- rust-runtime/aws-smithy-http-server/src/rejection.rs | 7 +++---- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/rust-runtime/aws-smithy-http-server-python/Cargo.toml b/rust-runtime/aws-smithy-http-server-python/Cargo.toml index 29306449a4..8085e000b4 100644 --- a/rust-runtime/aws-smithy-http-server-python/Cargo.toml +++ b/rust-runtime/aws-smithy-http-server-python/Cargo.toml @@ -12,6 +12,9 @@ Python server runtime for Smithy Rust Server Framework. """ publish = true +[features] +aws-lambda = ["aws-smithy-http-server/aws-lambda", "dep:lambda_http"] + [dependencies] aws-smithy-http = { path = "../aws-smithy-http" } aws-smithy-http-server = { path = "../aws-smithy-http-server" } @@ -22,7 +25,7 @@ bytes = "1.2" futures = "0.3" http = "0.2" hyper = { version = "0.14.20", features = ["server", "http1", "http2", "tcp", "stream"] } -lambda_http = "0.7.1" +lambda_http = { version = "0.7.1", optional = true } num_cpus = "1.13.1" parking_lot = "0.12.1" pin-project-lite = "0.2" diff --git a/rust-runtime/aws-smithy-http-server-python/src/lib.rs b/rust-runtime/aws-smithy-http-server-python/src/lib.rs index ee8e8b4b9b..7039b52619 100644 --- a/rust-runtime/aws-smithy-http-server-python/src/lib.rs +++ b/rust-runtime/aws-smithy-http-server-python/src/lib.rs @@ -3,6 +3,8 @@ * SPDX-License-Identifier: Apache-2.0 */ +#![cfg_attr(docsrs, feature(doc_cfg))] + //! Rust/Python bindings, runtime and utilities. //! //! This crates implements all the generic code needed to start and manage diff --git a/rust-runtime/aws-smithy-http-server-python/src/server.rs b/rust-runtime/aws-smithy-http-server-python/src/server.rs index 2de6c1fe4b..466a35151c 100644 --- a/rust-runtime/aws-smithy-http-server-python/src/server.rs +++ b/rust-runtime/aws-smithy-http-server-python/src/server.rs @@ -7,7 +7,7 @@ use std::{collections::HashMap, convert::Infallible, ops::Deref, process, thread use aws_smithy_http_server::{ body::{Body, BoxBody}, - routing::{IntoMakeService, LambdaHandler}, + routing::IntoMakeService, AddExtensionLayer, }; use http::{Request, Response}; @@ -422,7 +422,11 @@ event_loop.add_signal_handler(signal.SIGINT, /// /// Unlike the `run_server`, `run_lambda_handler` does not spawns other processes, /// it starts the Lambda handler on the current process. + #[cfg(feature = "aws-lambda")] + #[cfg_attr(docsrs, doc(cfg(feature = "aws-lambda")))] fn run_lambda_handler(&mut self, py: Python) -> PyResult<()> { + use aws_smithy_http_server::routing::LambdaHandler; + let event_loop = self.configure_python_event_loop(py)?; let service = self.build_and_configure_service(py, event_loop)?; let rt = runtime::Builder::new_multi_thread() diff --git a/rust-runtime/aws-smithy-http-server/examples/pokemon-service/Cargo.toml b/rust-runtime/aws-smithy-http-server/examples/pokemon-service/Cargo.toml index ac26711424..51f9c6f1a2 100644 --- a/rust-runtime/aws-smithy-http-server/examples/pokemon-service/Cargo.toml +++ b/rust-runtime/aws-smithy-http-server/examples/pokemon-service/Cargo.toml @@ -44,7 +44,7 @@ futures-util = "0.3" lambda_http = "0.7.1" # Local paths -aws-smithy-http-server = { path = "../../" } +aws-smithy-http-server = { path = "../../", features = ["aws-lambda"] } pokemon-service-server-sdk = { path = "../pokemon-service-server-sdk/" } [dev-dependencies] diff --git a/rust-runtime/aws-smithy-http-server/src/rejection.rs b/rust-runtime/aws-smithy-http-server/src/rejection.rs index 8c96c7254a..ca780487a6 100644 --- a/rust-runtime/aws-smithy-http-server/src/rejection.rs +++ b/rust-runtime/aws-smithy-http-server/src/rejection.rs @@ -261,10 +261,9 @@ convert_to_request_rejection!(std::str::Utf8Error, InvalidUtf8); // everyone will run a Hyper-based server in their services). convert_to_request_rejection!(hyper::Error, HttpBody); -// Required in order to accept Lambda HTTP requests using `Router`. -#[cfg(feature = "aws-lambda")] -#[cfg_attr(docsrs, doc(cfg(feature = "aws-lambda")))] -convert_to_request_rejection!(lambda_http::Error, HttpBody); +// Useful in general, but it also required in order to accept Lambda HTTP requests using +// `Router` since `lambda_http::Error` is a type alias for `Box`. +convert_to_request_rejection!(Box, HttpBody); pub mod any_rejections { //! This module hosts enums, up to size 8, which implement [`IntoResponse`] when their variants implement From a198347e35c9a460cfdedac0ce4e8719e95d6cd8 Mon Sep 17 00:00:00 2001 From: Luca Palmieri <20745048+LukeMathWalker@users.noreply.github.com> Date: Tue, 29 Nov 2022 15:01:17 +0000 Subject: [PATCH 5/5] Add an `aws-lambda` feature flag to the generated Python-specific crate. We enable it by default to make sure the Lambda method ends up visible in the final Python wrapper library. --- .../PythonServerCodegenDecorator.kt | 18 ++++++++++++++++++ .../generators/PythonApplicationGenerator.kt | 1 + 2 files changed, 19 insertions(+) diff --git a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/customizations/PythonServerCodegenDecorator.kt b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/customizations/PythonServerCodegenDecorator.kt index db4f71b301..150896eb54 100644 --- a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/customizations/PythonServerCodegenDecorator.kt +++ b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/customizations/PythonServerCodegenDecorator.kt @@ -7,6 +7,7 @@ package software.amazon.smithy.rust.codegen.server.python.smithy.customizations import software.amazon.smithy.model.neighbor.Walker import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator +import software.amazon.smithy.rust.codegen.core.rustlang.Feature import software.amazon.smithy.rust.codegen.core.rustlang.Writable import software.amazon.smithy.rust.codegen.core.rustlang.docs import software.amazon.smithy.rust.codegen.core.rustlang.rust @@ -103,6 +104,21 @@ class PubUsePythonTypesDecorator : RustCodegenDecorator { + override val name: String = "PythonFeatureFlagsDecorator" + override val order: Byte = 0 + + override fun extras(codegenContext: ServerCodegenContext, rustCrate: RustCrate) { + rustCrate.mergeFeature(Feature("aws-lambda", true, listOf("aws-smithy-http-server-python/aws-lambda"))) + } + + override fun supportsCodegenContext(clazz: Class): Boolean = + clazz.isAssignableFrom(ServerCodegenContext::class.java) +} + val DECORATORS = listOf( /** * Add the [InternalServerError] error to all operations. @@ -115,4 +131,6 @@ val DECORATORS = listOf( PubUsePythonTypesDecorator(), // Render the Python shared library export. PythonExportModuleDecorator(), + // Add the `aws-lambda` feature flag + PythonFeatureFlagsDecorator(), ) diff --git a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonApplicationGenerator.kt b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonApplicationGenerator.kt index 63fd61e1c9..7d5635b72f 100644 --- a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonApplicationGenerator.kt +++ b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonApplicationGenerator.kt @@ -278,6 +278,7 @@ class PythonApplicationGenerator( self.run_server(py, address, port, backlog, workers) } /// Lambda entrypoint: start the server on Lambda. + ##[cfg(feature = "aws-lambda")] ##[pyo3(text_signature = "(${'$'}self)")] pub fn run_lambda( &mut self,