Sign inSign up

ducminhgd/api-mock-server

By ducminhgd

•Updated 4 months ago

Integration & delivery
Developer tools
0

176

ducminhgd/api-mock-server repository overview

⁠API Mock Server

A self-hosted API mock server built with Rust, Leptos⁠ (full-stack SSR + WASM), and SQLite. Define collections of mock endpoints and serve deterministic HTTP responses without a real backend.

⁠Features

  • Manage collections, endpoints, and shares via a browser UI
  • Mock any HTTP method and path with configurable status code, headers, and response body
  • Path parameters using {param} syntax (e.g. /users/{id})
  • Import/export collections from Postman and Bruno formats
  • User and group management with role-based access (Admin / Regular)
  • JWT authentication
  • SQLite by default — zero external dependencies for storage

⁠Requirements

ToolVersion
Rust1.95+
cargo-leptos0.3.6
wasm32-unknown-unknown target(added via rustup)
Docker + Composefor containerised runs

⁠Quick start (local)

# 1. Add the WASM target (once)
rustup target add wasm32-unknown-unknown

# 2. Install cargo-leptos (once)
cargo install cargo-leptos --version 0.3.6 --locked

# 3. Copy and edit the environment file
cp .env.example .env          # set JWT_SECRET at minimum

# 4. Run migrations (SQLite file is created automatically)
DATABASE_URL=sqlite://./dev.db sqlx migrate run

# 5. Start the dev server with hot-reload
DATABASE_URL=sqlite://./dev.db JWT_SECRET=dev-secret cargo leptos watch

Open http://localhost:3000⁠.

⁠Environment variables

VariableDefaultDescription
DATABASE_URL—SQLite: sqlite:///path/to/app.db · Postgres: postgres://user:pass@host/db
JWT_SECRET—Required. Secret used to sign JWT tokens.
PORT3000HTTP port the server listens on.
LEPTOS_SITE_ADDR127.0.0.1:3000Full bind address (overrides PORT).
LEPTOS_SITE_ROOTsiteDirectory that contains the compiled frontend assets.

⁠Docker

⁠Single container — SQLite (default)
docker build -t api-mock-server .

docker run -d \
  -e JWT_SECRET=change-me \
  -p 3000:3000 \
  -v api_mock_data:/app/data \
  --name api-mock-server \
  api-mock-server

The SQLite database is stored in the /app/data volume and persists across restarts.

⁠Docker Compose — production

Pass -f docker-compose.yml to skip the dev override and build the optimised prod image stage:

# Start (builds if needed)
JWT_SECRET=change-me docker compose -f docker-compose.yml up -d

# Override the host port (default 3000)
APP_PORT=8080 JWT_SECRET=change-me docker compose -f docker-compose.yml up -d

The app_data named volume is created automatically and holds the SQLite file. JWT_SECRET defaults to change-me-in-production — always override it in production.

⁠Docker Compose — development (hot-reload)

Running without -f merges docker-compose.override.yml automatically. The override targets the dev image stage, mounts source files into the container, and runs cargo leptos watch so changes rebuild without restarting the container. Cargo registry and build artefacts are cached in named volumes (cargo_cache, target_cache) to avoid full recompiles on restart.

docker compose up

⁠Using PostgreSQL instead of SQLite

Pass a Postgres connection URL via DATABASE_URL:

# docker run
docker run -d \
  -e DATABASE_URL=postgres://user:pass@db-host/api_mock_server \
  -e JWT_SECRET=change-me \
  -p 3000:3000 \
  api-mock-server

# docker compose — add a db service or point at an external host
DATABASE_URL=postgres://user:pass@db-host/api_mock_server \
JWT_SECRET=change-me \
docker compose up -d

The same binary and migrations support SQLite, PostgreSQL, and MySQL — the driver is selected automatically from the URL scheme.

⁠Development

make dev        # cargo leptos watch
make test       # cargo test --features ssr
make lint       # clippy --fix + fmt
make build      # cargo leptos build --release
make pre-commit # lint + test + release build

⁠Project layout

src/
├── domain/          # Business entities and rules (no framework imports)
├── application/     # Use cases, DTOs, repository interfaces
├── adapters/
│   ├── http/        # Axum REST handlers
│   └── ui/          # Leptos components (SSR + WASM)
└── infrastructure/  # SQLx repositories, JWT, bcrypt, config
migrations/          # SQLx versioned SQL migrations
style/               # CSS
public/              # Static assets

⁠API reference

All REST endpoints are prefixed with /api.

MethodPathDescription
POST/api/auth/loginObtain a JWT token
GET/api/collectionsList collections
POST/api/collectionsCreate a collection
GET/api/collections/:idGet a collection
PUT/api/collections/:idUpdate a collection
DELETE/api/collections/:idDelete a collection
POST/api/collections/:id/duplicateDuplicate a collection
POST/api/collections/:id/transferTransfer ownership
GET/api/collections/:id/sharesList shares
POST/api/collections/:id/sharesAdd a share
PUT/api/collections/:id/shares/:sidUpdate share role
DELETE/api/collections/:id/shares/:sidRemove a share
GET/api/collections/:id/endpointsList endpoints
POST/api/collections/:id/endpointsCreate an endpoint
GET/api/collections/:id/endpoints/:eidGet an endpoint
PUT/api/collections/:id/endpoints/:eidUpdate an endpoint
DELETE/api/collections/:id/endpoints/:eidDelete an endpoint
POST/api/collections/:id/endpoints/:eid/duplicateDuplicate an endpoint
GET/api/groupsList groups
POST/api/groupsCreate a group
GET/api/groups/:idGet a group
PUT/api/groups/:idUpdate a group
DELETE/api/groups/:idDelete a group
GET/api/usersList users
POST/api/usersCreate a user
GET/api/users/:idGet a user
PUT/api/users/:idUpdate a user
DELETE/api/users/:idDelete a user
POST/api/users/:id/reset-passwordReset a user's password

Mock endpoints are served under /mocks/:collection_id/*path.

Tag Summary

No tags have been pushed to this repository yet.