[][src]Macro lando::gateway

macro_rules! gateway {
    (@module ($module:ident, $py2:ident, $py3:ident)
     @handlers ($($handler:expr => $target:expr),*)) => { ... };
    (crate $module:tt { $($handler:expr => $target:expr),* }) => { ... };
    (crate $module:tt { $($handler:expr => $target:expr,)* }) => { ... };
    ($($handler:expr => $target:expr),*) => { ... };
    ($($handler:expr => $target:expr,)*) => { ... };
    ($f:expr) => { ... };
}

A macro that exposes a Lambda function handler for AWS API gateway proxy event triggers.

Lambda functions accept two arguments (the event, a lando::Request, and a context, a LambdaContext) and are expected to return a result containing lando::Response. The function signature should look like:

fn handler(
  request: Request,
  context: LambdaContext
) -> Result<impl IntoResponse> {
  // impl...
}

To use this macro, you need the following macro_use declaration

#[macro_use]
extern crate lando;

Examples

You can export a lambda-ready function by wrapping a closure with gateway!:

gateway!(|request, _| {
Ok(lando::Response::new(format!(
      "hello {}",
       request
           .path_parameters()
           .get("name")
           .unwrap_or_else(|| "stranger")
   )))
});

You can also the provide gateway! macro with a function reference

The request argument is just a regular http::Request type, extendable with API gateway features, like accessing path and query string parameters, and more by importing lando::RequestExt`

The context argument is same type defined within the crowbar crate.


use lando::{LambdaContext, Request, Result, IntoResponse};

fn handler(
  request: Request,
  context: LambdaContext
) -> Result<impl IntoResponse> {
  println!("{:?}", request);
  Ok("👍")
}

gateway!(handler);

Export multiple lambda functions in one library

You can export multiple functions in the same module with a format similar to a match expression:


use lando::Response;

gateway! {
    "one" => |request, context| { Ok(Response::new("1")) },
    "two" => |request, context| { Ok(Response::new("2")) }
}