17
17
//! your function's execution path.
18
18
//!
19
19
//! ```rust,no_run
20
- //! use lambda_http::{handler, lambda_runtime::{self, Error} };
20
+ //! use lambda_http::{service_fn, Error};
21
21
//!
22
22
//! #[tokio::main]
23
23
//! async fn main() -> Result<(), Error> {
24
24
//! // initialize dependencies once here for the lifetime of your
25
25
//! // lambda task
26
- //! lambda_runtime ::run(handler (|request, context | async { Ok("👋 world!") })).await?;
26
+ //! lambda_http ::run(service_fn (|request| async { Ok("👋 world!") })).await?;
27
27
//! Ok(())
28
28
//! }
29
29
//! ```
34
34
//! with the [`RequestExt`](trait.RequestExt.html) trait.
35
35
//!
36
36
//! ```rust,no_run
37
- //! use lambda_http::{handler, lambda_runtime::{self, Context, Error} , IntoResponse, Request, RequestExt};
37
+ //! use lambda_http::{service_fn, Error, IntoResponse, Request, RequestExt};
38
38
//!
39
39
//! #[tokio::main]
40
40
//! async fn main() -> Result<(), Error> {
41
- //! lambda_runtime ::run(handler (hello)).await?;
41
+ //! lambda_http ::run(service_fn (hello)).await?;
42
42
//! Ok(())
43
43
//! }
44
44
//!
45
45
//! async fn hello(
46
- //! request: Request,
47
- //! _: Context
46
+ //! request: Request
48
47
//! ) -> Result<impl IntoResponse, Error> {
49
48
//! Ok(format!(
50
49
//! "hello {}",
62
61
extern crate maplit;
63
62
64
63
pub use http:: { self , Response } ;
65
- pub use lambda_runtime:: { self , Context } ;
66
- use lambda_runtime:: {
67
- tower:: util:: { service_fn, ServiceFn } ,
68
- Error , LambdaEvent , Service ,
69
- } ;
64
+ pub use lambda_runtime:: { self , tower:: util:: service_fn, Context , Error } ;
65
+ use lambda_runtime:: { LambdaEvent , Service } ;
70
66
71
67
mod body;
72
68
pub mod ext;
@@ -88,27 +84,10 @@ use std::{
88
84
/// Type alias for `http::Request`s with a fixed [`Body`](enum.Body.html) type
89
85
pub type Request = http:: Request < Body > ;
90
86
91
- /// Wraps a function that takes 2 arguments into one that only takes a [`LambdaEvent`]
92
- fn handler_wrapper < A , Fut > ( f : impl Fn ( A , Context ) -> Fut ) -> impl Fn ( LambdaEvent < A > ) -> Fut {
93
- move |req| f ( req. event , req. context )
94
- }
95
-
96
- /// Adapts a [`Service`] into another [`Service`].
97
- pub fn handler < ' a , R , Fut > (
98
- f : impl Fn ( Request , Context ) -> Fut ,
99
- ) -> Adapter < ' a , R , ServiceFn < impl Fn ( LambdaEvent < Request > ) -> Fut > >
100
- where
101
- R : IntoResponse ,
102
- Fut : Future < Output = Result < R , Error > > ,
103
- {
104
- Adapter {
105
- service : service_fn ( handler_wrapper ( f) ) ,
106
- _phantom_data : PhantomData ,
107
- }
108
- }
109
-
110
- #[ doc( hidden) ]
111
- pub struct TransformResponse < ' a , R , E > {
87
+ /// Future that will convert an [`IntoResponse`] into an actual [`LambdaResponse`]
88
+ ///
89
+ /// This is used by the `Adapter` wrapper and is completely internal to the `lambda_http::run` function.
90
+ struct TransformResponse < ' a , R , E > {
112
91
request_origin : RequestOrigin ,
113
92
fut : Pin < Box < dyn Future < Output = Result < R , E > > + Send + ' a > > ,
114
93
}
@@ -129,15 +108,31 @@ where
129
108
}
130
109
}
131
110
132
- #[ doc( hidden) ]
133
- pub struct Adapter < ' a , R , S > {
111
+ /// Wraps a `Service<Request>` in a `Service<LambdaEvent<Request>>`
112
+ ///
113
+ /// This is completely internal to the `lambda_http::run` function.
114
+ struct Adapter < ' a , R , S > {
134
115
service : S ,
135
116
_phantom_data : PhantomData < & ' a R > ,
136
117
}
137
118
119
+ impl < ' a , R , S > From < S > for Adapter < ' a , R , S >
120
+ where
121
+ S : Service < Request , Response = R , Error = Error > + Send ,
122
+ S :: Future : Send + ' a ,
123
+ R : IntoResponse ,
124
+ {
125
+ fn from ( service : S ) -> Self {
126
+ Adapter {
127
+ service,
128
+ _phantom_data : PhantomData ,
129
+ }
130
+ }
131
+ }
132
+
138
133
impl < ' a , R , S > Service < LambdaEvent < LambdaRequest < ' a > > > for Adapter < ' a , R , S >
139
134
where
140
- S : Service < LambdaEvent < Request > , Response = R , Error = Error > + Send ,
135
+ S : Service < Request , Response = R , Error = Error > + Send ,
141
136
S :: Future : Send + ' a ,
142
137
R : IntoResponse ,
143
138
{
@@ -151,7 +146,22 @@ where
151
146
152
147
fn call ( & mut self , req : LambdaEvent < LambdaRequest < ' a > > ) -> Self :: Future {
153
148
let request_origin = req. event . request_origin ( ) ;
154
- let fut = Box :: pin ( self . service . call ( LambdaEvent :: new ( req. event . into ( ) , req. context ) ) ) ;
149
+ let event: Request = req. event . into ( ) ;
150
+ let fut = Box :: pin ( self . service . call ( event. with_lambda_context ( req. context ) ) ) ;
155
151
TransformResponse { request_origin, fut }
156
152
}
157
153
}
154
+
155
+ /// Starts the Lambda Rust runtime and begins polling for events on the [Lambda
156
+ /// Runtime APIs](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html).
157
+ ///
158
+ /// This takes care of transforming the LambdaEvent into a [`Request`] and then
159
+ /// converting the result into a [`LambdaResponse`].
160
+ pub async fn run < ' a , S , R > ( handler : S ) -> Result < ( ) , Error >
161
+ where
162
+ S : Service < Request , Response = R , Error = Error > + Send ,
163
+ S :: Future : Send + ' a ,
164
+ R : IntoResponse ,
165
+ {
166
+ lambda_runtime:: run ( Adapter :: from ( handler) ) . await
167
+ }
0 commit comments