#async #http #request #router #guard


Declarative request routing and decoding with async support

3 unstable releases

0.2.0 Jul 31, 2019
0.1.1 Jun 6, 2019
0.1.0 May 31, 2019
0.0.0 Mar 5, 2019

#202 in HTTP server

0BSD license

768 lines


crates.io docs.rs Build Status

This crate provides Rocket-style declarative HTTP request routing and guarding. It can be used in both synchronous and fully async apps (using hyper's support for futures 0.1) and works on stable Rust.

You can declare the endpoints of your web application using attributes like #[post("/user/{id}/posts")], and this crate will generate code that dispatches incoming requests depending on the method and path.

Please refer to the changelog to see what changed in the last releases.

Rust Version Policy

Hyperdrive supports the latest stable Rust release, as well as the 2 stable releases before that. As long as these 3 Rust versions are still supported, bumping the minimum supported Rust version (MSRV) is not considered a breaking change.


This example shows how to use Hyperdrive to define routes for a simple webservice and how to spin up a hyper server that will serve these routes with a user-provided sync handler:

use hyperdrive::{FromRequest, body::Json, service::SyncService};
use hyper::{Server, Body};
use http::{Response, StatusCode};
use futures::prelude::*;
use serde::Deserialize;

enum Route {

    User {
        id: u32,

    Login {
        data: Json<Login>,

struct Login {
    email: String,
    password: String,

fn main() {
    // Prepare a hyper server using Hyperdrive's `SyncService` adapter.
    // If you want to write an async handler, you could use `AsyncService` instead.
    let srv = Server::bind(&"".parse().unwrap())
        .serve(SyncService::new(|route: Route, _| {
            match route {
                Route::Index => {
                    Response::new(Body::from("Hello World!"))
                Route::User { id } => {
                    Response::new(Body::from(format!("User #{}", id)))
                Route::Login { data } => {
                    if data.password == "hunter2" {
                        Response::new(Body::from(format!("Welcome, {}!", data.email)))
                    } else {
                            .body(Body::from("Invalid username or password"))
                            .expect("building response failed")

    let port = srv.local_addr().port();

    std::thread::spawn(move || tokio::run(srv.map_err(|e| {
        panic!("unexpected error: {}", e);

    // Let's make a login request to it
    let response = reqwest::Client::new()
        .post(&format!("{}/login", port))
        .body(r#"{ "email": "oof@example.com", "password": "hunter2" }"#)

    // This login request should succeed
    assert_eq!(response.status(), StatusCode::OK);


~257K SLoC