Skip to main content

handler

Attribute Macro handler 

Source
#[handler]
Expand description

Turns a function into a documented api handler

/// The first line is a `summary` available in openapi
///
/// The entire docstring is available as `description` in openapi
#[swaggapi::handler(Get, "/")]
async fn index() -> &'static str {
    "Hello World"
}

/// Deletes the entire application's state
///
/// This endpoint is very dangerous and can only be used by admins.
#[swaggapi::delete("/deleteAll", tags("admin", "dangerous"))]
async fn delete_all() -> () {
    // ...
}

§Arguments

  • method: The HTTP method this handler should respond to

    • required
    • one of Get, Post, Put, Delete, Head, Options, Patch, Trace, for example method = Get
  • path: The HTTP url this handler should respond on

    Note, the ApiContext can be used to apply a common prefix to a set of handlers.

    • required
    • a string literal, for example path = "/"
  • tags: A list of tags, mostly used to group handlers

    Read the OpenAPI specification for the full details

    • optional
    • list of string literal, for example tags("foo", "bar)

§Positional arguments

Since method and path are required, they can alternatively be passed as positional arguments:

  • #[handler(Get, "/")]
  • #[handler(Get, path = "/")]
  • #[handler("/", method = Get)]

§Variants

The first argument method can be replaced with the usage of one specialized variant of #[handler]: