Crate ruffus

Crate ruffus 

Source
Expand description

Ruffus - Fast, minimalist web framework for Rust

Ruffus is a web framework inspired by Express.js that provides a simple, ergonomic API for building web applications in Rust.

§Features

  • Express-like API: Familiar, intuitive routing and middleware system
  • Type-safe extractors: Extract path parameters, query strings, and JSON bodies with compile-time safety
  • Async/await support: Built on Tokio and Hyper for high-performance async I/O
  • Flexible middleware: Composable middleware for cross-cutting concerns
  • Router mounting: Organize routes with prefixes and nested routers

§Quick Start

use ruffus::{App, Request, Response};

#[tokio::main]
async fn main() {
    let mut app = App::new();
     
    app.get("/", |_req: Request| async {
        Ok(Response::text("Hello, World!".to_string()))
    });
     
    app.listen("127.0.0.1:3000").await.unwrap();
}

§Path Parameters

Extract dynamic segments from URLs:

app.get("/users/:id", |req: Request| async move {
    let id = req.param("id").unwrap();
    Ok(Response::text(format!("User ID: {}", id)))
});

§JSON Handling

Automatically serialize and deserialize JSON:

#[derive(Deserialize)]
struct CreateUser {
    name: String,
    email: String,
}

#[derive(Serialize)]
struct User {
    id: u64,
    name: String,
    email: String,
}

app.post("/users", |mut req: Request| async move {
    let body: CreateUser = req.json().await?;
    let user = User {
        id: 1,
        name: body.name,
        email: body.email,
    };
    Response::json(&user)
});

§Middleware

Add cross-cutting functionality with middleware:

struct Logger;

#[async_trait]
impl Middleware for Logger {
    async fn handle(&self, req: Request, next: Next) -> ruffus::Result<Response> {
        println!("{} {}", req.method(), req.uri());
        next.run(req).await
    }
}

app.use_middleware(Arc::new(Logger));

§Routers

Organize routes with common prefixes:

let mut api = Router::new("/api");

api.get("/users", |_req: Request| async {
    Ok(Response::json(&serde_json::json!({"users": []}))?)
});

api.post("/users", |mut req: Request| async move {
    // Handle user creation
    Ok(Response::json(&serde_json::json!({"status": "created"}))?)
});

let mut app = App::new();
app.mount("/", api);
// Routes are now available at /api/users

Re-exports§

pub use app::App;
pub use error::Error;
pub use extractors::FromRequest;
pub use extractors::Json;
pub use extractors::Path;
pub use extractors::Query;
pub use method::Method;
pub use middleware::Handler;
pub use middleware::Middleware;
pub use middleware::Next;
pub use request::Request;
pub use response::Response;
pub use router::PathPattern;
pub use router::Route;
pub use router::Router;
pub use router::Segment;

Modules§

app
Application type - the main entry point for Ruffus
error
Error types for Ruffus
extractors
Request extractors for type-safe data extraction
method
HTTP Method type
middleware
Middleware trait and types
request
HTTP Request type
response
HTTP Response type
router
Router for organizing routes with common prefixes

Type Aliases§

Result