Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

The best way to develop your web service with one click.

License

Notifications You must be signed in to change notification settings

aiqubits/webshelf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

WebShelf

License: MIT Rust Rust-Agent PRs Welcome Live Demo

The best way to develop your web service with one click.

WebShelf is a production-ready Rust web framework built on Axum, providing a complete backend scaffold with authentication, database integration, distributed locking, and comprehensive middleware support.

✨ Features

  • πŸ” JWT Authentication - Secure token-based authentication with Argon2 password hashing
  • πŸ—„οΈ Database Integration - PostgreSQL support via SeaORM with async operations
  • πŸ”’ Distributed Locking (Optional) - Redis-based distributed locks for scalable services
  • πŸ›‘οΈ Middleware Stack - Panic capture, CORS, tracing, and authentication layers
  • βœ… Input Validation - Request validation with email and password rules
  • πŸ“ Structured Logging - Tracing-based logging with configurable levels
  • βš™οΈ Flexible Configuration - TOML-based config with CLI argument overrides
  • πŸ§ͺ Testing Support - Unit tests and integration test framework
  • 🚦 RESTful API - Complete CRUD operations for user management
  • πŸ“¦ Production Ready - Error handling, compression, and graceful shutdown
  • πŸ”„ Utility Functions - Configuration loading, error handling, logging, and so on

πŸ“‹ Requirements

  • Rust 1.92 or higher
  • PostgreSQL 16+
  • Redis 7+ (for distributed locking)

πŸš€ Quick Start

1. Clone and Setup

git clone https://github.com/aiqubits/webshelf.git
cd webshelf

2. Configure Database

Create a Docker network:

docker network create webshelf-net

Create a PostgreSQL database:

# creatdb webshelf
docker run --name webshelf-postgres \
  --network webshelf-net \
  -e POSTGRES_USER=postgres \
  -e POSTGRES_PASSWORD=password \
  -e POSTGRES_DB=webshelf \
  -p 5432:5432 \
  -d postgres:16

Start Redis:

docker run --name webshelf-redis \
  --network webshelf-net \
  -p 6379:6379 \
  -d redis:7-alpine

Copy and edit configuration:

mv config.toml.example config.toml
# Edit config.toml with your database credentials

3. Run the Server

cargo run

The server will start on http://0.0.0.0:3000 by default.

πŸ”§ Configuration

Configuration File (config.toml)

# Database connection
database_url = "postgres://postgres:password@localhost:5432/webshelf"

# Redis for distributed locking
redis_url = "redis://localhost:6379"

# JWT settings
jwt_secret = "your-super-secret-key-change-in-production"
jwt_expiry_seconds = 3600

# Server settings
[server]
host = "0.0.0.0"
port = 3000

# Rate limiting
[rate_limit]
per_second = 2
burst_size = 5

Command Line Arguments

webshelf [OPTIONS]

Options:
  -H, --host <HOST>              Server bind address [default: 0.0.0.0]
  -P, --port <PORT>              Server port [default: 3000]
  -E, --env <ENV>                Environment [default: development]
  -C, --config <CONFIG>          Configuration file path [default: config.toml]
  -L, --log-level <LOG_LEVEL>    Log level [default: info]
  -h, --help                     Print help
  -V, --version                  Print version

Example:

cargo run -- --host 127.0.0.1 --port 8080 --log-level debug

πŸ“š API Endpoints

Authentication

Register User

POST /api/public/auth/register
Content-Type: application/json

{
  "email": "[email protected]",
  "password": "Password123",
  "name": "User Name"
}

Response:

{
  "message": "User registered successfully",
  "user_id": "550e8400-e29b-41d4-a716-446655440000"
}

Login

POST /api/public/auth/login
Content-Type: application/json

{
  "email": "[email protected]",
  "password": "Password123"
}

Response:

{
  "token": "eyJ0eXAiOiJKV1QiLCJhbGc...",
  "token_type": "Bearer",
  "expires_in": 3600,
  "user_id": "550e8400-e29b-41d4-a716-446655440000",
  "role": "user"
}

User Management

Create User

POST /api/users
Content-Type: application/json

{
  "email": "[email protected]",
  "password": "SecurePass123",
  "name": "New User"
}

Response:

{
  "id": "550e8400-e29b-41d4-a716-446655440000",
  "email": "[email protected]",
  "name": "New User",
  "role": "user",
  "created_at": "2026-01-11T06:00:00Z",
  "updated_at": "2026-01-11T06:00:00Z"
}

Get User

GET /api/users/{id}

Update User

PUT /api/users/{id}
Content-Type: application/json

{
  "email": "[email protected]",
  "name": "Updated Name",
  "role": "admin"
}

