All Projects → routerify → Routerify

routerify / Routerify

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

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to Routerify

Rayo.js
Micro framework for Node.js
Stars: ✭ 170 (-1.73%)
Mutual labels:  middleware, router, routing
Clevergo
👅 CleverGo is a lightweight, feature rich and high performance HTTP router for Go.
Stars: ✭ 246 (+42.2%)
Mutual labels:  middleware, router, routing
Simple Php Router
Simple, fast and yet powerful PHP router that is easy to get integrated and in any project. Heavily inspired by the way Laravel handles routing, with both simplicity and expand-ability in mind.
Stars: ✭ 279 (+61.27%)
Mutual labels:  middleware, router, routing
Django Macros Url
Django Macros URL. Routing must be simple as possible
Stars: ✭ 121 (-30.06%)
Mutual labels:  router, routing
Webgo
A minimal framework to build web apps; with handler chaining, middleware support; and most of all standard library compliant HTTP handlers(i.e. http.HandlerFunc).
Stars: ✭ 165 (-4.62%)
Mutual labels:  middleware, router
Universal Router
A simple middleware-style router for isomorphic JavaScript web apps
Stars: ✭ 1,598 (+823.7%)
Mutual labels:  router, routing
Dotweb
Simple and easy go web micro framework
Stars: ✭ 1,354 (+682.66%)
Mutual labels:  middleware, router
Redux First Routing
A minimal, framework-agnostic API for accomplishing Redux-first routing.
Stars: ✭ 133 (-23.12%)
Mutual labels:  middleware, routing
Http Router
🎉 Release 2.0 is released! Very fast HTTP router for PHP 7.1+ (incl. PHP8 with attributes) based on PSR-7 and PSR-15 with support for annotations and OpenApi (Swagger)
Stars: ✭ 124 (-28.32%)
Mutual labels:  middleware, router
Router5
Flexible and powerful universal routing solution
Stars: ✭ 1,704 (+884.97%)
Mutual labels:  router, routing
Pure Http
✨ The simple web framework for Node.js with zero dependencies.
Stars: ✭ 139 (-19.65%)
Mutual labels:  middleware, router
React Page Layout
Create layouts for react
Stars: ✭ 117 (-32.37%)
Mutual labels:  router, routing
Php Router
simple and flexible Router class for PHP. with Controllers and Middlewares support.
Stars: ✭ 111 (-35.84%)
Mutual labels:  router, routing
Gin
Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
Stars: ✭ 53,971 (+31097.11%)
Mutual labels:  middleware, router
Foxify
The fast, easy to use & typescript ready web framework for Node.js
Stars: ✭ 138 (-20.23%)
Mutual labels:  middleware, router
Nextjs Dynamic Routes
[Deprecated] Super simple way to create dynamic routes with Next.js
Stars: ✭ 145 (-16.18%)
Mutual labels:  router, routing
Bsdrp
BSD Router Project
Stars: ✭ 126 (-27.17%)
Mutual labels:  router, routing
Router
⚡️ A lightning fast HTTP router
Stars: ✭ 158 (-8.67%)
Mutual labels:  router, routing
Phprouter
PhpRouter is a powerful, minimal, and very fast HTTP URL router for PHP projects
Stars: ✭ 97 (-43.93%)
Mutual labels:  router, routing
Twig
Twig - less is more's web server for golang
Stars: ✭ 98 (-43.35%)
Mutual labels:  middleware, router

Github Actions Status crates.io Documentation Contributors MIT

Routerify

Routerify provides a lightweight, idiomatic, composable and modular router implementation with middleware support for the Rust HTTP library hyper.

Routerify's core features:

To generate a quick server app using Routerify and hyper, please check out hyper-routerify-server-template.

Compiler support: requires rustc 1.48+

Benchmarks

Framework Language Requests/sec
hyper v0.14 Rust 1.50.0 144,583
routerify v2.0.0 with hyper v0.14 Rust 1.50.0 144,621
actix-web v3 Rust 1.50.0 131,292
warp v0.3 Rust 1.50.0 145,362
go-httprouter, branch master Go 1.16 130,662
Rocket, branch master Rust 1.50.0 130,045

For more info, please visit Benchmarks.

Install

Add this to your Cargo.toml file:

[dependencies]
routerify = "2.0.0"
hyper = "0.14"
tokio = { version = "1", features = ["full"] }

Basic Example

A simple example using Routerify with hyper would look like the following:

use hyper::{Body, Request, Response, Server, StatusCode};
// Import the routerify prelude traits.
use routerify::prelude::*;
use routerify::{Middleware, Router, RouterService, RequestInfo};
use std::{convert::Infallible, net::SocketAddr};

// 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<Body>) -> Result<Response<Body>, Infallible> {
    // Access the app state.
    let state = req.data::<State>().unwrap();
    println!("State value: {}", state.0);

    Ok(Response::new(Body::from("Home page")))
}

// A handler for "/users/:userId" 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))))
}

// A middleware which logs an http request.
async fn logger(req: Request<Body>) -> Result<Request<Body>, 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::Error, _: RequestInfo) -> Response<Body> {
    eprintln!("{}", err);
    Response::builder()
        .status(StatusCode::INTERNAL_SERVER_ERROR)
        .body(Body::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<Body, 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()
        // Specify the state data which will be available to every route handlers,
        // error handler and middlewares.
        .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 router = router();

    // Create a Service from the router above to handle incoming requests.
    let service = RouterService::new(router).unwrap();

    // The address on which the server will be listening.
    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));

    // Create a server by passing the created service to `.serve` method.
    let server = Server::bind(&addr).serve(service);

    println!("App is running on: {}", addr);
    if let Err(err) = server.await {
        eprintln!("Server error: {}", err);
   }
}

Documentation

Please visit: Docs for an exhaustive documentation.

Examples

The examples.

Contributing

Your PRs and suggestions are always welcome.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].