titan_lambda/
lib.rs

1mod lambda_handler_service;
2use std::future::Future;
3
4pub use lambda_http::Request;
5
6pub use lambda_handler_service::LambdaHandlerService;
7use titan::{lambda::LambdaAppService, App};
8use titan_core::{FromRequest, Handler, Respondable};
9
10/// Wraps an async handler in a runtime environment for processing AWS Lambda events.
11///
12/// This function creates a [`LambdaHandlerService`] that integrates the provided handler with an AWS Lambda-compatible runtime.
13/// The handler is an async function responsible for processing incoming requests and generating responses.
14///
15/// # Type Parameters
16/// - `H`: The type of the handler. It must be a function or type that implements the [`Handler`] trait.
17/// - `Args`: The type representing the arguments passed to the handler. It must implement the [`FromRequest`] trait.
18///
19/// # Parameters
20/// - `handler`: An async function or handler type that processes requests in the Lambda runtime. The handler should
21///   be an async function or struct that implements the [`Handler`] trait.
22///
23/// # Constraints
24/// - `H`:
25///   - Must implement the [`Handler`] trait for handling requests of type `Args`.
26///   - Must be [`Clone`] to allow it to be used concurrently for multiple requests.
27/// - `H::Future`:
28///   - Must implement [`Future`] with an output type corresponding to the handler’s response.
29///   - Must be [`Send`] to ensure it can be used across threads safely.
30/// - `H::Output`:
31///   - Must implement [`Respondable`], allowing the handler's output to be transformed into a valid Lambda response.
32/// - `Args`:
33///   - Must implement the [`FromRequest`] trait to handle the conversion of incoming requests into the `Args` type.
34///   - Must be both [`Send`] and [`Sync`] to ensure safe concurrent access and use in the Lambda environment.
35///   - Must have a `'static` lifetime to ensure it does not contain any non-static references.
36/// - `Args::Error`:
37///   - Must be [`Send`] to ensure errors can be safely sent across threads.
38///
39/// # Returns
40/// A [`LambdaHandlerService`] instance that processes incoming Lambda events using the provided handler.
41///
42/// # Examples
43///
44/// ```rust
45/// use titan::{web, Respondable};
46///
47/// async fn my_handler(body_str: String) -> impl Respondable {
48///     "Hello World"
49/// }
50///
51/// #[tokio::main]
52/// async fn main() {
53///   // Uncomment the last line to run example
54///   titan_lambda::handler_runtime(my_handler).run(); // .await.unwrap();
55/// }
56/// ```
57///
58/// # See Also
59/// - [`Handler`]: For the handler trait that your async function must implement.
60/// - [`FromRequest`]: For handling the conversion of incoming data into request types.
61/// - [`Respondable`]: For handling the conversion of response data into a Lambda-compatible format.
62/// - [`LambdaHandlerService`]: For the service that integrates the handler with AWS Lambda.
63///
64/// # Errors
65/// Any errors that arise during request processing or handler execution will be propagated through the [`LambdaHandlerService`].
66pub fn handler_runtime<H, Args>(handler: H) -> LambdaHandlerService<H, Args>
67where
68  H: Handler<Args> + Clone,
69  H::Future: Future<Output = H::Output> + Send,
70  H::Output: Respondable,
71  Args: FromRequest + Send + Sync + 'static,
72  Args::Error: Send,
73{
74  LambdaHandlerService::new(handler)
75}
76
77/// Wraps an [`App`] in a runtime environment compatible with AWS Lambda.
78///
79/// This function sets up a [`LambdaAppService`] to process events in an AWS Lambda runtime,
80/// using the provided [`App`] instance. It simplifies the integration of applications with Lambda
81/// by providing a service that manages incoming requests and outgoing responses.
82///
83/// # Parameters
84/// - `app`: An instance of [`App`] that represents the application to be run in the Lambda environment.
85///
86/// # Returns
87/// A [`LambdaAppService`] that can process incoming Lambda events using the provided application.
88///
89/// # Examples
90///
91/// ```rust
92/// use titan::App;
93///
94/// #[tokio::main]
95/// async fn main() {
96///   let app = App::default(); // Empty app example
97///
98///   // Uncomment last line to run this example
99///   titan_lambda::app_runtime(app).run(); // .await.unwrap();
100/// }
101/// ```
102///
103/// # See Also
104/// - [`App`]: For implementing the application logic.
105/// - [`LambdaAppService`]: For the service that integrates with AWS Lambda.
106///
107/// # Errors
108/// Any errors encountered during the processing of requests are handled and propagated
109/// by the [`LambdaAppService`].
110pub fn app_runtime(app: App) -> LambdaAppService {
111  LambdaAppService::new(app)
112}