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, req::HttpRequest};
#[tokio::main]
async fn main() {
let mut app = App::new();
// Define routes
app.get("/", |_req: HttpRequest, res| async move {
res.ok().text("Hello, World!")
});
app.get("/api/users", |_req: HttpRequest, 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
wyndcrate (optionalwith-wyndfeature) - 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 datalogger: Request/response logging middlewarewith-wynd: WebSocket support via thewyndcrate
§Advanced Examples
§RESTful API with JSON
use ripress::{app::App, types::RouterFns, req::HttpRequest};
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: HttpRequest, 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: HttpRequest, 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: HttpRequest, 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, req::HttpRequest};
#[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: HttpRequest, 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;
}§Validation and Extraction based implementation
Ripress provides a powerful and flexible way to handle request data extraction and validation. It supports various types of data extraction, including route parameters, query parameters, JSON bodies, and request meta data. The framework also provides a set of macros and traits for easily implementing custom extraction and validation logic.
§Example
use ripress::{
app::App,
req::{
body::json_data::{JsonBody, JsonBodyValidated},
query_params::QueryParam,
request_headers::Headers,
route_params::Params,
},
types::RouterFns,
};
use ripress_derive::{FromJson, FromParams, FromQueryParam};
use serde::{Deserialize, Serialize};
use validator::Validate;
#[derive(FromJson, Deserialize, Debug, Serialize)]
struct User {
username: String,
}
#[derive(FromJson, Deserialize, Validate, Debug)]
struct Signup {
#[validate(length(min = 3))]
username: String,
#[validate(email)]
email: String,
}
#[derive(Deserialize, Debug, FromQueryParam)]
struct PageQuery {
page: u32,
}
#[derive(Deserialize, Debug, FromParams)]
struct PathParams {
id: String,
}
#[derive(FromParams)]
struct OrgParams {
org_id: String,
user_id: String,
}
#[derive(FromQueryParam)]
struct OrgQueryParams {
query: String,
}
#[tokio::main]
async fn main() {
let mut app = App::new();
// Classic JsonBody extractor (not validated)
app.post("/json", |body: JsonBody<User>, res| async move {
let username = &body.username;
println!("Classic JsonBody: {}", username);
res.ok().json(body)
});
// ValidatedJson extractor - performs validation on deserialization
app.post(
"/signup",
|body: JsonBodyValidated<Signup>, res| async move {
println!("Email: {}, Username: {}", body.email, body.username);
res.ok().json(serde_json::json!({
"msg": "Signup received",
"user": &body.username,
"email": &body.email
}))
},
);
// Query string extractor
app.get(
"/articles",
|query: QueryParam<PageQuery>, res| async move {
let page = query.page;
res.ok().json(serde_json::json!({ "page": page }))
},
);
// Path param extractor
app.get("/user/:id", |params: Params<PathParams>, res| async move {
println!("Path params: id: {:?}", params.id);
res.ok().json(serde_json::json!({ "id": params.id }))
});
// Mixing extractors: Path param, query param, standard request and response
app.get(
"/org/:org_id/user/:user_id",
|(path, query, _headers): (Params<OrgParams>, QueryParam<OrgQueryParams>, Headers), res| async move {
res.ok().json(serde_json::json!({
"org_id": path.org_id,
"user_id": path.user_id,
"query": query.query,
}))
},
);
app.listen(3000, || {
println!("Ripress extractor demo listening 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.
- macros
- Procedural and attribute macros for the Ripress framework.
- 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.
Macros§
- middlewares
- A macro for convenient construction of middleware vectors (
Middlewares).