| Crates.io | routerify_ng |
| lib.rs | routerify_ng |
| version | 0.3.0 |
| created_at | 2025-11-06 20:42:09.099355+00 |
| updated_at | 2025-11-08 14:15:09.740446+00 |
| description | A lightweight, idiomatic, composable and modular router implementation with middleware support for the Rust HTTP library hyper.rs 1.7. |
| homepage | https://github.com/guru901/routerify_ng |
| repository | https://github.com/guru901/routerify_ng |
| max_upload_size | |
| id | 1920401 |
| size | 233,056 |
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.
RegexSetWebSocket support (compatible with Hyper 1.x)| 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)
Add this to your Cargo.toml:
[dependencies]
routerify_ng = "0.1.0"
hyper = "1.7"
tokio = { version = "1", features = ["full"] }
use http_body_util::Full;
use hyper::body::Incoming;
use hyper::service::Service;
use hyper::{Request, Response, StatusCode};
use hyper_util::rt::TokioExecutor;
use hyper_util::rt::TokioIo;
use hyper_util::server::conn::auto::Builder;
// Import the routerify prelude traits.
use routerify_ng::prelude::*;
use routerify_ng::{Middleware, RequestInfo, Router, RouterService};
use std::sync::Arc;
use std::{convert::Infallible, net::SocketAddr};
use tokio::net::TcpListener;
// Define an app state to share it across the route handlers and middlewares.
struct State(u64);
// A handler for "/" page.
async fn home_handler(req: Request<Incoming>) -> Result<Response<Full<hyper::body::Bytes>>, Infallible> {
Ok(Response::new(Full::new(hyper::body::Bytes::from("Home page"))))
}
// A handler for "/users/:userId" page.
async fn user_handler(req: Request<Incoming>) -> Result<Response<Full<hyper::body::Bytes>>, Infallible> {
let user_id = req.param("userId").unwrap();
Ok(Response::new(Full::new(hyper::body::Bytes::from(format!(
"Hello {}",
user_id
)))))
}
// A middleware which logs an http request.
async fn logger(req: Request<Incoming>) -> Result<Request<Incoming>, Infallible> {
println!("{} {} {}", req.remote_addr(), req.method(), req.uri().path());
Ok(req)
}
// Define an error handler function which will accept the `routerify::Error`
// and the request information and generates an appropriate response.
async fn error_handler(err: routerify_ng::RouteError, _: RequestInfo) -> Response<Full<hyper::body::Bytes>> {
eprintln!("{}", err);
Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Full::new(hyper::body::Bytes::from(format!(
"Something went wrong: {}",
err
))))
.unwrap()
}
// Create a `Router<Body, Infallible>` for response body type `hyper::Body`
// and for handler error type `Infallible`.
fn router() -> Router<Incoming, Infallible> {
// Create a router and specify the logger middleware and the handlers.
// Here, "Middleware::pre" means we're adding a pre middleware which will be executed
// before any route handlers.
Router::builder()
.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 router = router();
// Create a Service from the router above to handle incoming requests.
let router_service = Arc::new(RouterService::new(router).unwrap());
// The address on which the server will be listening.
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
let listener = TcpListener::bind(addr).await.unwrap();
println!("App is running on: {}", addr);
loop {
match listener.accept().await {
Ok((stream, _)) => {
let router_service = Arc::clone(&router_service);
tokio::task::spawn(async move {
// Get the request service for this connection
let request_service = router_service.call(&stream).await.unwrap();
// Wrap the stream in TokioIo for hyper
let io = TokioIo::new(stream);
let builder = Builder::new(TokioExecutor::new());
// Serve the connection
if let Err(err) = builder.serve_connection(io, request_service).await {
eprintln!("Error serving connection: {:?}", err);
}
});
}
Err(e) => {
eprintln!("Error accepting connection: {}", e);
}
}
}
}
Docs for an exhaustive documentation.
Find runnable examples in the examples directory.
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.
Licensed under the MIT License.
Routerify-NG β keeping Hyper simple, fast, and modern for the next generation of Rust web developers.