|
| 1 | +use crate::request::LambdaRequest; |
| 2 | +use crate::tower::ServiceBuilder; |
| 3 | +use crate::{Request, RequestExt}; |
| 4 | +pub use aws_lambda_events::encodings::Body as LambdaEventBody; |
| 5 | +use bytes::Bytes; |
| 6 | +pub use http::{self, Response}; |
| 7 | +use http_body::Body; |
| 8 | +use lambda_runtime::LambdaEvent; |
| 9 | +pub use lambda_runtime::{self, service_fn, tower, Context, Error, Service}; |
| 10 | +use std::fmt::{Debug, Display}; |
| 11 | + |
| 12 | +/// Starts the Lambda Rust runtime and stream response back [Configure Lambda |
| 13 | +/// Streaming Response](https://docs.aws.amazon.com/lambda/latest/dg/configuration-response-streaming.html). |
| 14 | +/// |
| 15 | +/// This takes care of transforming the LambdaEvent into a [`Request`] and |
| 16 | +/// accepts [`http::Response<http_body::Body>`] as response. |
| 17 | +pub async fn run_with_streaming_response<'a, S, B, E>(handler: S) -> Result<(), Error> |
| 18 | +where |
| 19 | + S: Service<Request, Response = Response<B>, Error = E>, |
| 20 | + S::Future: Send + 'a, |
| 21 | + E: Debug + Display, |
| 22 | + B: Body + Unpin + Send + 'static, |
| 23 | + B::Data: Into<Bytes> + Send, |
| 24 | + B::Error: Into<Error> + Send + Debug, |
| 25 | +{ |
| 26 | + let svc = ServiceBuilder::new() |
| 27 | + .map_request(|req: LambdaEvent<LambdaRequest>| { |
| 28 | + let event: Request = req.payload.into(); |
| 29 | + event.with_lambda_context(req.context) |
| 30 | + }) |
| 31 | + .service(handler); |
| 32 | + |
| 33 | + lambda_runtime::run_with_streaming_response(svc).await |
| 34 | +} |
0 commit comments