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
- Request/Response abstraction
- Async/await support
- Extensible architecture
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 DateWrapper;
use ;
use ;
// Handler function
async
// Default 404 handler
async
async
Advanced Example
Here's a more complete example showing different request handlers and data extraction:
use ;
use header;
use EncodeWrapper;
use ;
use ;
use Deserialize;
// Data structure for form/JSON extraction
// Form data handler
async
// JSON data handler
async
async
Core Components
Router
The router provides flexible request routing with support for:
- Path parameters
- Query parameters
- HTTP method matching
- Wildcard routes
- Nested routers
- Request filters
Middleware
Middleware provides a way to process requests/responses before they reach your handlers. Built-in middleware includes:
DateWrapper: Adds date headers to responsesEncodeWrapper: Handles response encoding- you can custom others through
Wrapper
Example of adding middleware:
router.builder
.wrap
.wrap
.build;
Request Handlers
Handlers can be created from async functions using handler_fn:
async
router.route;
Data Extraction
The framework supports extracting data from requests in different formats:
// Form data extraction
async
// JSON extraction
async
Architecture
The framework is built with a modular architecture:
router: Request routing and handler dispatchwrapper: Middleware processing pipelinehandler: Request handler traits and implementationsresponse: Response building and formatting
Performance
Built on micro-http, the framework maintains high performance through:
- Zero-allocation routing where possible
- Efficient middleware chaining
- Minimal copying of request/response data
- Async I/O throughout
- Smart memory management
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