fr-rust Documentation
fr-rust is a comprehensive, high-performance web framework utility library built on top of Actix-Web. It provides out-of-the-box support for DDoS protection, standardized responses, WebSockets, Redis, Database pooling, Cryptography, and common verification services (JWT, OTP, Link Verification, Email).
1. Quick Start & Configuration
Set up your server, initialize shared states, and protect your app with the built-in DDoS shield in your main entry point.
use *;
// Use actix web
use ;
async
// Route Configuration
2. Common Types
fr-rust exports convenient type aliases to reduce boilerplate:
| Type Alias | Original Type | Description |
|---|---|---|
| Rsp | HttpResponse | Standard HTTP Response |
| Rqs | HttpRequest | Standard HTTP Request |
| Rlt | Result<(), actix::Error> | Standard Actix Result |
| MainRlt | (Varies) | Main Function Result |
| FileRlt | (Varies) | File Streaming Result |
3. Responses & Routing
fr-rust provides utility functions to return standard HTTP responses, stream files, and parse JSON easily.
File Streaming
Stream large files directly to the client with ease.
pub async
Standard & JSON Responses
| Response Helper | Purpose | Example |
|---|---|---|
| http_ok(msg) | 200 OK with string | http_ok("Success") |
| http_bad(msg) | 400 Bad Request with string | http_bad("Error") |
| send_str(msg) | Raw string response | send_str("Hello") |
| send_json(data) | Standard JSON response (Vec/Struct) | send_json(vec![1, 2]) |
| http_ok_json(data) | 200 OK with JSON map/macro | http_ok_json(json!({"a": 1})) |
| http_bad_json(data) | 400 Bad Request with JSON | http_bad_json(json!({"err": true})) |
| Implementation Example: |
async
4. WebSockets
Built on top of actix-ws, the WebSocket manager (WsManager) provides robust room management and messaging utilities. Core Methods:
// Use actix-ws with this manager
// 1. Create a high-performance unbounded channel for a user connection
use mpsc;
let = ;
// 2. Manage Users
ws_manager.register;
ws_manager.drop_user;
// 3. Manage Rooms
ws_manager.join_room;
ws_manager.leave_room;
ws_manager.drop_room; // Note: Renamed from drop_user(room_name) to clarify intent
// 4. Send Messages
ws_manager.msg_user;
ws_manager.msg_room;
ws_manager.broadcast;
// 5. Query Rooms
let messages = ws_manager.get_room_msgs;
// Use the manager in actix route
async
5. Database Operations
Access your relational database easily via AppData.
- execute: Run queries without expecting a return dataset (CREATE, INSERT, UPDATE).
- query: Fetch multiple rows.
- query_one: Fetch exactly one row.
- query_opt: Fetch an optional row (returns Option).
async
6. Redis Integrations
Injected via AppData, the Redis client supports standard operations, TTL, Hashes, Lists, Sets, and coordinated batch Pub/Sub. Pub/Sub Example:
// Another operations same as deadpool_redis or redis-rs
// It just helps you to create redis pool auto & handle pub/sub.
use AsyncCommands;
use StreamExt;
let redis = redis_manager.get_connection.await.unwrap;
// Publish a message
redis.publish.await.unwrap;
// Subscribe to a stream
let mut stream = redis.subscribe.await?;
while let Some = stream.next.await
7. Verification & Notification Services
7.1 JSON Web Tokens (JWT)
let secret = "my_ultra_secure_secret_key_2026";
let user_id = "user_12345";
// Generate token (No expiration)
let forever_token = jwt.generate_token.unwrap;
// Generate token (Expiring)
let current_time = now.duration_since.unwrap.as_secs as usize;
let expiry_timestamp = current_time + 3600; // 1 hour from now
let expiring_token = jwt.generate_exp_token.unwrap;
// Verify
let is_valid = jwt.verify_token;
7.2 OTP Generation & Verification
Access via AppData.
let otp = otp_service.generate_otp.await.unwrap; // 6-digit OTP
if otp_service.verify_otp.await.unwrap
7.3 Link Verification Tokens
Access via AppData.
let token = linkv_service.generate_token.await.unwrap;
if linkv_service.verify_token.await.unwrap
7.4 Email Service
Access via AppData.
async
8. Cryptography Service
Inject AppData to securely encrypt data or hash passwords.
- Symmetric Encryption: encrypt_text / decrypt_text
- Password Hashing: hash_data / verify_hash (Async, highly secure)
- Fast Hashing: sha256_hash
async
9. General Utilities
Standalone utility macros and functions for rapid development.
// Capture user input directly from the terminal (Python-like)
let name = input;
// Generate a random HEX-encoded key of a specific byte length
let key = generate_key; // 100 character random string