A REST API for compressing and managing data, built with Rust and Axum, following the 12-factor principles.
This application follows the 12-factor methodology for building software-as-a-service applications:
- Codebase - Single codebase tracked in Git
- Dependencies - Explicitly declared and isolated (Cargo.toml)
- Config - Stored in environment variables (.env file for development)
- Backing services - Database configuration via DATABASE_URL
- Build, release, run - Separate stages (build.sh script)
- Processes - Stateless, share-nothing architecture
- Port binding - Exports services via port binding
- Concurrency - Scales out via process model (multiple workers)
- Disposability - Fast startup and graceful shutdown
- Dev/prod parity - Minimal divergence between environments
- Logs - Treat logs as event streams
- Admin processes - Run admin tasks as one-off processes
- RESTful API for managing compressed data
- CRUD operations (Create, Read, Update, Delete)
- JSON request/response handling
- Structured logging with tracing
- Configuration via environment variables
- Health check endpoint
- PostgreSQL database storage
- Administrative CLI for database management
- Makefile for simplified commands
- Development mode with auto-reload
- OpenAPI documentation with interactive UI
GET /- Welcome messageGET /health- Health checkGET /items- Get all itemsPOST /items- Create a new itemGET /items/{id}- Get a specific itemPUT /items/{id}- Update a specific itemDELETE /items/{id}- Delete a specific itemGET /scalar- OpenAPI documentation (Scalar UI)
{
"id": "string (UUID)",
"name": "string",
"data": "string (base64 encoded)",
"created_at": "string (ISO 8601)",
"updated_at": "string (ISO 8601)"
}The application is configured through environment variables:
HOST- Server host (default: 0.0.0.0)PORT- Server port (default: 3000)DATABASE_URL- PostgreSQL database connection stringDEBUG- Debug mode (default: false)RUST_LOG- Log level (default: info)
-
Install Rust: https://www.rust-lang.org/tools/install
-
Install cargo-watch for development mode:
cargo install cargo-watch
-
Install PostgreSQL: https://www.postgresql.org/download/
-
Create a PostgreSQL database:
CREATE DATABASE rust_compress_api;
-
Clone the repository:
git clone <repository-url> cd rust_compress_api
-
Copy the example environment file:
cp .env.example .env
-
Update the DATABASE_URL in the .env file to match your PostgreSQL setup
For development with auto-reload:
make devFor development with auto-reload and database:
make dev-db-
Start only the database service:
make db-up
-
In another terminal, run the application:
make run
-
The API will be available at
http://localhost:3000
-
Start all services:
docker-compose up
-
The API will be available at
http://localhost:3000
-
Run the application:
make run
-
The API will be available at
http://localhost:3000
The API includes interactive documentation using Scalar UI:
- Start the application
- Navigate to
http://localhost:3000/scalarin your browser - Explore and test all API endpoints
The documentation includes:
- Detailed descriptions of all endpoints
- Request/response schemas
- Example requests and responses
- Interactive testing capabilities
The application includes a CLI tool for administrative tasks:
Using Makefile commands:
# Count total items in database
make admin-count
# Show database statistics
make admin-stats
# Delete all items from database (requires confirmation)
make admin-clearOr using cargo directly:
# Count total items in database
cargo run --bin admin -- count
# Show database statistics
cargo run --bin admin -- stats
# Delete all items from database (requires confirmation)
cargo run --bin admin -- clear --confirmUsing Makefile:
make buildOr using cargo directly:
cargo build --releaseThe binaries will be located at:
target/release/rust_compress_api- Main API servertarget/release/admin- Administrative CLI tool
curl -X POST http://localhost:3000/items \
-H "Content-Type: application/json" \
-d '{
"name": "My Data",
"data": "SGVsbG8gV29ybGQ="
}'curl http://localhost:3000/itemscurl http://localhost:3000/items/{id}curl -X PUT http://localhost:3000/items/{id} \
-H "Content-Type: application/json" \
-d '{
"name": "Updated Name"
}'curl -X DELETE http://localhost:3000/items/{id}The application uses structured logging with different levels (error, warn, info, debug, trace).
Set the RUST_LOG environment variable to control the log level.
This application can be deployed to any cloud platform that supports running Rust binaries. It follows the 12-factor principles making it suitable for containerized deployments (Docker, Kubernetes) or platform-as-a-service providers (Heroku, etc.).