Skip to content

Commit b10f35e

Browse files
committed
Added Config to Context.
1 parent 1f512a9 commit b10f35e

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

lambda/src/lib.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ pub struct HandlerFn<F> {
130130
impl<F, A, B, Error, Fut> Handler<A, B> for HandlerFn<F>
131131
where
132132
F: Fn(A, Context) -> Fut,
133-
Fut: Future<Output = Result<B, Error>> + Send ,
133+
Fut: Future<Output = Result<B, Error>> + Send,
134134
Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>> + fmt::Display,
135135
{
136136
type Error = Error;
@@ -170,6 +170,7 @@ where
170170
&self,
171171
incoming: impl Stream<Item = Result<http::Response<hyper::Body>, Error>> + Send,
172172
handler: F,
173+
config: &Config,
173174
) -> Result<(), Error>
174175
where
175176
F: Handler<A, B> + Send + Sync + 'static,
@@ -186,7 +187,9 @@ where
186187
let event = event?;
187188
let (parts, body) = event.into_parts();
188189

189-
let ctx: Context = Context::try_from(parts.headers)?;
190+
let ctx: Context = Context::try_from(parts.headers)
191+
.expect("Unable to get Context from http headers")
192+
.with_config(config);
190193
let body = hyper::body::to_bytes(body).await?;
191194
trace!("{}", std::str::from_utf8(&body)?); // this may be very verbose
192195
let body = serde_json::from_slice(&body)?;
@@ -328,23 +331,23 @@ where
328331
pub async fn run<A, B, F>(handler: F) -> Result<(), Error>
329332
where
330333
F: Handler<A, B> + Send + Sync + 'static,
331-
<F as Handler<A, B>>::Fut: Future<Output = Result<B, <F as Handler<A, B>>::Error>> + Send + 'static,
334+
<F as Handler<A, B>>::Fut: Future<Output = Result<B, <F as Handler<A, B>>::Error>> + Send + 'static,
332335
<F as Handler<A, B>>::Error: fmt::Display + Send + Sync + 'static,
333336
A: for<'de> Deserialize<'de> + Send + Sync + 'static,
334337
B: Serialize + Send + Sync + 'static,
335338
{
336339
trace!("Loading config from env");
337340
let config = Config::from_env()?;
338-
let uri = config.endpoint.try_into().expect("Unable to convert to URL");
341+
let uri = config.endpoint.as_str().try_into().expect("Unable to convert to URL");
339342
let runtime = Runtime::builder()
340343
.with_connector(HttpConnector::new())
341344
.with_endpoint(uri)
342345
.build()
343-
.expect("Unable create runtime");
346+
.expect("Unable to create runtime");
344347

345348
let client = &runtime.client;
346349
let incoming = incoming(client);
347-
runtime.run(incoming, handler).await
350+
runtime.run(incoming, handler, &config).await
348351
}
349352

350353
fn type_name_of_val<T>(_: T) -> &'static str {

lambda/src/types.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,13 @@ impl TryFrom<HeaderMap> for Context {
141141
Ok(ctx)
142142
}
143143
}
144+
145+
impl Context {
146+
/// Add environment details to the context by setting `env_config`.
147+
pub fn with_config(self, config: &Config) -> Self {
148+
Self {
149+
env_config: config.clone(),
150+
..self
151+
}
152+
}
153+
}

0 commit comments

Comments
 (0)