reinhardt-server
HTTP server implementation for Reinhardt framework
Overview
`reinhardt-server` provides a high-performance HTTP server implementation for Reinhardt applications, built on Hyper with support for WebSockets and GraphQL.
Features
Implemented ✓
This crate provides the following features:
-
Core HTTP Server: High-performance HTTP/1.1 server
- Async request processing with Tokio runtime
- Custom handler support via Handler trait
- Middleware pipeline for request/response processing
- Builder pattern for middleware configuration
- Efficient TCP connection management
- Automatic request/response conversion
- Built-in error handling
-
WebSocket Support (feature = "websocket"): WebSocket server implementation
- tokio-tungstenite based WebSocket server
- Custom message handler support
- Broadcast support for multiple clients
- Client connection management with automatic registration/unregistration
- Connection lifecycle hooks (on_connect, on_disconnect)
- Text and binary message handling
- Automatic connection management
-
GraphQL Support (feature = "graphql"): GraphQL endpoint integration
- async-graphql integration
- Schema builder for Query and Mutation roots
- POST request handling for GraphQL queries
- JSON response serialization
- Error handling for GraphQL errors
Installation
Add reinhardt to your Cargo.toml:
[]
= { = "0.1.0-rc.25", = ["server"] }
# For WebSocket support:
# reinhardt = { version = "0.1.0-rc.25", features = ["server", "websocket"] }
# For GraphQL support:
# reinhardt = { version = "0.1.0-rc.25", features = ["server", "graphql"] }
# Or use a preset:
# reinhardt = { version = "0.1.0-rc.25", features = ["standard"] } # Recommended
# reinhardt = { version = "0.1.0-rc.25", features = ["full"] } # All features
Then import server features:
use ;
use ; // WebSocket
use graphql_handler; // GraphQL
Note: Server features are included in the standard and full feature presets.
Usage
Basic HTTP Server
use ;
use ;
use Error;
use Arc;
async
async
HTTP Server with Middleware
use HttpServer;
use ;
use Error;
use Arc;
;
;
async
WebSocket Server
use ;
use Arc;
;
async
WebSocket Server with Broadcast
use ;
use Arc;
;
async
GraphQL Server
use graphql_handler;
use ;
;
async
server
Features
Implemented ✓
Core HTTP Server
- HTTP/1.1 Server: High-performance HTTP/1.1 server implementation based on Hyper
- Async Request Processing: Full asynchronous processing with Tokio runtime
- Custom Handler Support: Add custom logic by implementing the
Handlertrait - TCP Connection Management: Efficient TCP connection management and task spawning
- Request/Response Conversion: Automatic conversion between Hyper requests and reinhardt-http Request/Response
- Error Handling: Automatically converts handler errors to 500 error responses
WebSocket Support (feature = "websocket")
- WebSocket Server: WebSocket server implementation based on tokio-tungstenite
- Custom Message Handlers: Customize message processing via the
WebSocketHandlertrait - Connection Lifecycle Hooks: Handle connection events with
on_connectandon_disconnect - Text/Binary Messages: Process text messages and echo binary messages
- Automatic Connection Management: Automatic handling of WebSocket connection establishment, message loops, and closure
- Peer Information: Access to client SocketAddr information
GraphQL Support (feature = "graphql")
- GraphQL Handler: GraphQL endpoint support with async-graphql integration
- Schema Builder: Automatic schema construction from Query and Mutation roots
- POST Request Processing: Execute GraphQL queries via POST requests
- JSON Responses: Automatic JSON serialization of GraphQL execution results
- Error Handling: Proper handling and response of GraphQL errors
- Empty Subscriptions: Uses
EmptySubscriptionby default
Convenience Functions
serve()function: Helper function providing easy HTTP server startupserve_websocket()function: Helper function providing easy WebSocket server startupgraphql_handler()function: Simplifies Arc wrapping of GraphQL handlers
Graceful Shutdown
- ShutdownCoordinator: Graceful shutdown coordination mechanism
- Signal handling (SIGTERM, SIGINT)
- Wait for existing connections to complete
- Shutdown with timeout processing
- Shutdown notification via broadcast channel
- shutdown_signal(): Listen for OS shutdown signals
- listen_with_shutdown(): Start server with graceful shutdown support
- serve_with_shutdown(): Convenience function with graceful shutdown support
- with_shutdown(): Add shutdown handling to Futures
HTTP/2 Support
- Http2Server: HTTP/2 protocol server implementation
- Uses hyper-util's HTTP/2 builder
- Full asynchronous request processing
- Graceful shutdown support
- Uses same Handler trait as HTTP/1.1
- serve_http2(): Easy HTTP/2 server startup
- serve_http2_with_shutdown(): HTTP/2 server startup with graceful shutdown support
Request Timeouts
- TimeoutHandler: Request timeout middleware
- Configurable timeout duration
- Returns 408 Request Timeout response on timeout
- Can wrap any Handler
- Fully tested
Rate Limiting
- RateLimitHandler: Rate limiting middleware
- IP address-based rate limiting
- Supports Fixed Window and Sliding Window strategies
- Configurable window period and maximum request count
- Returns 429 Too Many Requests response when rate limit exceeded
- RateLimitConfig: Rate limit configuration
per_minute(): Per-minute rate limitingper_hour(): Per-hour rate limiting- Custom configurable
Advanced HTTP Features
- Middleware Pipeline: Middleware chain for request/response processing
- Connection Pooling: Efficient HTTP connection pooling mechanism
- Request Logging: Structured request logging
WebSocket Advanced Features
- Broadcast Support: Message broadcasting to multiple clients
- Room-Based Management: Manage clients by rooms
- Message Compression: WebSocket message compression support
- Heartbeat/Ping-Pong: Connection alive check mechanism
- Authentication/Authorization: Authentication and authorization for WebSocket connections
GraphQL Advanced Features
- Subscription Support: Real-time GraphQL subscriptions
- DataLoader Integration: DataLoader for solving N+1 problems
- GraphQL Playground: GraphQL IDE integration
- File Uploads: File uploads via GraphQL
- Batch Queries: Batch execution of multiple queries
Testing & Monitoring
- Metrics: Server metrics collection and publishing
- Health Checks: Server health check endpoints
- Tracing: Distributed tracing support
License
Licensed under the BSD 3-Clause License.