A robust, scalable URL shortener built with Python, FastAPI, and MongoDB, following a microservice architecture and containerized with Docker.
| Technology | Description |
|---|---|
| Python | Core programming language. |
| FastAPI | High-performance web framework for building the APIs. |
| MongoDB | NoSQL database for storing URL mappings. |
| Docker & Docker Compose | For containerizing and orchestrating the microservices. |
| Pydantic | For data validation and settings management. |
| ShortUUID | For generating short, unambiguous URL codes. |
- Microservice Architecture: The application is split into two distinct services for scalability and separation of concerns:
- API Service: Handles the creation and management of short URLs (
POST /shorten). - Redirect Service: Handles the high-throughput task of redirecting short URLs to their original destination (
GET /{short_code}).
- API Service: Handles the creation and management of short URLs (
- Containerized: Fully containerized with Docker, allowing for easy setup and consistent deployment across any environment.
- Scalable: The decoupled nature of the services means the redirect service can be scaled independently to handle high traffic loads.
- Industry-Standard Codebase: Follows best practices for project structure, configuration management, and API design.
Follow these instructions to get the project up and running locally or in a cloud environment like GitHub Codespaces.
- Docker and Docker Compose must be installed on your system.
-
Clone the repository:
git clone <your-repo-url> cd url-shortener
-
Build and run the services: From the root directory, run the following command. This will build the Docker images and start all three containers.
docker-compose up --build
The application is now running.
- The API Service is available at
http://localhost:8000 - The Redirect Service is available at
http://localhost:8001
- The API Service is available at
Send a POST request to the API service to create a new short URL.
- Endpoint:
POST /shorten - Port:
8000 - Body (JSON):
{ "url": "[https://www.google.com](https://www.google.com)" }
Example curl command:
curl -X POST -H "Content-Type: application/json" \
-d '{"url": "[https://www.google.com](https://www.google.com)"}' \
http://localhost:8000/shortenSuccessful Response (201 Created):
{
"short_url": "http://localhost:8001/AbC1dE2f"
}Take the short_url from the response and use it to be redirected.
- Endpoint:
GET /{short_code} - Port:
8001
Example:
Pasting http://localhost:8001/AbC1dE2f into your browser will redirect you to https://www.google.com.
The project follows a scalable structure with a clear separation of concerns for each service.
url-shortener/
├── api-service/ # Handles URL creation
│ ├── .env
│ ├── Dockerfile
│ ├── requirements.txt
│ └── app/
│ ├── config.py
│ ├── database.py
│ ├── main.py
│ ├── models.py
│ ├── routers/
│ └── schemas.py
│
├── redirect-service/ # Handles URL redirection
│ ├── .env
│ ├── Dockerfile
│ ├── requirements.txt
│ └── app/
│ └── ... (similar structure)
│
└── docker-compose.yml # Orchestrates all services