Overview

Warp is a super-easy, composable, web server framework for warp speeds.

Built on top of hyper, warp is an asynchronous web framework for Rust that aims to be fast, safe, and easy to use. Its fundamental building block is the Filter system, which allows you to combine and compose small, reusable pieces of logic to handle requests.

Key Features

Thanks to its Filter system, warp provides a rich set of features out of the box:

  • Path Routing & Parameter Extraction: Define routes with static segments and typed parameters.
  • Header Requirements & Extraction: Match requests based on headers and extract their values.
  • Query String Deserialization: Automatically deserialize query strings into your custom types.
  • JSON and Form Bodies: Handle request bodies with application/json or application/x-www-form-urlencoded content types.
  • Multipart Form Data: Process file uploads and multipart forms.
  • Static Files and Directories: Serve static assets with built-in support for conditional and range requests.
  • WebSockets: Handle WebSocket upgrades and communication.
  • Server-Sent Events (SSE): Stream events to clients easily.
  • Access Logging & Tracing: Integrated logging and diagnostics via the log and tracing crates.
  • Compression: Gzip, Deflate, and Brotli response compression.

Since it's built on hyper, you automatically get:

  • HTTP/1 & HTTP/2 support
  • A highly asynchronous architecture
  • One of the fastest and most correct HTTP implementations available.

The Filter System

The main concept in warp is the Filter, which represents a piece of logic that can be applied to a request. Filters can be chained together with and() to create a sequence of requirements, or combined with or() to provide fallback routes. They can extract data from the request, which is then passed to your handler functions in a type-safe way.

Here is a small example demonstrating the composition of several filters:

use warp::Filter;

#[tokio::main]
async fn main() {
    // Match requests like GET /hello/warp
    let hello = warp::path!("hello" / String)
        // The `String` segment is extracted and passed to the map function
        .map(|name| format!("Hello, {}!", name));

    warp::serve(hello)
        .run(([127, 0, 0, 1], 3030))
        .await;
}

This example creates a route that matches GET /hello/:name, extracts the name parameter as a String, and replies with a personalized greeting. This documentation will guide you through building everything from simple routes like this to complex, full-featured web services.