Crate micro_web

Source
Expand description

High-level web framework built on top of micro-http.

This crate provides an ergonomic web framework that simplifies building HTTP services by offering high-level abstractions while leveraging the efficient HTTP implementation from the micro-http crate.

§Core Features

  • Ergonomic Request Handling

    • Async function handlers with automatic type conversion
    • Flexible routing with path parameters
    • Built-in support for common data formats (JSON, form data, etc.)
  • Middleware System

    • Composable request/response transformations
    • Built-in middleware for common tasks (compression, date headers, etc.)
    • Easy to implement custom middleware
  • Type-Safe Extractors

    • Automatic request data extraction into Rust types
    • Support for headers, query parameters, and request bodies
    • Custom extractor implementation possible
  • Flexible Response Types

    • Automatic conversion of Rust types to HTTP responses
    • Streaming response support
    • Content negotiation and compression

§Architecture

The framework is organized into several key modules:

  • Core Types ([handler], [request], responder)

    • Request handling traits and implementations
    • Context types for accessing request data
    • Response generation utilities
  • Routing (router)

    • URL pattern matching
    • HTTP method filtering
    • Route parameter extraction
  • Data Extraction (extract)

    • Query string parsing
    • Form data handling
    • JSON serialization/deserialization
  • Request Filtering ([filter])

    • Header-based filtering
    • Method matching
    • Custom filter implementation
  • Middleware ([wrapper])

    • Response transformation
    • Cross-cutting concerns
    • Built-in middleware components

§Example

use micro_web::{
    router::{get, Router},
    handler_fn,
    Server,
};

// Define a simple handler
async fn hello_world() -> &'static str {
    "Hello, World!"
}

#[tokio::main]
async fn main() {
    // Create a router
    let router = Router::builder()
        .route("/", get(handler_fn(hello_world)))
        .build();

    // Start the server
    Server::builder()
        .router(router)
        .bind("127.0.0.1:3000")
        .build()
        .unwrap()
        .start()
        .await;
}

§Relationship with micro-http

This framework builds upon the low-level HTTP implementation provided by micro-http:

  • micro-http handles the raw TCP connections and HTTP protocol details
  • This crate provides the high-level abstractions for building web services
  • The integration is seamless while maintaining performance

See the micro-http documentation for more details about the underlying implementation.

Modules§

date
HTTP date header value management service.
encoding
Module for handling HTTP response body encoding.
extract
Request data extraction module
responder
Response handling module that converts handler results into HTTP responses.
router

Structs§

FnHandler
A wrapper type that converts async functions into request handlers.
OptionReqBody
PathParams
Represents path parameters extracted from the URL path of an HTTP request.
RequestContext
Represents the context of an HTTP request, providing access to both the request headers and any path parameters extracted from the URL.
ResponseBody
Server
Core server implementation that processes HTTP requests.

Traits§

FnTrait
A trait for abstracting over async functions with varying numbers of parameters.

Functions§

handler_fn
Creates a new FnHandler that wraps the given async function.