Skip to main content

handle_request

Macro handle_request 

Source
macro_rules! handle_request {
    ($handler:ident) => { ... };
}
Expand description

Wire up an HTTP flare entrypoint with a single line.

Pass the name of a function that takes no arguments and returns a FlareAction. The macro expands to:

  • the alloc export the host runtime requires (via export_alloc!),
  • a #[no_mangle] extern "C" fn handle_request() -> i64 that resets the bump arena, calls your handler, and encodes the action for the host.

Read inbound request data from the request module and write your response with response. Returning the action - rather than calling a host function - lets the host decide whether to ship the response, transform an upstream response, or pass through to origin.

use flaron_sdk::{request, response, FlareAction};

flaron_sdk::handle_request!(my_flare);

fn my_flare() -> FlareAction {
    let body = format!("hello from {} {}", request::method(), request::url());
    response::set_status(200);
    response::set_header("content-type", "text/plain");
    response::set_body_str(&body);
    FlareAction::Respond
}

Use ws_handlers! for WebSocket flares instead - the two macros are mutually exclusive within one crate because they both define alloc.