Macro lando::gateway [] [src]

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) => { ... };
}

Macro to wrap a Lambda function handler for API gateway events.

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

This example is not tested
fn handler(request: Request, context: LambdaContext) -> Result

To use this macro, you need to macro_use both crowbar and cpython, because crowbar references multiple cpython macros.

This example is not tested
#[macro_use(gateway)]
extern crate lando;
#[macro_use]
extern crate cpython;

Examples

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

gateway!(|request, context| {
    println!("{:?}", request);
    Ok(lando::Response::new(()))
});

You can also the provide gateway! macro with a named function:

use lando::{LambdaContext, Request, Response, Result, Body};

fn handler(request: Request, context: LambdaContext) -> Result {
    println!("{:?}", request);
    Ok(Response::new(":thumbsup:".into()))
}

gateway!(handler);

Multiple functions

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

use lando::Response;

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

Changing the dynamic library name

Be default, lando assumes a library named "lambda", If you need to change the name of the resulting dynamic library that gets built, you first need to change the [lib] section in your Cargo.toml file

[lib]
name = "solo"
crate-type = ["cdylib"]

You then also need to change the names of the library indentifiers, expected by the cpython crate, by using the following gateway! format

gateway! {
    crate (libsolo, initlibsolo, PyInit_libsolo) {
        "handler" => |request, context| {
           Ok(lando::Response::new(
              "hello from libsolo"
           ))
        }
    }
};