routerify_ng 0.1.0

A lightweight, idiomatic, composable and modular router implementation with middleware support for the Rust HTTP library hyper.rs 1.7.
Documentation

Routerify-NG

CI crates.io Documentation License

Routerify-NG (Next Generation) is a modern, lightweight, idiomatic, and modular router for the Rust HTTP library Hyper 1.x.
It’s a maintained and upgraded fork of the original Routerify rewritten for the new Hyper service model.


✨ Highlights

  • πŸŒ€ Build complex routing with scopes and middlewares
  • βš™οΈ Fully compatible with Hyper 1.x and Tokio 1.x
  • πŸš€ Fast route matching via RegexSet
  • 🧩 Middleware system with shared state between routes
  • πŸ’¬ Robust error handling
  • πŸ”„ WebSocket support (compatible with Hyper 1.x)
  • πŸ“š Extensive documentation and examples

⚑ Benchmarks

Framework Language Requests/sec
Hyper 1.7 Rust 2024 160 000 +
Routerify-NG (Hyper 1.7) Rust 2024 158 000 +
Actix-Web 4 Rust 2024 150 000 +
Warp 0.3 Rust 2024 145 000 +

(benchmarks vary per system; see benchmarks folder)


Install

Add this to your Cargo.toml:

[dependencies]
routerify_ng = "0.1.0"
hyper = "1.7"
tokio = { version = "1", features = ["full"] }

Example

use hyper::{Body, Request, Response, Server, StatusCode};
use routerify_ng::prelude::*;
use routerify_ng::{Middleware, Router, RouterService, RequestInfo};
use std::{convert::Infallible, net::SocketAddr};

struct State(u64);

async fn home_handler(req: Request<Body>) -> Result<Response<Body>, Infallible> {
    let state = req.data::<State>().unwrap();
    println!("State value: {}", state.0);
    Ok(Response::new(Body::from("Home page")))
}

async fn user_handler(req: Request<Body>) -> Result<Response<Body>, Infallible> {
    let user_id = req.param("userId").unwrap();
    Ok(Response::new(Body::from(format!("Hello {}", user_id))))
}

async fn logger(req: Request<Body>) -> Result<Request<Body>, Infallible> {
    println!("{} {} {}", req.remote_addr(), req.method(), req.uri().path());
    Ok(req)
}

async fn error_handler(err: routerify_ng::RouteError, _: RequestInfo) -> Response<Body> {
    eprintln!("{}", err);
    Response::builder()
        .status(StatusCode::INTERNAL_SERVER_ERROR)
        .body(Body::from(format!("Something went wrong: {}", err)))
        .unwrap()
}

fn router() -> Router<Body, Infallible> {
    Router::builder()
        .data(State(100))
        .middleware(Middleware::pre(logger))
        .get("/", home_handler)
        .get("/users/:userId", user_handler)
        .err_handler_with_info(error_handler)
        .build()
        .unwrap()
}

#[tokio::main]
async fn main() {
    let service = RouterService::new(router()).unwrap();
    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));

    println!("App running on: {}", addr);

    if let Err(err) = Server::bind(&addr).serve(service).await {
        eprintln!("Server error: {}", err);
    }
}

Documentation

Docs for an exhaustive documentation.

Examples

Find runnable examples in the examples directory.

Contributing

PRs, ideas, and suggestions are always welcome!

If you’d like to help maintain Routerify-NG or extend its ecosystem (WebSockets, tower integration, macros, etc.), open an issue or pull request.

License

Licensed under the MIT License.

Routerify-NG β€” keeping Hyper simple, fast, and modern for the next generation of Rust web developers.