Delete User

DELETE /api/users/{id}

List Users (with pagination)

GET /api/users?page=1&per_page=10

Health Check

GET /api/health

Response:

{
  "status": "ok",
  "version": "0.0.1"
}

πŸ—οΈ Project Structure

webshelf/
│── k8s/                     # Kubernetes manifests
│── migrations/              # Database migrations
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ handlers/            # Request handlers
β”‚   β”‚   β”œβ”€β”€ api.rs           # API request handlers
β”‚   β”‚   β”œβ”€β”€ auth.rs          # Auth request handlers
β”‚   β”‚   └── mod.rs
β”‚   β”œβ”€β”€ middleware/          # Middleware components
β”‚   β”‚   β”œβ”€β”€ auth.rs          # JWT authentication
β”‚   β”‚   β”œβ”€β”€ panic.rs         # Panic capture
β”‚   β”‚   └── mod.rs
β”‚   β”œβ”€β”€ models/              # Data models
β”‚   β”‚   β”œβ”€β”€ user.rs          # User entity
β”‚   β”‚   └── mod.rs
β”‚   β”œβ”€β”€ routes/              # API routes
β”‚   β”‚   β”œβ”€β”€ api.rs           # User CRUD endpoints
β”‚   β”‚   β”œβ”€β”€ auth.rs          # Authentication endpoints
β”‚   β”‚   └── mod.rs
β”‚   β”œβ”€β”€ services/            # Business logic
β”‚   β”‚   β”œβ”€β”€ auth.rs          # Authentication service
β”‚   β”‚   β”œβ”€β”€ user.rs          # User service
β”‚   β”‚   β”œβ”€β”€ lock.rs          # Distributed locking
β”‚   β”‚   └── mod.rs
β”‚   β”œβ”€β”€ utils/               # Utilities
β”‚   β”‚   β”œβ”€β”€ config.rs        # Configuration loading
β”‚   β”‚   β”œβ”€β”€ error.rs         # Error types
β”‚   β”‚   β”œβ”€β”€ logger.rs        # Logging setup
β”‚   β”‚   β”œβ”€β”€ password.rs      # Password hashing
β”‚   β”‚   β”œβ”€β”€ validator.rs     # Input validation
β”‚   β”‚   └── mod.rs
β”‚   β”œβ”€β”€ bootstrap.rs         # Initialization logic
β”‚   β”œβ”€β”€ lib.rs               # Library exports
β”‚   β”œβ”€β”€ main.rs              # Application entry
β”‚   └── migrations.rs        # Database migrations
β”œβ”€β”€ tests/
β”‚   └── integration_tests.rs # Integration tests
β”œβ”€β”€ Cargo.toml               # Dependencies
β”œβ”€β”€ config.toml.example      # Configuration
└── README.md                # This file

πŸ§ͺ Testing

Run Unit Tests

cargo test

Run Integration Tests

Note: Integration tests require PostgreSQL and Redis to be running.

# Start PostgreSQL and Redis first
cargo test --test integration_tests

Test Coverage

  • βœ… Password hashing and verification
  • βœ… Input validation (email, password)
  • βœ… Configuration loading
  • βœ… API endpoint integration tests
  • βœ… User CRUD operations

πŸ” Security Features

  • Password Hashing: Argon2 algorithm with salt
  • JWT Tokens: Secure token generation and validation
  • Input Validation: Email format and password strength checks
  • CORS: Configurable cross-origin resource sharing
  • Panic Recovery: Graceful error handling without server crashes

Password Requirements

  • Minimum 8 characters
  • At least one lowercase letter
  • At least one uppercase letter
  • At least one digit

πŸ“¦ Dependencies

Core

  • axum - Web framework
  • tokio - Async runtime
  • sea-orm - ORM for PostgreSQL
  • redis - Distributed locking

Authentication

  • jsonwebtoken - JWT handling
  • argon2 - Password hashing

Utilities

  • serde - Serialization
  • validator - Input validation
  • tracing - Structured logging
  • anyhow/thiserror - Error handling

See Cargo.toml for the complete dependency list.

πŸ› οΈ Development

Run in Development Mode

cargo run -- --env development --log-level debug

Build for Production

cargo build --release

Run Production Binary

./target/release/webshelf --config prod.config.toml

πŸ“Š Middleware Stack

Middleware execution order (innermost to outermost):

  1. Panic Capture - Catches panics and returns 500 errors
  2. Authentication - JWT token validation (for protected routes)
  3. Trace - Request/response logging
  4. CORS - Cross-origin resource sharing

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ‘₯ Authors

About

The best way to develop your web service with one click.

Resources

License

Stars

Watchers

Forks

Packages

No packages published