Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Adapter to be .boxed() #540

Merged
merged 1 commit into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
62 changes: 59 additions & 3 deletions lambda-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub struct Adapter<'a, R, S> {
impl<'a, R, S, E> From<S> for Adapter<'a, R, S>
where
S: Service<Request, Response = R, Error = E>,
S::Future: 'a,
S::Future: Send + 'a,
R: IntoResponse,
{
fn from(service: S) -> Self {
Expand All @@ -148,7 +148,7 @@ where
impl<'a, R, S, E> Service<LambdaEvent<LambdaRequest>> for Adapter<'a, R, S>
where
S: Service<Request, Response = R, Error = E>,
S::Future: 'a,
S::Future: Send + 'a,
R: IntoResponse,
{
type Response = LambdaResponse;
Expand Down Expand Up @@ -176,9 +176,65 @@ where
pub async fn run<'a, R, S, E>(handler: S) -> Result<(), Error>
where
S: Service<Request, Response = R, Error = E>,
S::Future: 'a,
S::Future: Send + 'a,
R: IntoResponse,
E: std::fmt::Debug + std::fmt::Display,
{
lambda_runtime::run(Adapter::from(handler)).await
}

#[cfg(test)]
mod test_adapter {
use std::task::{Context, Poll};

use crate::{
http::{Response, StatusCode},
lambda_runtime::LambdaEvent,
request::LambdaRequest,
response::LambdaResponse,
tower::{util::BoxService, Service, ServiceBuilder, ServiceExt},
Adapter, Body, Request,
};

// A middleware that logs requests before forwarding them to another service
struct LogService<S> {
inner: S,
}

impl<S> Service<LambdaEvent<LambdaRequest>> for LogService<S>
where
S: Service<LambdaEvent<LambdaRequest>>,
{
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;

fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}

fn call(&mut self, event: LambdaEvent<LambdaRequest>) -> Self::Future {
// Log the request
println!("Lambda event: {:#?}", event);

self.inner.call(event)
}
}

/// This tests that `Adapter` can be used in a `tower::Service` where the user
/// may require additional middleware between `lambda_runtime::run` and where
/// the `LambdaEvent` is converted into a `Request`.
#[test]
fn adapter_is_boxable() {
let _service: BoxService<LambdaEvent<LambdaRequest>, LambdaResponse, http::Error> = ServiceBuilder::new()
.layer_fn(|service| {
// This could be any middleware that logs, inspects, or manipulates
// the `LambdaEvent` before it's converted to a `Request` by `Adapter`.

LogService { inner: service }
})
.layer_fn(Adapter::from)
.service_fn(|_event: Request| async move { Response::builder().status(StatusCode::OK).body(Body::Empty) })
.boxed();
dcormier marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 1 addition & 1 deletion lambda-http/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl LambdaRequest {
}

/// RequestFuture type
pub type RequestFuture<'a, R, E> = Pin<Box<dyn Future<Output = Result<R, E>> + 'a>>;
pub type RequestFuture<'a, R, E> = Pin<Box<dyn Future<Output = Result<R, E>> + Send + 'a>>;

/// Represents the origin from which the lambda was requested from.
#[doc(hidden)]
Expand Down
15 changes: 9 additions & 6 deletions lambda-http/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub trait IntoResponse {

impl<B> IntoResponse for Response<B>
where
B: ConvertBody + 'static,
B: ConvertBody + Send + 'static,
{
fn into_response(self) -> ResponseFuture {
let (parts, body) = self.into_parts();
Expand Down Expand Up @@ -180,15 +180,16 @@ impl IntoResponse for serde_json::Value {
}
}

pub type ResponseFuture = Pin<Box<dyn Future<Output = Response<Body>>>>;
pub type ResponseFuture = Pin<Box<dyn Future<Output = Response<Body>> + Send>>;

pub trait ConvertBody {
fn convert(self, parts: HeaderMap) -> BodyFuture;
}

impl<B> ConvertBody for B
where
B: HttpBody + Unpin + 'static,
B: HttpBody + Unpin + Send + 'static,
B::Data: Send,
B::Error: fmt::Debug,
{
fn convert(self, headers: HeaderMap) -> BodyFuture {
Expand Down Expand Up @@ -227,15 +228,17 @@ where

fn convert_to_binary<B>(body: B) -> BodyFuture
where
B: HttpBody + Unpin + 'static,
B: HttpBody + Unpin + Send + 'static,
B::Data: Send,
B::Error: fmt::Debug,
{
Box::pin(async move { Body::from(to_bytes(body).await.expect("unable to read bytes from body").to_vec()) })
}

fn convert_to_text<B>(body: B, content_type: &str) -> BodyFuture
where
B: HttpBody + Unpin + 'static,
B: HttpBody + Unpin + Send + 'static,
B::Data: Send,
B::Error: fmt::Debug,
{
let mime_type = content_type.parse::<Mime>();
Expand All @@ -260,7 +263,7 @@ where
})
}

pub type BodyFuture = Pin<Box<dyn Future<Output = Body>>>;
pub type BodyFuture = Pin<Box<dyn Future<Output = Body> + Send>>;

#[cfg(test)]
mod tests {
Expand Down