Skip to main content

handler

Macro handler 

Source
macro_rules! handler {
    ($name:ident, |$req:ident : Request| $body:expr) => { ... };
}
Expand description

Define a handler function.

This macro generates the #[no_mangle] extern "C" boilerplate so your handler is a plain Rust closure that receives a Request and returns a Response.

use cufflink_fn::prelude::*;

cufflink_fn::init!();

handler!(get_stats, |req: Request| {
    let rows = db::query("SELECT COUNT(*) as total FROM orders");
    Response::json(&json!({"total": rows[0]["total"]}))
});

handler!(create_order, |req: Request| {
    let body = req.body();
    let customer = body["customer_id"].as_str().unwrap_or("unknown");
    db::execute(&format!(
        "INSERT INTO orders (customer_id, status) VALUES ('{}', 'pending')",
        customer
    ));
    Response::json(&json!({"status": "created"}))
});