Skip to content

Commit

Permalink
feat: add new lambda feature
Browse files Browse the repository at this point in the history
For interacting with `lambda_http`, part of the official awslabs toolkit.
  • Loading branch information
Fishrock123 committed Apr 22, 2021
1 parent d81eabc commit ed1d862
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ rustdoc-args = ["--cfg", "feature=\"docs\""]

[features]
default = ["h1-server", "cookies", "logger", "sessions"]
docs = ["unstable"]
unstable = []

cookies = ["http-types/cookies"]
h1-server = ["async-h1"]
lambda = ["lambda_http", "http", "http-types/hyperium_http"]
logger = ["femme"]
docs = ["unstable"]
sessions = ["async-session", "cookies"]
unstable = []

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

# feature = lambda
lambda_http = { version = "0.3.0", optional = true }
http = { version = "0.2.4", optional = true }

[dev-dependencies]
async-std = { version = "1.6.5", features = ["unstable", "attributes"] }
criterion = "0.3.3"
Expand Down
39 changes: 39 additions & 0 deletions src/lambda.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::convert::TryInto;
use std::future::Future;
use std::pin::Pin;

use crate::http::Error;
use crate::{Body, Server};

impl<State> lambda_http::Handler for Server<State>
where
State: Clone + Send + Sync + 'static,
{
type Error = Error;
type Response = lambda_http::Response<lambda_http::Body>;
type Fut = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;

fn call(&self, event: lambda_http::Request, context: lambda_http::Context) -> Self::Fut {
let server = self.clone();
Box::pin(async move {
let (parts, body) = event.into_parts();
let body = match body {
lambda_http::Body::Empty => Body::empty(),
lambda_http::Body::Text(text) => Body::from_string(text),
lambda_http::Body::Binary(bytes) => Body::from_bytes(bytes),
};
let mut req: http_types::Request = http::Request::from_parts(parts, body).try_into()?;

req.ext_mut().insert(context);
let res: http_types::Response = server.respond(req).await?;

let res: http::Response<Body> = res.try_into()?;
let (parts, body) = res.into_parts();
let body = match body.is_empty() {
Some(true) => lambda_http::Body::Empty,
_ => lambda_http::Body::Binary(body.into_bytes().await?)
};
Ok(http::Response::from_parts(parts, body))
})
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ mod route;
mod router;
mod server;

#[cfg(feature = "lambda")]
mod lambda;

pub mod convert;
pub mod listener;
pub mod log;
Expand Down

0 comments on commit ed1d862

Please sign in to comment.