Micro Web
A lightweight, modular web framework built on top of micro-http, providing an elegant and efficient way to build web applications in Rust.
Features
- Built on micro-http for high performance HTTP handling
- Flexible routing with path parameters and filters
- Type-safe request/response handling with automatic data extraction
- Async/await support throughout
- Extensible architecture with decorators and middleware
- Built-in support for common tasks (compression, date headers, etc.)
Quick Start
Add this to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["rt-multi-thread", "net", "io-util", "macros", "sync", "signal"] }
Basic Example
Here's a simple hello world example:
use ;
use Server;
use DateServiceDecorator;
/// A simple handler that returns "hello world"
async
/// Default handler for unmatched handlers (404 responses)
///
/// This handler is called when no other handlers match the incoming request
async
async
Advanced Example
Here's a more complete example showing different request handlers, data extraction, and middleware:
use Method;
use ;
use header;
use ;
use EncodeDecorator;
use Server;
use Deserialize;
/// User struct for demonstrating data extraction
/// Simple GET handler that demonstrates method and optional string extraction
///
/// Example request:
/// ```bash
/// curl http://127.0.0.1:8080/
/// ```
async
/// Handler that extracts form data into a User struct
///
/// Example request:
/// ```bash
/// curl -v -H "Transfer-Encoding: chunked" \
/// -d "name=hello&zip=world&c=abc" \
/// http://127.0.0.1:8080/
/// ```
async
/// Handler that extracts JSON data into a User struct
///
/// Example request:
/// ```bash
/// curl -v -H "Transfer-Encoding: chunked" \
/// -H 'Content-Type: application/json' \
/// -d '{"name":"hello","zip":"world"}' \
/// http://127.0.0.1:8080/
/// ```
async
/// Simple POST handler demonstrating method and optional string extraction
///
/// Example request:
/// ```bash
/// curl -X POST http://127.0.0.1:8080/
/// ```
async
/// Another GET handler for a different route
///
/// Example request:
/// ```bash
/// curl http://127.0.0.1:8080/4
/// ```
async
/// Default handler for unmatched routes
///
/// Example request:
/// ```bash
/// curl http://127.0.0.1:8080/non-existent-path
/// ```
async
async
Core Components
Router
The router provides flexible request routing with support for:
- Path parameters and pattern matching
- Query parameters extraction
- HTTP method filtering
- Header-based filtering
- Composable filter chains
- Nested routers
Request Handlers
Handlers can be convert from async functions. The framework supports automatic type conversion for both request data extraction and response generation:
// Simple handler returning a string
async
// Handler with path parameter
async
// Handler with JSON request body
async
Data Extraction
The framework provides type-safe extractors for common data formats:
- Path parameters (
Path<T>) - Query parameters (
Query<T>) - Form data (
Form<T>) - JSON (
Json<T>) - Headers (
HeaderMap) - Custom extractors via
FromRequesttrait
Middleware and Decorators
The framework uses a flexible decorator pattern for middleware:
// Add multiple decorators
router.builder
.with_global_decorator // Adds Date header
.with_global_decorator // Handles response compression
.build;
Built-in decorators include:
DateServiceDecorator: Adds date headers to responsesCompressionDecorator: Handles response compression- Custom decorators via
Decoratortrait
Architecture
The framework is built with a modular architecture:
router: Request routing and filter systemhandler: Request handler traits and implementationsextract: Type-safe data extractiondecorator: Middleware and response transformationresponder: Response generation
Performance
Built on micro-http, the framework maintains high performance through:
- Zero-allocation routing where possible
- Efficient middleware chaining via decorators
- Minimal copying of request/response data
- Async I/O throughout
- Smart date handling with cached timestamps
- Optimized header handling
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Development
- Clone the repository
- Run tests:
cargo test - Run examples:
cargo run --example hello_world
Guidelines
- Write tests for new functionality
- Follow Rust best practices
- Document public APIs
- Keep performance in mind
License
This project is licensed under the MIT License or Apache-2.0 License, pick one.
Safety
This crate uses unsafe code in limited, well-documented cases for performance optimization. All unsafe usage is carefully reviewed and tested.
Status
This project is in alpha stage. APIs may change between versions.
Related Projects
- micro-http: The underlying HTTP server implementation