Macro gateway::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 gateway::Request, and the context, a LambdaContext) and returns a value (a serde_json Value). The function signature should look like:

Be careful when using this code, it's not being tested!
fn handler(event: Request, context: LambdaContext) -> GatewayResult

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

Be careful when using this code, it's not being tested!
#[macro_use(gateway)]
extern crate gateway;
#[macro_use]
extern crate cpython;

Examples

You can wrap a closure with gateway!:

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

You can also define a named function:

use gateway::{Request, Response, LambdaContext, GatewayResult};

fn my_handler(request: Request, context: LambdaContext) -> GatewayResult {
    println!("{:?}", request);
    Ok(Response::builder().body(":thumbsup:").build())
}

gateway!(my_handler);

Multiple handlers

You can define multiple handlers in the same module in a way similar to match:

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

Changing the dynamic library name

If you need to change the name of the built dynamic library, you first need to change the [lib] section in Cargo.toml:

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

You then also need to change the names of the library symbols, which you can do by extending upon the multiple handler version of gateway!:

gateway! {
    crate (libkappa, initlibkappa, PyInit_libkappa) {
        "handler" => |request, context| {
           Ok(gateway::Response::builder().body(
              "hi from libkappa"
           ).build())
        }
    }
};