class Azu::HandlerPipeline

Overview

Fluent builder for composing HTTP handler middleware pipelines

The HandlerPipeline provides a clean, chainable API for building middleware stacks. Handlers are executed in the order they are added.

Example:

pipeline = Azu::HandlerPipeline.new
  .use(Azu::Handler::Rescuer.new)
  .use(Azu::Handler::CORS.new(origins: ["https://example.com"]))
  .use(Azu::Handler::CSRF.new)
  .use { |ctx| puts "Request: #{ctx.request.path}" }
  .build

# Use with HTTP::Server
server = HTTP::Server.new(pipeline) { |ctx| ... }

Defined in:

azu/handler_pipeline.cr

Instance Method Summary

Instance Method Detail

def build : HTTP::Handler #

Build the handler chain

Returns the first handler in the chain with all handlers linked together. Raises if the pipeline is empty.


[View source]
def build? : HTTP::Handler | Nil #

Build the handler chain, returning nil if empty


[View source]
def clear : self #

Clear all handlers from the pipeline


[View source]
def empty? : Bool #

Returns true if no handlers have been added


[View source]
def size : Int32 #

Returns the number of handlers in the pipeline


[View source]
def use(handler : HTTP::Handler) : self #

Add a handler to the pipeline

Handlers are executed in the order they are added. Each handler should call call_next(context) to continue the chain.


[View source]
def use(&block : HTTP::Server::Context -> Nil) : self #

Add a block-based handler to the pipeline

The block receives the HTTP context and should process the request. The next handler in the chain is called automatically after the block.

Example:

pipeline.use { |ctx| ctx.response.headers["X-Custom"] = "value" }

[View source]
def use_if(condition : Bool, handler : HTTP::Handler) : self #

Add a handler conditionally

The handler is only added if the condition is true.

Example:

pipeline.use_if(ENV["ENABLE_CORS"]? == "true", cors_handler)

[View source]