Skip to content
Closed
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
1,197 changes: 0 additions & 1,197 deletions Cargo.lock

This file was deleted.

File renamed without changes.
2 changes: 1 addition & 1 deletion lambda-http/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub enum PayloadError {
/// }
///
/// fn main() {
/// lambda!(handler)
/// lambda!(handler);
/// }
///
/// fn handler(
Expand Down
45 changes: 27 additions & 18 deletions lambda-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! use lambda_runtime::{Context, error::HandlerError};
//!
//! fn main() {
//! lambda!(hello)
//! lambda!(hello);
//! }
//!
//! fn hello(
Expand Down Expand Up @@ -49,7 +49,7 @@
//! .unwrap_or_else(|| "stranger")
//! )
//! )
//! )
//! );
//! }
//! ```

Expand All @@ -58,9 +58,13 @@
#[macro_use]
extern crate maplit;

pub use tokio;

pub use http::{self, Response};

use lambda_runtime::{self as lambda, error::HandlerError, Context};
use tokio::runtime::Runtime as TokioRuntime;

use tokio::prelude::future::{Future, IntoFuture};

mod body;
mod ext;
Expand All @@ -75,16 +79,19 @@ use crate::{request::LambdaRequest, response::LambdaResponse};
pub type Request = http::Request<Body>;

/// Functions serving as ALB and API Gateway handlers must conform to this type.
pub trait Handler<R> {
pub trait Handler<Ret, Fut>: Send
where Fut: IntoFuture<Item=Ret, Error=HandlerError> {
/// Run the handler.
fn run(&mut self, event: Request, ctx: Context) -> Result<R, HandlerError>;
fn run(&mut self, event: Request, ctx: Context) -> Fut;
}

impl<F, R> Handler<R> for F
impl<Fun, Ret, Fut> Handler<Ret, Fut> for Fun
where
F: FnMut(Request, Context) -> Result<R, HandlerError>,
Fun: FnMut(Request, Context) -> Fut + Send,
Fut: IntoFuture<Item=Ret, Error=HandlerError> + Send,
Fut::Future: Send + 'static,
{
fn run(&mut self, event: Request, ctx: Context) -> Result<R, HandlerError> {
fn run(&mut self, event: Request, ctx: Context) -> Fut {
(*self)(event, ctx)
}
}
Expand All @@ -97,35 +104,37 @@ where
///
/// # Panics
/// The function panics if the Lambda environment variables are not set.
pub fn start<R>(f: impl Handler<R>, runtime: Option<TokioRuntime>)
pub fn start<Ret, Fut>(f: impl Handler<Ret, Fut>) -> impl Future<Item=(), Error=()>
where
R: IntoResponse,
Ret: IntoResponse,
Fut: IntoFuture<Item=Ret, Error=HandlerError> + Send,
Fut::Future: Send + 'static,
{
// handler requires a mutable ref
let mut func = f;
lambda::start(
|req: LambdaRequest<'_>, ctx: Context| {
move |req: LambdaRequest<'_>, ctx: Context| {
let is_alb = req.request_context.is_alb();
func.run(req.into(), ctx)
.map(|resp| LambdaResponse::from_response(is_alb, resp.into_response()))
func.run(req.into(), ctx).into_future()
.map(move |resp| LambdaResponse::from_response(is_alb, resp.into_response()))
},
runtime,
// runtime,
)
}

/// A macro for starting new handler's poll for API Gateway and ALB events
#[macro_export]
macro_rules! lambda {
($handler:expr) => {
$crate::start($handler, None)
$crate::tokio::run($crate::start($handler))
};
($handler:expr, $runtime:expr) => {
$crate::start($handler, Some($runtime))
$runtime.spawn($crate::start($handler))
};
($handler:ident) => {
$crate::start($handler, None)
$crate::tokio::run($crate::start($handler))
};
($handler:ident, $runtime:expr) => {
$crate::start($handler, Some($runtime))
$runtime.spawn($crate::start($handler))
};
}
3 changes: 2 additions & 1 deletion lambda-runtime-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ lambda_runtime_errors = { path = "../lambda-runtime-errors", version = "^0.1" }
failure = "^0.1"

[dev-dependencies]
chrono = "^0.4"
chrono = "^0.4"
lambda_runtime_errors_derive = { path = "../lambda-runtime-errors-derive", version = "^0.1" }
Loading