class
Azu::HandlerPipeline
- Azu::HandlerPipeline
- Reference
- Object
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.crInstance Method Summary
-
#build : HTTP::Handler
Build the handler chain
-
#build? : HTTP::Handler | Nil
Build the handler chain, returning nil if empty
-
#clear : self
Clear all handlers from the pipeline
-
#empty? : Bool
Returns true if no handlers have been added
-
#size : Int32
Returns the number of handlers in the pipeline
-
#use(handler : HTTP::Handler) : self
Add a handler to the pipeline
-
#use(&block : HTTP::Server::Context -> Nil) : self
Add a block-based handler to the pipeline
-
#use_if(condition : Bool, handler : HTTP::Handler) : self
Add a handler conditionally
Instance Method Detail
Build the handler chain
Returns the first handler in the chain with all handlers linked together. Raises if the pipeline is empty.
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.
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" }
Add a handler conditionally
The handler is only added if the condition is true.
Example:
pipeline.use_if(ENV["ENABLE_CORS"]? == "true", cors_handler)