openapi_lambda/
runtime.rs

1use aws_lambda_events::apigw::{ApiGatewayProxyRequest, ApiGatewayProxyResponse};
2use futures::FutureExt;
3use lambda_runtime::{service_fn, LambdaEvent};
4
5use std::future::Future;
6
7/// Start the Lambda runtime to handle requests for the specified API using the specified
8/// middleware.
9///
10/// # Example
11///
12/// ```rust,ignore
13/// // Replace `my_api` with the name of your crate and `backend` with the name of the module
14/// // passed to `ApiLambda::new()`.
15/// use my_api::backend::Api;
16/// use my_api::backend_handler::BackendApiHandler;
17/// use openapi_lambda::run_lambda;
18///
19/// #[tokio::main]
20/// pub async fn main() {
21///   let api = BackendApiHandler::new(...);
22///   let middleware = ...; // Instantiate your middleware here.
23///
24///   run_lambda(|event| api.dispatch_request(event, &middleware)).await
25/// }
26/// ```
27pub async fn run_lambda<F, Fut>(mut dispatch_event: F)
28where
29  F: FnMut(LambdaEvent<ApiGatewayProxyRequest>) -> Fut,
30  Fut: Future<Output = ApiGatewayProxyResponse>,
31{
32  lambda_runtime::run(service_fn(|event: LambdaEvent<ApiGatewayProxyRequest>| {
33    dispatch_event(event).map(Result::<_, std::convert::Infallible>::Ok)
34  }))
35  .await
36  .expect("Lambda run loop should never exit")
37}