RS-MOCK-SERVER 🦀
A simple, zero-configuration mock server built in Rust. Spin up a realistic REST API for local development or testing just by creating folders and files.
It works by scanning a directory and mapping its structure directly to API routes, with clever filename conventions for handling HTTP methods, dynamic parameters, and static assets.
Quick Start
Installation
# Install from crates.io
# Or build from source
&&
Basic Usage
# Start server (uses ./mocks folder, port 4520)
# Custom port and folder
Create Your First Endpoints
# Create mock directory structure
# Simple GET endpoint
# → GET /api/users
# Dynamic route with ID parameter
# → GET /api/users/123
# Full CRUD REST API
# → GET, POST, PUT, PATCH, DELETE /api/users[/{id}]
Core Filename Patterns
| Pattern | Example | Generated Route | Description |
|---|---|---|---|
[method] |
get.json |
GET /api/users |
Basic HTTP method |
[method]{id} |
get{id}.json |
GET /api/users/{id} |
Dynamic parameter |
[method]{value} |
get{admin}.json |
GET /api/users/admin |
Specific value |
[method]{start-end} |
get{1-5}.json |
GET /api/users/1 to /5 |
Numeric range |
Features
- 🚀 File-System Routing: Your folder structure defines your API routes. No config files needed.
- 🧩 Dynamic Path Generation: Create routes with parameters (
{id}), specific values ({admin}), and even numeric ranges ({1-10}) right from the filename. - ⚙️ Full HTTP Method Support: Define
GET,POST,PUT,DELETE,PATCH, andOPTIONSendpoints. - 🔗 In-Memory REST API: Create fully functional CRUD APIs with automatic ID generation and data persistence during runtime using special
rest.jsonorrest.jgdfiles. - 🔐 JWT Authentication: Automatic authentication system with login/logout endpoints and route protection using special
{auth}files. - 📤 File Upload & Download: Create upload endpoints with automatic file handling and download capabilities using special
{upload}folders. - 🖼️ Static File Serving: Automatically serves any file (like images, CSS, or JS) with its correct
Content-Typeif the filename doesn't match a method pattern. - 📊 JGD Support: Generate dynamic JSON responses using JGD (JSON Generation Definition) files with the JGD-rs library for realistic test data.
- 🌐 Public Directory Serving: Serve a directory of static files (e.g., a frontend build) from a root public folder, or map a folder like public-assets to a custom /assets route.
- 🔄 Hot Reload: Automatically restarts the server when files are added, modified, or deleted in the mock directory.
- 🌐 Web Interface: Access the root URL to get an interactive web interface for testing all your endpoints directly in the browser.
- 🔧 Configurable: Easily change the port and mock directory via command-line arguments.
- ⚡ Lightweight & Fast: Built with Rust for minimal resource usage and maximum performance.
- 🗄️ SQL Routes: Use
.sqlfiles to create GET endpoints that execute SQL queries against the in-memory database and return results as JSON.
How It Works
The server recursively scans a root directory (defaults to ./mocks) and translates the file and folder paths into API endpoints.
Folder Structure → URL Path
The path of each folder becomes the base URL for the routes within it.
- A folder at
./mocks/api/userscreates the base route/api/users. - A nested folder at
./mocks/api/users/profilescreates the base route/api/users/profiles.
Filename Conventions → Endpoints
The name of a file determines the HTTP method and the final URL segment. The content of the file is served as the response body.
The following table shows how different filename patterns are mapped to routes, assuming they are inside a ./mocks/api/users directory:
| Filename Pattern | Example File | Generated Route(s) | Description |
|---|---|---|---|
[method] |
get.json |
GET /api/users |
Creates a route for a standard HTTP method. |
[method]{id} |
get{id}.json |
GET /api/users/{id} |
A dynamic segment that accepts any value in that position. |
[method]{value} |
get{admin}.json |
GET /api/users/admin |
Matches a specific, hardcoded value. |
[method]{start-end} |
get{1-5}.json |
GET /api/users/1GET /api/users/2...GET /api/users/5 |
A numeric range that generates multiple distinct routes. |
rest[{params}] |
rest.json |
GET /api/usersPOST /api/usersGET /api/users/{id}PUT /api/users/{id}PATCH /api/users/{id}DELETE /api/users/{id} |
In-Memory REST API.Creates a full CRUD API with automatic ID generation, data persistence,and initial data loading from the JSON array in the file. |
rest[{params}] |
rest.jgd |
GET /api/usersPOST /api/usersGET /api/users/{id}PUT /api/users/{id}PATCH /api/users/{id}DELETE /api/users/{id} |
In-Memory REST API with JGD.Creates a full CRUD API with dynamic fake data generation using JGDas initial data, then maintains persistence during runtime. |
{auth} |
{auth}.json |
POST /api/loginPOST /api/logout |
JWT Authentication. Creates login and logout endpoints with JWT token generationand validation middleware for route protection. |
[filename].[ext] |
avatar.png |
GET /api/users/avatar |
Static File. Any filename that doesn't match the patterns above is served as a static asset.The Content-Type header is automatically set based on the file's extension. |
[filename].jgd |
users.jgd |
GET /api/users/users |
JGD File. JSON Generation Definition files that dynamically generate realistic JSON datausing the JGD-rs library. |
Documentation
📚 Feature Guides
- Basic Routing - Learn filename patterns and route creation
- REST APIs - Build full CRUD APIs with persistence
- Authentication - JWT-based auth with route protection
- File Uploads - Handle file uploads and downloads
- Static Files - Serve assets with automatic Content-Type
- JGD Files - Generate dynamic JSON with realistic data
- Web Interface - Interactive testing and management
- Hot Reload - Development workflow and file monitoring
- SQL Routes - Execute
.sqlfiles as GET endpoints against in-memory database
🚀 Quick Examples
Simple API
# → GET /api/health
CRUD REST API
# → Full CRUD at /products/*
Authentication
# → POST /auth/login, POST /auth/logout
File Uploads
# → POST /upload, GET /upload, GET /upload/{filename}
Example Structure
mocks/
├── api/
│ ├── users/
│ │ ├── get.json # GET /api/users
│ │ ├── get{id}.json # GET /api/users/{id}
│ │ └── post.json # POST /api/users
│ └── products/
│ └── rest.json # Full CRUD /api/products/*
├── auth/
│ └── {auth}.json # Login/logout endpoints
├── {upload}/ # File upload endpoints
└── assets/
└── logo.png # GET /assets/logo.png
Generated Routes:
GET /api/users- List usersGET /api/users/123- Get user by IDPOST /api/users- Create userGET,POST,PUT,DELETE /api/products/*- Full REST APIPOST /auth/login- User authenticationPOST /upload- File uploadsGET /assets/logo.png- Static assetsGET /- Homepage
CLI Options
Development
Getting Started
-
Clone the repository:
-
Install dependencies:
-
Set up development environment:
Development Commands
The project includes a Makefile with convenient development commands:
# Run tests
# Run tests in watch mode (requires cargo-watch)
# Run all quality checks (tests, clippy, formatting)
# Format code
# Run Clippy linter
# Build the project
# Run the application
# Set up Git hooks
Pre-commit Hooks
This project uses Git pre-commit hooks to ensure code quality. The hooks automatically:
- ✅ Run all tests before each commit
- ❌ Block commits if tests fail
- 🎯 Ensure consistent code quality
Automatic setup: Pre-commit hooks are installed automatically when you run make dev-setup or make setup-hooks.
Manual bypass (not recommended): If you need to commit without running tests:
Configuration: You can customize the pre-commit checks by editing .git/hooks/pre-commit. The hook supports:
- Running tests (enabled by default)
- Running Clippy checks (disabled by default)
- Checking code formatting (disabled by default)
Testing
The project includes comprehensive test coverage:
# Run all tests
# Run specific test module
# Run tests with output
Contributing
- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Make your changes and ensure tests pass
- Commit your changes (pre-commit hooks will run automatically)
- Push to your fork and submit a pull request
Code Quality: All contributions must:
- Pass existing tests
- Include tests for new functionality
- Follow Rust formatting standards (
cargo fmt) - Pass Clippy linting (
cargo clippy)