HyperChad Router
Async routing system for HyperChad applications with request handling and navigation.
Overview
The HyperChad Router package provides:
- Async Routing: Full async request routing and handling
- Route Matching: Flexible route path matching with literals and prefixes
- Request Processing: Comprehensive request parsing and handling
- Form Support: Multipart form and JSON body parsing
- Client Detection: OS and client information detection
- Navigation System: Programmatic navigation and content delivery
Features
Route Matching
- Literal Routes: Exact path matching
- Multiple Literals: Match against multiple possible paths
- Prefix Matching: Match paths with specific prefixes
- Flexible Patterns: Support for various route patterns
Request Handling
- HTTP Methods: Support for GET, POST, PUT, DELETE, PATCH
- Query Parameters: Automatic query string parsing
- Headers: Complete header access and manipulation
- Cookies: Cookie parsing and management
- Body Parsing: JSON and form data parsing
Form Processing
- Multipart Forms: Complete multipart form support
- File Uploads: File upload handling with base64 encoding
- JSON Bodies: JSON request body parsing
- URL Encoded: URL-encoded form data support
- Content-Type Detection: Automatic content type handling
Client Information
- OS Detection: Automatic operating system detection
- Client Info: Structured client information
- Request Context: Rich request context and metadata
Installation
Add this to your Cargo.toml:
[]
= { = "../hyperchad/router" }
# Enable additional features
= {
path = "../hyperchad/router",
= ["serde", "form", "static-routes"]
}
Usage
Basic Router Setup
use ;
use Content;
// Create router
let router = new
.with_route
.with_route;
// Navigate to route
let content = router.navigate.await?;
Route Patterns
use RoutePath;
// Literal route
let home_route = Literal;
// Multiple literals
let api_routes = Literals;
// Prefix matching
let static_route = LiteralPrefix;
// From string slice arrays
let routes: RoutePath = &.into;
Request Information
use ;
// Create request with client info
let client_info = ClientInfo ;
let request = from;
// Access request properties
println!;
println!;
println!;
Form Handling
use Deserialize;
let router = new
.with_route_result;
JSON Body Parsing
use ;
let router = new
.with_route_result;
Static Routes
// Static routes (compiled at build time)
let router = new
.with_static_route
.with_static_route;
Navigation and Content Delivery
// Spawn navigation in background
let handle = router.navigate_spawn;
// Wait for navigation result
match handle.await
// Send navigation result to receiver
router.navigate_send.await?;
// Wait for content on receiver
if let Some = router.wait_for_navigation.await
Error Handling
use ;
match router.navigate.await
Route Types
RoutePath Variants
- Literal(String): Exact string match
- Literals(Vec): Match any of multiple strings
- LiteralPrefix(String): Match strings with specific prefix
Content Types
- Html: HTML content
- Json: JSON responses
- Css: CSS stylesheets
- JavaScript: JavaScript files
- Custom: Custom content types
Client Information
ClientInfo Structure
Default Client Detection
Automatic OS detection using the os_info crate provides default client information.
Feature Flags
serde: Enable JSON and form parsingform: Enable multipart form supportstatic-routes: Enable static route compilation
Dependencies
- Tokio: Async runtime and task management
- Futures: Future utilities
- Bytes: Efficient byte handling
- Flume: Channel communication
- QString: Query string parsing
- Serde: Optional serialization support
- Mime Multipart: Optional form parsing
Integration
This package is designed for:
- Web Applications: Full-featured web application routing
- API Servers: RESTful API endpoint handling
- Form Processing: Web form and file upload handling
- SPA Routing: Single-page application routing
- Content Management: Dynamic content delivery systems