1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#![warn(missing_docs, rust_2018_idioms)]
#[cfg(test)]
#[macro_use]
extern crate maplit;
pub use http::{self, Response};
use lambda_runtime::LambdaEvent;
pub use lambda_runtime::{self, service_fn, tower, Context, Error, Service};
use request::RequestFuture;
use response::ResponseFuture;
pub mod ext;
pub mod request;
mod response;
pub use crate::{
ext::{RequestExt, RequestPayloadExt},
response::IntoResponse,
};
use crate::{
request::{LambdaRequest, RequestOrigin},
response::LambdaResponse,
};
pub use aws_lambda_events;
pub use aws_lambda_events::encodings::Body;
use std::{
future::Future,
marker::PhantomData,
pin::Pin,
task::{Context as TaskContext, Poll},
};
mod streaming;
pub use streaming::run_with_streaming_response;
pub type Request = http::Request<Body>;
#[doc(hidden)]
pub enum TransformResponse<'a, R, E> {
Request(RequestOrigin, RequestFuture<'a, R, E>),
Response(RequestOrigin, ResponseFuture),
}
impl<'a, R, E> Future for TransformResponse<'a, R, E>
where
R: IntoResponse,
{
type Output = Result<LambdaResponse, E>;
fn poll(mut self: Pin<&mut Self>, cx: &mut TaskContext<'_>) -> Poll<Self::Output> {
match *self {
TransformResponse::Request(ref mut origin, ref mut request) => match request.as_mut().poll(cx) {
Poll::Ready(Ok(resp)) => {
*self = TransformResponse::Response(origin.clone(), resp.into_response());
self.poll(cx)
}
Poll::Ready(Err(err)) => Poll::Ready(Err(err)),
Poll::Pending => Poll::Pending,
},
TransformResponse::Response(ref mut origin, ref mut response) => match response.as_mut().poll(cx) {
Poll::Ready(resp) => Poll::Ready(Ok(LambdaResponse::from_response(origin, resp))),
Poll::Pending => Poll::Pending,
},
}
}
}
#[doc(hidden)]
pub struct Adapter<'a, R, S> {
service: S,
_phantom_data: PhantomData<&'a R>,
}
impl<'a, R, S, E> From<S> for Adapter<'a, R, S>
where
S: Service<Request, Response = R, Error = E>,
S::Future: Send + 'a,
R: IntoResponse,
{
fn from(service: S) -> Self {
Adapter {
service,
_phantom_data: PhantomData,
}
}
}
impl<'a, R, S, E> Service<LambdaEvent<LambdaRequest>> for Adapter<'a, R, S>
where
S: Service<Request, Response = R, Error = E>,
S::Future: Send + 'a,
R: IntoResponse,
{
type Response = LambdaResponse;
type Error = E;
type Future = TransformResponse<'a, R, Self::Error>;
fn poll_ready(&mut self, cx: &mut core::task::Context<'_>) -> core::task::Poll<Result<(), Self::Error>> {
self.service.poll_ready(cx)
}
fn call(&mut self, req: LambdaEvent<LambdaRequest>) -> Self::Future {
let request_origin = req.payload.request_origin();
let event: Request = req.payload.into();
let fut = Box::pin(self.service.call(event.with_lambda_context(req.context)));
TransformResponse::Request(request_origin, fut)
}
}
pub async fn run<'a, R, S, E>(handler: S) -> Result<(), Error>
where
S: Service<Request, Response = R, Error = E>,
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,
};
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 {
println!("Lambda event: {event:#?}");
self.inner.call(event)
}
}
#[test]
fn adapter_is_boxable() {
let _service: BoxService<LambdaEvent<LambdaRequest>, LambdaResponse, http::Error> = ServiceBuilder::new()
.layer_fn(|service| {
LogService { inner: service }
})
.layer_fn(Adapter::from)
.service_fn(|_event: Request| async move { Response::builder().status(StatusCode::OK).body(Body::Empty) })
.boxed();
}
}