A URL shortener service built with Rust, implementing CQRS (Command Query Responsibility Segregation) pattern and Clean Architecture principles.
- 🏗️ CQRS Architecture - Separate read and write operations
- 🧹 Clean Architecture - Clear separation of concerns
- ⚡ Fast & Async - Built on Tokio and Axum
- 🧪 Well-tested - Comprehensive test coverage
- 🎯 Type-safe - Leveraging Rust's type system
- 📦 In-memory storage - DashMap for concurrent access
src/
├── app/ # Business logic
│ ├── command/ # Write operations (CQRS Command)
│ └── query/ # Read operations (CQRS Query)
├── adapters/ # Infrastructure adapters
│ └── inmemory/ # In-memory repository implementation
├── ports/ # External interfaces
│ └── httpapi/ # HTTP API (Axum)
├── di/ # Dependency Injection container
├── error.rs # Error types
└── id_provider.rs # ID generation (NanoID)
- Rust 1.70+ (2021 edition)
cargo runServer starts on http://localhost:3001
Request:
curl -X POST http://localhost:3001/ \
-H 'Content-Type: application/json' \
-d '{"url": "https://google.com"}'Response:
{
"id": "abc123"
}Request:
curl http://localhost:3001/abc123Response:
{
"url": "https://google.com/"
}Invalid URL:
# Request
curl -X POST http://localhost:3001/ \
-H 'Content-Type: application/json' \
-d '{"url": "not-a-valid-url"}'
# Response (400 Bad Request)
{
"message": "Invalid URL"
}URL Not Found:
# Request
curl http://localhost:3001/nonexistent
# Response (404 Not Found)
{
"message": "Not found"
}Run all tests:
cargo testRun tests with output:
cargo test -- --nocaptureRun specific test:
cargo test test_invalid_url# Debug build
cargo build
# Release build (optimized)
cargo build --release./target/release/rust-shortener-cqrs# Format code
cargo fmt
# Lint with Clippy
cargo clippy --all-targets --all-features -- -D warningsCurrently, the server port is hardcoded to 3001 in main.rs.
- Axum - Web framework
- Tokio - Async runtime
- DashMap - Concurrent HashMap
- NanoID - Unique ID generation
- Serde - Serialization/Deserialization
- Tower - Middleware
- Tracing - Logging
- Add persistent storage (PostgreSQL/Redis)
- Environment-based configuration
- Rate limiting middleware
- URL scheme validation (http/https only)
- SSRF protection
- HTTP redirects instead of JSON response
- Metrics and monitoring
- Health check endpoint
- Docker support
- CI/CD pipeline
Contributions are welcome! Please feel free to submit a Pull Request.
This project is open source and available under the MIT License.
