Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new lambda feature #824

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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