Skip to main content

export_middleware_module

Macro export_middleware_module 

Source
macro_rules! export_middleware_module {
    ($middleware_type:ty) => { ... };
    (env: $env_var:literal, $middleware_type:ty) => { ... };
    ($module_name:expr, $middleware_type:ty) => { ... };
}
Expand description

Macro to generate all required module export functions This eliminates boilerplate code for new modules

§Usage Options

use httpward_core::export_middleware_module;
use httpward_core::httpward_middleware::middleware_trait::HttpWardMiddleware;
use httpward_core::httpward_middleware::next::Next;
use rama::{Context, http::{Body, Request, Response}};
use async_trait::async_trait;
use std::convert::Infallible;

#[derive(Default)]
struct MyMiddleware;

#[async_trait]
impl HttpWardMiddleware for MyMiddleware {
    fn name(&self) -> Option<&'static str> {
        Some("my_middleware")
    }

    async fn handle(
        &self,
        _ctx: Context<()>,
        request: Request<Body>,
        next: Next<'_>,
    ) -> Result<Response<Body>, Box<dyn std::error::Error + Send + Sync>> {
        next.run(_ctx, request).await
    }
}

export_middleware_module!(MyMiddleware);  // Uses Cargo.toml name

§2. Custom module name

use httpward_core::export_middleware_module;
use httpward_core::httpward_middleware::middleware_trait::HttpWardMiddleware;
use httpward_core::httpward_middleware::next::Next;
use rama::{Context, http::{Body, Request, Response}};
use async_trait::async_trait;

#[derive(Default)]
struct MyMiddleware;

#[async_trait]
impl HttpWardMiddleware for MyMiddleware {
    fn name(&self) -> Option<&'static str> {
        Some("my_middleware")
    }

    async fn handle(
        &self,
        _ctx: Context<()>,
        request: Request<Body>,
        next: Next<'_>,
    ) -> Result<Response<Body>, Box<dyn std::error::Error + Send + Sync>> {
        next.run(_ctx, request).await
    }
}

export_middleware_module!("custom_name", MyMiddleware);

§3. Environment variable name (example with literal)

use httpward_core::export_middleware_module;
use httpward_core::httpward_middleware::middleware_trait::HttpWardMiddleware;
use httpward_core::httpward_middleware::next::Next;
use rama::{Context, http::{Body, Request, Response}};
use async_trait::async_trait;

#[derive(Default)]
struct MyMiddleware;

#[async_trait]
impl HttpWardMiddleware for MyMiddleware {
    fn name(&self) -> Option<&'static str> {
        Some("my_middleware")
    }

    async fn handle(
        &self,
        _ctx: Context<()>,
        request: Request<Body>,
        next: Next<'_>,
    ) -> Result<Response<Body>, Box<dyn std::error::Error + Send + Sync>> {
        next.run(_ctx, request).await
    }
}

export_middleware_module!("my_module_name", MyMiddleware);

§Generated Functions

  • module_set_logger - Sets up module logger with the given name
  • create_middleware - Creates middleware instance of type T
  • destroy_middleware - Destroys middleware instance