Skip to main content

handler

Attribute Macro handler 

Source
#[handler]
Expand description

Attribute macro for controller handler methods

Transforms handler functions to automatically extract typed parameters from HTTP requests using the FromRequest trait.

§Examples

§With Request parameter:

use ferro::{handler, Request, Response, json_response};

#[handler]
pub async fn index(req: Request) -> Response {
    json_response!({ "message": "Hello" })
}

§With FormRequest parameter:

use ferro::{handler, Response, json_response, request};

#[request]
pub struct CreateUserRequest {
    #[validate(email)]
    pub email: String,
}

#[handler]
pub async fn store(form: CreateUserRequest) -> Response {
    // `form` is already validated - returns 422 if invalid
    json_response!({ "email": form.email })
}

§Without parameters:

#[handler]
pub async fn health_check() -> Response {
    json_response!({ "status": "ok" })
}