A scalable URL shortener service built with FastAPI, featuring real-time analytics, containerization with Docker, CI/CD pipeline with GitHub Actions, and deployment to Google Cloud Run.
Service URL: https://url-shortener-939986137088.us-central1.run.app
- API Documentation: https://url-shortener-939986137088.us-central1.run.app/docs
- Health Check: https://url-shortener-939986137088.us-central1.run.app/health
- RESTful API for URL shortening and analytics
- Real-time analytics via WebSocket connections
- Scalable containerized deployment
- Automated CI/CD pipeline
- Production-ready cloud infrastructure
- Comprehensive test coverage
- Security best practices
| Method | Endpoint | Description |
|---|---|---|
POST |
/shorten |
Create a shortened URL |
GET |
/{short_code} |
Redirect to original URL |
GET |
/analytics/{short_code} |
Get analytics for short URL |
WS |
/ws/analytics/{short_code} |
Real-time analytics updates |
GET |
/health |
Health check endpoint |
# Install dependencies
pip install -r requirements.txt
# Run the application
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
# Access the service
# API: http://localhost:8000
# Documentation: http://localhost:8000/docs# Build the image
docker build -t url-shortener .
# Run the container
docker run -p 8000:8000 url-shortener# Execute test suite
python windows_test.py
# Run with coverage
pytest --cov=main --cov-report=htmlcurl -X POST "https://url-shortener-939986137088.us-central1.run.app/shorten" \
-H "Content-Type: application/json" \
-d '{"url": "https://www.example.com"}'Response:
{
"short_code": "abc123",
"shortened_url": "https://url-shortener-939986137088.us-central1.run.app/abc123",
"original_url": "https://www.example.com"
}curl -L "https://url-shortener-939986137088.us-central1.run.app/abc123"
# Redirects to https://www.example.comcurl "https://url-shortener-939986137088.us-central1.run.app/analytics/abc123"Response:
{
"short_code": "abc123",
"original_url": "https://www.example.com",
"redirect_count": 5,
"created_at": "2024-01-15T10:30:00.000Z"
}# Using CLI client
python websocket_client.py abc123 --url wss://url-shortener-939986137088.us-central1.run.app
# Create and monitor new URL
python websocket_client.py new_code --create "https://example.com" --url wss://url-shortener-939986137088.us-central1.run.app┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Client/Web │────│ Load Balancer │────│ Cloud Run │
│ Browser │ │ │ │ (FastAPI) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│
┌─────────────────┐ │
│ Database │─────────────┤
│ (SQLite) │ │
└─────────────────┘ │
│
┌─────────────────┐ ┌─────────────────┐ ┌───────▼─────────┐
│ Monitoring & │────│ GitHub Actions │────│ Container │
│ Logging │ │ CI/CD │ │ Registry │
└─────────────────┘ └─────────────────┘ └─────────────────┘
- FastAPI: High-performance web framework with automatic API documentation
- SQLAlchemy: Database ORM with connection pooling
- WebSocket: Real-time bidirectional communication
- Cloud Run: Serverless container platform with automatic scaling
- Google Container Registry: Container image storage
- GitHub Actions: Automated CI/CD pipeline
The automated pipeline includes:
- Code Quality: Linting with flake8, formatting with black, import sorting with isort
- Testing: Unit tests with pytest, coverage reporting
- Build: Docker image creation and optimization
- Deploy: Automated deployment to Google Cloud Run
- Verify: Post-deployment health checks
- Trigger: Push to main branch or pull request
- Test Environment: Ubuntu latest with Python 3.11
- Container Registry: Google Container Registry (GCR)
- Deployment Target: Google Cloud Run (us-central1)
- Project ID: url-shortener-20250527232607
- Region: us-central1
- Compute: Cloud Run (serverless containers)
- Storage: SQLite (lightweight, embedded database)
- Networking: HTTPS-only with automatic SSL
- Container Security: Non-root user execution
- IAM: Least-privilege service account permissions
- Network: HTTPS enforcement, no public database access
- Authentication: Service-to-service authentication with Google Cloud
- Input Validation: Pydantic models for request validation
- Auto-scaling: 0 to 10 instances based on demand
- Resource Limits: 512Mi memory, 1 CPU per instance
- Concurrency: 100 concurrent requests per instance
- Timeout: 300 seconds per request
├── main.py # FastAPI application
├── requirements.txt # Python dependencies
├── Dockerfile # Container configuration
├── windows_test.py # Test suite
├── websocket_client.html # WebSocket browser client
├── websocket_client.py # WebSocket CLI client
├── .github/workflows/ci-cd.yml # CI/CD pipeline
└── README.md # Documentation
- Linting: flake8 with extended ignore rules
- Formatting: black for consistent code style
- Import Management: isort for organized imports
- Testing: pytest with coverage reporting
- Type Hints: Python type annotations where applicable
- Create feature branch from main
- Implement functionality with tests
- Run local quality checks and tests
- Create pull request
- Automated CI/CD pipeline validation
- Code review and merge
- Automatic deployment to production
- Unit Tests: Core business logic validation
- Integration Tests: API endpoint testing
- WebSocket Tests: Real-time functionality validation
- Error Handling: Edge case and failure scenario testing
# Run all tests
python windows_test.py
# Run specific test categories
pytest -k "test_shorten_url"
# Generate coverage report
pytest --cov=main --cov-report=htmlTotal Development Time: 9.5 hours
| Component | Time Allocation |
|---|---|
| Backend Development | 3 hours |
| Testing Implementation | 3 hours |
| Containerization | 1 hours |
| CI/CD Pipeline | 1 hours |
| Infrastructure Setup | 1 hours |
| Documentation | 0.5 hours |
FastAPI over Django: Chosen for superior API performance, built-in async support, automatic OpenAPI documentation, and modern Python features.
SQLite over PostgreSQL: Selected for simplicity and deployment efficiency. Demonstrates all required functionality without database connection complexity. Production deployment would use Cloud SQL PostgreSQL.
Cloud Run over GKE: Serverless approach eliminates infrastructure management overhead, provides automatic scaling, and offers cost-effective pay-per-use pricing model.
Google Container Registry over Artifact Registry: Established workflow with fewer permission complexities for initial deployment. Artifact Registry recommended for production environments.
- Database: Migrate to Cloud SQL PostgreSQL with read replicas
- Caching: Implement Redis for frequently accessed URLs
- CDN: Add CloudFlare or Google CDN for global performance
- Load Balancing: Configure advanced load balancing strategies
- Rate Limiting: Implement API rate limiting and abuse prevention
- Authentication: Add user accounts and API key management
- Monitoring: Enhanced security monitoring and alerting
- Audit Logging: Comprehensive access and modification logging
- Monitoring: Custom metrics, dashboards, and alerting
- Backup Strategy: Automated database backups and recovery procedures
- Disaster Recovery: Multi-region deployment and failover mechanisms
- Performance Optimization: Database indexing and query optimization
- RESTful API with all specified endpoints
- Python with FastAPI framework
- Database integration with proper ORM
- Comprehensive unit testing
- Robust error handling for edge cases
- Optimized Dockerfile with multi-stage builds
- Security-hardened container (non-root user)
- Health check implementation
- Efficient layer caching
- GitHub Actions workflow automation
- Code quality enforcement (linting, formatting)
- Automated testing execution
- Container building and registry pushing
- Google Cloud Run deployment
- IAM configuration with least-privilege principles
- Service-to-service authentication
- Environment variable management
- Monitoring and logging integration
- Real-time analytics implementation
- Multiple concurrent connection support
- Client-side examples (CLI and web)
- Connection lifecycle management
- Heartbeat and cleanup mechanisms
- Health Checks: Automated endpoint monitoring
- Performance Metrics: Response time and throughput tracking
- Error Tracking: Exception monitoring and alerting
- Resource Usage: CPU, memory, and scaling metrics
Common issues and solutions documented in operational runbooks. Comprehensive logging enables rapid issue identification and resolution.
Rolling deployments ensure zero-downtime updates. Feature flags enable safe rollout of new functionality.
Built with Python, FastAPI, Docker, and Google Cloud Platform