Crate ripress

Crate ripress 

Source
Expand description

§Ripress

Ripress is a lightweight, modular web framework for building HTTP APIs and web applications in Rust. It provides a simple and flexible API for defining routes, handling requests and responses, and composing middleware. Inspired by Express.js, Ripress brings the familiar developer experience to Rust while maintaining high performance.

§Quick Start

use ripress::{app::App, types::RouterFns};

#[tokio::main]
async fn main() {
    let mut app = App::new();

    // Define routes
    app.get("/", |_req, res| async move {
        res.ok().text("Hello, World!")
    });

    app.get("/api/users", |_req, res| async move {
        res.ok().json(serde_json::json!({
            "users": ["Alice", "Bob", "Charlie"]
        }))
    });

    // Add middleware
    app.use_cors(None);

    // Start server
    app.listen(3000, || {
        println!("Server running on http://localhost:3000");
    }).await;
}

§Key Features

  • Express.js-like API: Familiar routing and middleware patterns
  • Async/Await Support: Built on Tokio for high-performance async operations
  • Type Safety: Full Rust type safety with compile-time error checking
  • Built-in Middleware: CORS, logging, compression, rate limiting, and more
  • Request/Response Objects: Rich APIs for handling HTTP data
  • WebSocket Support: Real-time communication via the wynd crate (optional with-wynd feature)
  • Static File Serving: Built-in support for serving static assets

§Optional Features

Several features are optional and can be enabled to reduce compile time and binary size:

  • compression: Response compression middleware (gzip/deflate)
  • file-upload: File upload middleware for multipart form data
  • logger: Request/response logging middleware
  • with-wynd: WebSocket support via the wynd crate

§Advanced Examples

§RESTful API with JSON

use ripress::{app::App, types::RouterFns};
use serde::{Deserialize, Serialize};

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

#[tokio::main]
async fn main() {
    let mut app = App::new();

    // GET /users - List all users
    app.get("/users", |_req, res| async move {
        let users = vec![
            User { id: 1, name: "Alice".to_string(), email: "alice@example.com".to_string() },
            User { id: 2, name: "Bob".to_string(), email: "bob@example.com".to_string() },
        ];
        res.ok().json(users)
    });

    // POST /users - Create a new user
    app.post("/users", |req, res| async move {
        match req.json::<User>() {
            Ok(user) => res.created().json(user),
            Err(_) => res.bad_request().text("Invalid JSON"),
        }
    });

    // GET /users/:id - Get user by ID
    app.get("/users/:id", |req, res| async move {
        let user_id = req.params.get("id").unwrap_or("0");
        res.ok().json(serde_json::json!({
            "id": user_id,
            "message": "User found"
        }))
    });

    app.listen(3000, || {
        println!("API server running on http://localhost:3000");
    }).await;
}

§File Upload with Middleware

use ripress::{app::App, middlewares::file_upload::file_upload, types::RouterFns};

#[tokio::main]
async fn main() {
    let mut app = App::new();

    // Add file upload middleware
    app.use_pre_middleware("/upload", file_upload(None));

    app.post("/upload", |req, res| async move {
        // Access uploaded files through request data
        if let Some(file_data) = req.get_data("uploaded_file") {
            res.ok().text(format!("File uploaded: {}", file_data))
        } else {
            res.bad_request().text("No file uploaded")
        }
    });

    app.listen(3000, || {
        println!("File upload server running on http://localhost:3000");
    }).await;
}

Modules§

app
The main application struct and its methods for configuring and running your server.
context
Common context types for handler functions.
error
Error types and utilities for the Ripress framework.
helpers
Utility functions and helpers for common web tasks.
middlewares
Built-in middleware modules for CORS, logging, file uploads, and rate limiting.
req
The HTTP request struct and its methods for extracting data from requests.
res
The HTTP response struct and its methods for building responses.
router
The router struct and routing logic for organizing endpoints.
types
Core types, traits, and enums used throughout the framework.