Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions lambda-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
29 changes: 29 additions & 0 deletions lambda-http/examples/hello-cors.rs
Original file line number Diff line number Diff line change
@@ -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<Response<Body>, 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"),
})
}
5 changes: 1 addition & 4 deletions lambda-http/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,7 @@ impl HttpBody for Body {
}

fn is_end_stream(&self) -> bool {
match self {
Body::Empty => true,
_ => false,
}
matches!(self, Body::Empty)
}

fn size_hint(&self) -> SizeHint {
Expand Down
12 changes: 6 additions & 6 deletions lambda-http/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub enum LambdaRequest<'a> {
body: Option<Cow<'a, str>>,
#[serde(default)]
is_base64_encoded: bool,
request_context: ApiGatewayV2RequestContext,
request_context: Box<ApiGatewayV2RequestContext>,
},
#[serde(rename_all = "camelCase")]
Alb {
Expand All @@ -64,7 +64,7 @@ pub enum LambdaRequest<'a> {
body: Option<Cow<'a, str>>,
#[serde(default)]
is_base64_encoded: bool,
request_context: AlbRequestContext,
request_context: Box<AlbRequestContext>,
},
#[serde(rename_all = "camelCase")]
ApiGateway {
Expand All @@ -86,7 +86,7 @@ pub enum LambdaRequest<'a> {
body: Option<Cow<'a, str>>,
#[serde(default)]
is_base64_encoded: bool,
request_context: ApiGatewayRequestContext,
request_context: Box<ApiGatewayRequestContext>,
#[serde(default, deserialize_with = "nullable_default")]
resource: Option<String>,
},
Expand Down Expand Up @@ -198,11 +198,11 @@ pub struct AlbRequestContext {
#[serde(untagged)]
pub enum RequestContext {
/// API Gateway v2 request context
ApiGatewayV2(ApiGatewayV2RequestContext),
ApiGatewayV2(Box<ApiGatewayV2RequestContext>),
/// API Gateway request context
ApiGateway(ApiGatewayRequestContext),
ApiGateway(Box<ApiGatewayRequestContext>),
/// ALB request context
Alb(AlbRequestContext),
Alb(Box<AlbRequestContext>),
}

/// Elastic load balancer context information
Expand Down
1 change: 1 addition & 0 deletions lambda-integration-tests/src/bin/logs-trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl MyLogsProcessor {
impl Service<Vec<LambdaLog>> for MyLogsProcessor {
type Response = ();
type Error = Error;
#[allow(clippy::type_complexity)]
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;

fn poll_ready(&mut self, _cx: &mut core::task::Context<'_>) -> core::task::Poll<Result<(), Self::Error>> {
Expand Down
2 changes: 1 addition & 1 deletion lambda-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

Expand Down