Handlers
Handlers provide middleware functionality for Azu applications, allowing you to process requests and responses at different stages of the request lifecycle.
Built-in Handlers
Azu::Handler::Rescuer
Handles exceptions and provides error responses.
Azu.start [
Azu::Handler::Rescuer.new
]Features:
Automatic exception handling
Development-friendly error pages
Production-safe error responses
Stack trace logging in development
Azu::Handler::Logger
Provides request/response logging.
Azu.start [
Azu::Handler::Logger.new
]Features:
Request method and path logging
Response status and timing
Error logging
Configurable log levels
Azu::Handler::CORS
Handles Cross-Origin Resource Sharing (CORS) headers.
Configuration:
origins- Allowed originsmethods- Allowed HTTP methodsheaders- Allowed headerscredentials- Allow credentials
Azu::Handler::Static
Serves static files from a directory.
Configuration:
directory- Directory to serve files fromprefix- URL prefix for static filesindex- Default file to serve for directories
Azu::Handler::CSRF
Provides CSRF protection for state-changing operations following OWASP recommendations.
Protection Strategies:
SignedDoubleSubmit
HMAC-signed token with timestamp validation
Recommended (default)
SynchronizerToken
Token stored in cookie, verified against form/header
Good
DoubleSubmit
Simple double submit cookie
Not recommended
Configuration Options:
skip_routes- Array of paths to bypass CSRF protectionstrategy- Protection strategy (default:SignedDoubleSubmit)secret_key- HMAC secret key (auto-generated if not provided)cookie_name- Cookie name (default:csrf_token)header_name- Header name for AJAX (default:X-CSRF-TOKEN)param_name- Form parameter name (default:_csrf)cookie_max_age- Token expiry in seconds (default: 86400 / 24 hours)cookie_same_site- SameSite policy (default:Strict)secure_cookies- Use secure cookies (default:true)
Helper Methods:
Strategy Selection:
Azu::Handler::Throttle
Provides rate limiting and DDoS protection.
Configuration Options:
interval- Duration in seconds until request counter resets (default: 5)duration- Duration in seconds to block an IP (default: 900 / 15 minutes)threshold- Number of requests allowed per interval (default: 100)blacklist- Array of IPs to immediately blockwhitelist- Array of IPs to always allow
Response:
When rate limited, returns HTTP 429 with Retry-After header.
Monitoring:
Azu::Handler::RequestId
Adds unique request IDs for distributed tracing.
Features:
Generates or uses existing
X-Request-IDheaderEnables request correlation across services
Useful for debugging and log aggregation
Azu::Handler::PerformanceMonitor
Tracks request and component performance metrics (compile-time optional).
Features:
Request processing time tracking
Component lifecycle metrics
Memory usage monitoring
Enable via
PERFORMANCE_MONITORING=truecompile flag
Custom Handlers
Create custom handlers by inheriting from Azu::Handler::Base.
Basic Handler
Handler with Configuration
Handler with State
Handler Lifecycle
Handlers are executed in the order they are added to the middleware stack.
Request Phase
Error Handling
Handler Registration
Application Level
Endpoint Level
Handler Configuration
Environment-based Configuration
Conditional Handlers
Handler Testing
Unit Testing
Integration Testing
Performance Considerations
Handler Order
Order handlers by their processing requirements:
Security handlers (CORS, CSRF)
Logging handlers (Logger)
Business logic handlers (Custom)
Error handlers (Rescuer)
Handler Efficiency
Common Patterns
Authentication Handler
Rate Limiting Handler
Caching Handler
Next Steps
Learn about Configuration
Explore Error Handling
Understand Middleware Patterns
Last updated
Was this helpful?
