Skip to main content

prologue_hooks

Attribute Macro prologue_hooks 

Source
#[prologue_hooks]
Expand description

Executes multiple specified functions before the main handler function.

This attribute macro configures multiple pre-execution hooks that run before the main function logic. The specified hook functions will be called in the order provided, followed by the main function execution.

§Usage

use hyperlane::*;
use hyperlane_macros::*;

struct PrologueHooks;

impl ServerHook for PrologueHooks {
    async fn new(_: &mut Stream, _: &mut Context) -> Self {
        Self
    }

    #[is_get_method]
    #[is_http_version]
    async fn handle(self, _: &mut Stream, ctx: &mut Context) -> Status { Status::Continue }
}

async fn prologue_hooks_fn(stream: &mut Stream, ctx: &mut Context) {
    let hook = PrologueHooks::new(stream, ctx).await;
    hook.handle(stream, ctx).await;
}

#[route("/hook")]
struct Hook;

impl ServerHook for Hook {
    async fn new(_: &mut Stream, _: &mut Context) -> Self {
        Self
    }

    #[prologue_hooks(prologue_hooks_fn)]
    #[response_body("Testing hook macro")]
    async fn handle(self, stream: &mut Stream, ctx: &mut Context) -> Status { Status::Continue }
}

The macro accepts a comma-separated list of function names as parameters. All hook functions and the main function must accept a Context parameter. Avoid combining this macro with other macros on the same function to prevent macro expansion conflicts.

§Advanced Usage with Method Expressions

use hyperlane::*;
use hyperlane_macros::*;

#[route("/hooks_expression")]
struct HooksExpression;

impl ServerHook for HooksExpression {
    async fn new(_: &mut Stream, _: &mut Context) -> Self {
        Self
    }

    #[is_get_method]
    #[prologue_hooks(HooksExpression::new_hook, HooksExpression::method_hook)]
    #[response_body("hooks expression test")]
    async fn handle(self, stream: &mut Stream, ctx: &mut Context) -> Status { Status::Continue }
}

impl HooksExpression {
    async fn new_hook(_: &mut Stream, ctx: &mut Context) -> Status { Status::Continue }

    async fn method_hook(_: &mut Stream, ctx: &mut Context) -> Status { Status::Continue }
}