A high-performance URL shortener service built with modern Rust technologies. This service provides a simple API for creating shortened URLs and redirecting users to their original destinations.
- Fast URL shortening: Generate short, unique identifiers for long URLs
- Reliable redirects: Permanent redirects to original URLs with proper HTTP status codes
- PostgreSQL storage: Persistent storage with database migrations
- Comprehensive logging: Structured logging with tracing and request IDs
- Health monitoring: Built-in health check endpoint
- Production ready: Deployed on Shuttle with HTTPS support
- Framework: Axum - Modern async web framework
- Database: PostgreSQL with SQLx for type-safe queries
- Deployment: Shuttle - Serverless Rust deployment platform
- Logging: Structured logging with
tracingand Bunyan formatting - Testing: Comprehensive integration tests with testcontainers
POST /
Content-Type: text/plain
# Example
curl -d 'https://shuttle.dev/' https://your-domain.com/Response: Returns the shortened URL
https://your-domain.com/AbC123
GET /{id}
# Example
curl -L https://your-domain.com/AbC123Response: HTTP 308 Permanent Redirect to the original URL
GET /health_check
# Example
curl https://your-domain.com/health_checkResponse: HTTP 200 OK with empty body
- Rust (latest stable)
- Shuttle CLI
- PostgreSQL (for local development)
-
Clone the repository
git clone https://github.com/sentinel1909/url-shortener-v1.git cd url-shortener-v1 -
Install dependencies
cargo build
-
Set up the database
# The migrations will run automatically when starting the application # Database schema is located in /migrations
-
Run locally with Shuttle
shuttle run
-
Test the service
# Shorten a URL curl -d 'https://example.com' http://localhost:8000/ # Visit the shortened URL curl -L http://localhost:8000/AbC123
-
Login to Shuttle
shuttle login
-
Deploy
shuttle deploy
The project includes comprehensive integration tests using testcontainers for database testing.
# Run all tests
cargo test
# Run tests with logging output
TEST_LOG=1 cargo test
# Run specific test module
cargo test health_check- ✅ Health check endpoint
- ✅ URL shortening functionality
- ✅ URL redirection
- ✅ Database integration
- ✅ Error handling
src/
├── bin/
│ └── main.rs # Application entry point
└── lib/
├── lib.rs # Library crate root
├── startup.rs # Application configuration
├── telemetry.rs # Logging and tracing setup
└── routes/
├── mod.rs # Route module exports
├── health_check.rs # Health check handler
├── shorten.rs # URL shortening handler
└── redirect.rs # URL redirect handler
tests/
└── api/
├── main.rs # Integration test entry
├── helpers.rs # Test utilities and setup
├── health_check.rs # Health check tests
├── shorten.rs # URL shortening tests
└── redirect.rs # URL redirect tests
migrations/
├── 20250106035925_url-shortener.up.sql # Database schema
└── 20250106035925_url-shortener.down.sql # Rollback migration
The application uses environment-based configuration:
- Database: Automatically provisioned by Shuttle
- Logging: Configurable via
RUST_LOGenvironment variable - Port: Automatically assigned by Shuttle
CREATE TABLE urls (
id VARCHAR(6) PRIMARY KEY, -- Short identifier (nanoid)
url VARCHAR NOT NULL -- Original URL
);- Structured Logging: JSON-formatted logs with request correlation IDs
- Request Tracing: Full request lifecycle tracing
- Health Checks:
/health_checkendpoint for uptime monitoring - Error Handling: Comprehensive error responses with appropriate HTTP status codes
- Web UI with Tera templates
- User authentication and URL management
- Analytics and usage statistics
- Custom short URL aliases
- URL expiration and cleanup
- Rate limiting
- Status page with BetterStack integration
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the License.txt file for details.
Jeffery D. Mitchell
- Email: crusty.rustacean@gmail.com
- GitHub: @crustyrustacean
- Built with Shuttle - Amazing Rust deployment platform
- Inspired by the Shuttle URL Shortener Tutorial