webshelf

Crates.iowebshelf
lib.rswebshelf
version0.1.0
created_at2026-01-07 07:22:32.004803+00
updated_at2026-01-11 13:43:43.270266+00
descriptionThe best way to develop your web service with one click.
homepagehttps://www.openpick.org/webshelf
repositoryhttps://github.com/aiqubits/webshelf
max_upload_size
id2027699
size214,876
(aiqubits)

documentation

README

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": "newuser@example.com",
  "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": "newuser@example.com",
  "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": "newuser@example.com",
  "password": "SecurePass123",
  "name": "New User"
}

Response:

{
  "id": "550e8400-e29b-41d4-a716-446655440000",
  "email": "newuser@example.com",
  "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": "updated@example.com",
  "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

Commit count: 4

cargo fmt