Skip to main content

handler

Attribute Macro handler 

Source
#[handler]
Expand description

Converts a function or impl block into a Salvo Handler.

On a function, #[handler] generates a zero-sized handler type with the same name as the function and implements salvo::Handler for it. Salvo injects any supported arguments by type, so handlers can list only the values they need and in any order:

  • &mut Request
  • &mut Depot
  • &mut Response
  • &mut FlowCtrl
  • extractible request data

A return value that implements Salvo’s writer traits is written to the response automatically.

On an impl block, the method named handle is used as the implementation of Handler for the implementing type.

§Examples

use salvo_core::prelude::*;

#[handler]
async fn hello() -> &'static str {
    "Hello, world!"
}
use salvo_core::prelude::*;

struct Health;

#[handler]
impl Health {
    fn handle() -> &'static str {
        "ok"
    }
}

See salvo_core::handler for the full handler guide.