Skip to content

Commit 1f82b1c

Browse files
committed
feat: add new lambda feature
For interacting with `lambda_http`, part of the official awslabs toolkit.
1 parent d81eabc commit 1f82b1c

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

Cargo.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ rustdoc-args = ["--cfg", "feature=\"docs\""]
2525

2626
[features]
2727
default = ["h1-server", "cookies", "logger", "sessions"]
28+
docs = ["unstable"]
29+
unstable = []
30+
2831
cookies = ["http-types/cookies"]
2932
h1-server = ["async-h1"]
33+
lambda = ["lambda_http", "http", "http-types/hyperium_http"]
3034
logger = ["femme"]
31-
docs = ["unstable"]
3235
sessions = ["async-session", "cookies"]
33-
unstable = []
3436

3537
[dependencies]
3638
async-h1 = { version = "2.3.0", optional = true }
@@ -49,6 +51,10 @@ route-recognizer = "0.2.0"
4951
serde = "1.0.117"
5052
serde_json = "1.0.59"
5153

54+
# feature = lambda
55+
lambda_http = { version = "0.3.0", optional = true }
56+
http = { version = "0.2.4", optional = true }
57+
5258
[dev-dependencies]
5359
async-std = { version = "1.6.5", features = ["unstable", "attributes"] }
5460
criterion = "0.3.3"

src/lambda.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use std::convert::TryInto;
2+
use std::future::Future;
3+
use std::pin::Pin;
4+
5+
use crate::http::Error;
6+
use crate::{Body, Server};
7+
8+
impl<State> lambda_http::Handler for Server<State>
9+
where
10+
State: Clone + Send + Sync + 'static,
11+
{
12+
type Error = Error;
13+
type Response = lambda_http::Response<lambda_http::Body>;
14+
type Fut = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;
15+
16+
fn call(&self, event: lambda_http::Request, context: lambda_http::Context) -> Self::Fut {
17+
let server = self.clone();
18+
Box::pin(async move {
19+
let (parts, body) = event.into_parts();
20+
let body = match body {
21+
lambda_http::Body::Empty => Body::empty(),
22+
lambda_http::Body::Text(text) => Body::from_string(text),
23+
lambda_http::Body::Binary(bytes) => Body::from_bytes(bytes),
24+
};
25+
let mut req: http_types::Request = http::Request::from_parts(parts, body).try_into()?;
26+
27+
req.ext_mut().insert(context);
28+
let res: http_types::Response = server.respond(req).await?;
29+
30+
let res: http::Response<Body> = res.try_into()?;
31+
let (parts, body) = res.into_parts();
32+
let body = match body.is_empty() {
33+
Some(true) => lambda_http::Body::Empty,
34+
_ => lambda_http::Body::Binary(body.into_bytes().await?)
35+
};
36+
Ok(http::Response::from_parts(parts, body))
37+
})
38+
}
39+
}

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ mod route;
7575
mod router;
7676
mod server;
7777

78+
// #[cfg(feature = "lambda")]
79+
mod lambda;
80+
7881
pub mod convert;
7982
pub mod listener;
8083
pub mod log;

0 commit comments

Comments
 (0)