pub struct Next { /* private fields */ }Expand description
Represents the next step in the middleware chain.
Next is a continuation that encapsulates the remaining middleware and the final
handler. When called via run(), it executes the next middleware (or handler) in
the chain and returns the response.
§Cloning
Next is cheap to clone as it uses Arc internally for shared ownership.
§Examples
§Basic Usage
use ignitia::middleware::{Middleware, Next};
use ignitia::{Request, Response};
#[derive(Clone)]
struct LoggingMiddleware;
#[async_trait::async_trait]
impl Middleware for LoggingMiddleware {
async fn handle(&self, req: Request, next: Next) -> Response {
println!("Processing request...");
// Call next middleware/handler
let response = next.run(req).await;
println!("Request processed");
response
}
}§Conditional Next Execution
use ignitia::middleware::{Middleware, Next};
use ignitia::{Request, Response, StatusCode};
#[derive(Clone)]
struct AuthMiddleware;
#[async_trait::async_trait]
impl Middleware for AuthMiddleware {
async fn handle(&self, req: Request, next: Next) -> Response {
if req.header("authorization").is_some() {
// Authorized - continue chain
next.run(req).await
} else {
// Not authorized - don't call next
Response::new(StatusCode::UNAUTHORIZED)
}
}
}Implementations§
Source§impl Next
impl Next
Sourcepub fn new<F>(f: F) -> Self
pub fn new<F>(f: F) -> Self
Create a new Next continuation.
This method is used internally by the framework to build the middleware chain. It wraps a function that takes a request and returns a future resolving to a response.
§Arguments
func- Function representing the next step in the chain
§Returns
Returns a new Next instance.
§Examples
use ignitia::middleware::Next;
use ignitia::{Request, Response};
let next = Next::new(|req: Request| {
Box::pin(async move {
Response::text("Hello from next")
})
});Sourcepub async fn run(self, req: Request) -> Response
pub async fn run(self, req: Request) -> Response
Execute the next step in the middleware chain.
This method invokes the next middleware or handler in the chain with the given request and awaits its completion, returning the resulting response.
§Arguments
req- The HTTP request to pass to the next step
§Returns
Returns the HTTP Response from the next step in the chain.
§Examples
§Simple Pass-through
use ignitia::middleware::{Middleware, Next};
use ignitia::{Request, Response};
#[derive(Clone)]
struct PassThroughMiddleware;
#[async_trait::async_trait]
impl Middleware for PassThroughMiddleware {
async fn handle(&self, req: Request, next: Next) -> Response {
// Simply pass request to next step
next.run(req).await
}
}§Measuring Execution Time
use ignitia::middleware::{Middleware, Next};
use ignitia::{Request, Response};
use std::time::Instant;
#[derive(Clone)]
struct TimerMiddleware;
#[async_trait::async_trait]
impl Middleware for TimerMiddleware {
async fn handle(&self, req: Request, next: Next) -> Response {
let start = Instant::now();
// Run next middleware/handler
let response = next.run(req).await;
let duration = start.elapsed();
println!("Request took: {:?}", duration);
response
}
}