🚀 RustyX
A fast, minimalist web framework for Rust inspired by ExpressJS
Getting Started • Installation • Documentation • Examples • Deployment • Contributing
📖 Table of Contents
- Features
- Installation
- Getting Started
- Core Concepts
- Advanced Features
- Deployment
- API Reference
- Examples
- Contributing
- License
✨ Features
| Feature | Description |
|---|---|
| 🎯 ExpressJS-like API | Familiar interface for JavaScript/Node.js developers |
| ⚡ Blazingly Fast | Built on Hyper and Tokio for maximum performance |
| 🔌 Middleware Support | Logger, CORS, Rate Limiting, Helmet, Timeout |
| 🗄️ Multi-Database ORM | MongoDB, MySQL, PostgreSQL, SQLite support |
| 🌐 WebSocket Support | Real-time bidirectional communication |
| 📁 Static Files | Serve static assets with MIME type detection |
| 📤 File Upload | Multer-like file upload with validation |
| 🔒 Type-Safe | Leverage Rust's type system for safer code |
| 📝 JSON-First | Designed for building REST APIs |
| 🛡️ Security | Built-in Helmet middleware for security headers |
| ⏱️ Rate Limiting | Protect APIs from abuse |
📦 Installation
Prerequisites
- Rust 1.70+ - Install Rust
- Cargo - Comes with Rust
Add to Cargo.toml
[]
= "0.1.0"
= { = "1", = ["full"] }
= { = "1.0", = ["derive"] }
= "1.0"
With Database Features
[]
# SQLite (default)
= "0.1.0"
# MySQL
= { = "0.1.0", = ["mysql"] }
# PostgreSQL
= { = "0.1.0", = ["postgres"] }
# MongoDB
= { = "0.1.0", = ["mongodb"] }
# All databases
= { = "0.1.0", = ["full"] }
Feature Flags
| Feature | Description | Default |
|---|---|---|
default |
SQLite support | ✅ |
mysql |
MySQL database | ❌ |
postgres |
PostgreSQL database | ❌ |
sqlite |
SQLite database | ✅ |
mongodb |
MongoDB database | ❌ |
full |
All database drivers | ❌ |
🚀 Getting Started
1. Create a New Project
2. Add Dependencies
Edit Cargo.toml:
[]
= "my_api"
= "0.1.0"
= "2021"
[]
= "0.1.0"
= { = "1", = ["full"] }
= { = "1.0", = ["derive"] }
= "1.0"
3. Create Your First API
Edit src/main.rs:
use *;
async
4. Run Your Server
Visit http://localhost:3000 in your browser!
📚 Core Concepts
Application
The RustyX struct is the main entry point:
use *;
let app = new;
// Configure routes
app.get;
app.post;
app.put;
app.delete;
app.patch;
// Add middleware
app.use_middleware;
app.use_middleware;
// Start server
app.listen.await?;
Routing
RustyX supports ExpressJS-style routing with path parameters:
// Basic routes
app.get;
app.post;
// Path parameters
app.get;
// Multiple parameters
app.get;
// Using Router for grouping
let mut api = with_prefix;
api.get;
api.post;
app.use_router;
Request
The Request object provides access to request data:
|req, res| async move
Response
The Response object provides methods for sending responses:
|req, res| async move
Middleware
Middleware functions process requests before they reach route handlers:
use *;
let app = new;
// Built-in middleware
app.use_middleware; // Request logging
app.use_middleware; // CORS headers
app.use_middleware; // Security headers
app.use_middleware; // 30s timeout
app.use_middleware; // Add X-Request-ID
app.use_middleware; // Add X-Response-Time
// Rate limiting
let rate_config = new; // 100 req/min
app.use_middleware;
// Custom middleware
app.use_middleware;
🔥 Advanced Features
File Upload
Handle single and multiple file uploads (similar to Express Multer):
use *;
let uploader = new;
app.post;
Configuration options:
new
.destination // Upload directory
.max_file_size_mb // Max 10MB
.max_files // Max 5 files per request
.images_only // Only images (PNG, JPG, GIF, WebP)
.documents_only // Only docs (PDF, DOC, XLS)
.allowed_extensions // Custom extensions
.keep_original_name // Keep original filename
.use_uuid // UUID filename (default)
📖 Full documentation: docs/UPLOAD.md
Rate Limiting
Protect your API from abuse:
use ;
// Basic: 100 requests per 60 seconds
app.use_middleware;
// Advanced configuration
let config = new
.message
.skip;
app.use_middleware;
WebSocket Support
Real-time communication:
use ;
let ws_server = new;
// Send to specific client
ws_server.send_to.await;
// Broadcast to all
ws_server.broadcast.await;
// Room-based messaging
ws_server.join_room;
ws_server.broadcast_to_room.await;
Static File Serving
Serve static files:
use ;
// Basic usage
let config = new;
app.get;
// With options
let config = new
.index
.max_age
.directory_listing;
Database Integration
Connect and query databases:
use *;
// Configure SQLite
let config = new;
// Configure PostgreSQL
let config = new
.host
.port
.username
.password;
// Initialize connection
init_db.await?;
// Query builder
let query = table
.select
.where_eq
.order_by
.limit
.build;
🚀 Deployment
Production Build
# Create optimized release build
# The binary is at ./target/release/my_api
Docker Deployment
Create Dockerfile:
# Build stage
FROM rust:1.75 as builder
WORKDIR /app
COPY . .
RUN cargo build --release
# Runtime stage
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y libssl3 ca-certificates && rm -rf /var/lib/apt/lists/*
COPY --from=builder /app/target/release/my_api /usr/local/bin/
EXPOSE 3000
CMD ["my_api"]
Build and run:
Docker Compose
version: '3.8'
services:
api:
build: .
ports:
- "3000:3000"
environment:
- RUST_LOG=info
- DATABASE_URL=postgres://user:pass@db:5432/mydb
depends_on:
- db
db:
image: postgres:15
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: mydb
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
Environment Variables
use env;
let port: u16 = var
.unwrap_or_else
.parse
.unwrap;
let db_url = var
.expect;
Systemd Service
Create /etc/systemd/system/my-api.service:
[Unit]
Description=My RustyX API
After=network.target
[Service]
Type=simple
User=www-data
WorkingDirectory=/opt/my-api
ExecStart=/opt/my-api/my_api
Restart=always
RestartSec=5
Environment=RUST_LOG=info
Environment=PORT=3000
[Install]
WantedBy=multi-user.target
Enable and start:
Nginx Reverse Proxy
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
}
}
Cloud Deployment
AWS / DigitalOcean / Linode
- Create a VPS instance
- Install Rust:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh - Clone and build your project
- Configure systemd service
- Set up Nginx reverse proxy
- Configure SSL with Let's Encrypt
Railway / Render / Fly.io
These platforms auto-detect Rust projects. Just push your code!
# Fly.io example
📖 API Reference
RustyX (Application)
| Method | Signature | Description |
|---|---|---|
new() |
RustyX::new() |
Create new app |
get() |
.get(path, handler) |
GET route |
post() |
.post(path, handler) |
POST route |
put() |
.put(path, handler) |
PUT route |
delete() |
.delete(path, handler) |
DELETE route |
patch() |
.patch(path, handler) |
PATCH route |
use_middleware() |
.use_middleware(fn) |
Add middleware |
use_router() |
.use_router(path, router) |
Mount router |
listen() |
.listen(port).await |
Start server |
Request
| Method | Returns | Description |
|---|---|---|
method() |
&Method |
HTTP method |
path() |
&str |
Request path |
param(name) |
Option<&String> |
URL parameter |
query_param(name) |
Option<&String> |
Query parameter |
json<T>() |
Result<T> |
Parse JSON body |
body() |
&Bytes |
Raw body |
header(name) |
Option<&str> |
Get header |
bearer_token() |
Option<&str> |
Bearer token |
ip() |
IpAddr |
Client IP |
Response
| Method | Description |
|---|---|
.status(code) |
Set status code |
.json(data) |
Send JSON |
.send(text) |
Send text |
.html(html) |
Send HTML |
.redirect(url) |
Redirect |
.header(name, value) |
Set header |
.cookie(name, value, opts) |
Set cookie |
📝 Examples
REST API with CRUD
use *;
async
With Authentication
app.use_middleware;
🤝 Contributing
We welcome contributions! Please see CONTRIBUTING.md for guidelines.
- Fork the repository
- Create feature branch:
git checkout -b feature/amazing-feature - Commit changes:
git commit -m 'Add amazing feature' - Push:
git push origin feature/amazing-feature - Open a Pull Request
📄 License
This project is licensed under the MIT License - see the LICENSE file.
Made with ❤️ by the RustyX Team
⭐ Star us on GitHub!