oxidite-middleware
HTTP middleware for Oxidite (CORS, logging, compression, rate limiting).
Overview
oxidite-middleware provides a collection of commonly needed HTTP middleware for the Oxidite web framework. Built on top of the tower ecosystem, it offers composable, reusable components that can be easily integrated into your application's request/response pipeline.
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Features
- CORS handling - Configurable Cross-Origin Resource Sharing policies
- Request logging - Detailed request/response logging with customizable formats
- Gzip compression - Automatic response compression for bandwidth savings
- Rate limiting - Sliding window rate limiting to prevent abuse
- CSRF protection - Cross-Site Request Forgery prevention
- Security headers - Automatic addition of security headers
- Timeout handling - Request timeout protection
- Request ID tracking - Unique request IDs for tracing
- Easy composition - Seamless integration with the Tower service stack
Usage
Basic Setup
Use the ServiceBuilder to compose multiple middleware layers:
use *;
use ;
async
CORS Middleware
Configure Cross-Origin Resource Sharing policies:
use ;
use Duration;
let cors = new
.allow_origin
.allow_methods
.allow_headers
.max_age; // Cache preflight for 1 day
let app = new
.layer
.service;
Logging Middleware
Add detailed request/response logging:
use LoggerLayer;
let app = new
.layer
.service;
// Or customize the log format
let custom_logger = custom;
Compression Middleware
Enable automatic response compression:
use CompressionLayer;
let app = new
.layer
.service;
// The middleware automatically compresses responses based on Accept-Encoding header
Rate Limiting Middleware
Protect your application from abuse with rate limiting:
use RateLimitLayer;
use Duration;
// Allow 100 requests per minute per IP address
let rate_limiter = new;
let app = new
.layer
.service;
// For more granular control
let custom_limiter = builder
.limit // Max requests
.duration // Time window
.build;
Security Headers
Add important security headers to responses:
use SecurityHeadersLayer;
let security = new
.hsts
.x_frame_options
.x_content_type_options
.x_xss_protection;
let app = new
.layer
.service;
CSRF Protection
Protect against Cross-Site Request Forgery attacks:
use CsrfLayer;
let csrf = new
.cookie_name
.header_name;
let app = new
.layer
.service;
Timeout Middleware
Prevent hanging requests with timeouts:
use TimeoutLayer;
use Duration;
let timeout = new;
let app = new
.layer
.service;
Request ID Middleware
Track requests across your application with unique IDs:
use RequestIdLayer;
let request_id = new;
let app = new
.layer
.service;
// Request IDs can be accessed in handlers via request extensions
Complete Example
Combine multiple middleware for a production-ready application:
use *;
use ;
use Duration;
async
Custom Middleware
Create your own middleware by implementing the Tower Layer and Service traits:
use ;
use ;
use ;
;
Integration with Oxidite
All middleware is designed to work seamlessly with Oxidite's architecture:
use *;
use ;
async
async
License
MIT