Skip to content

A scalable URL shortener built with Python and FastAPI, using a containerized microservice architecture with Docker and MongoDB.

Notifications You must be signed in to change notification settings

ByteMeShiv/url-shortener-microservice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Microservice-Based URL Shortener

A robust, scalable URL shortener built with Python, FastAPI, and MongoDB, following a microservice architecture and containerized with Docker.

Tech Stack

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.

Features

  • 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}).
  • 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.

Getting Started

Follow these instructions to get the project up and running locally or in a cloud environment like GitHub Codespaces.

### Prerequisites

  • Docker and Docker Compose must be installed on your system.

### Installation & Running

  1. Clone the repository:

    git clone <your-repo-url>
    cd url-shortener
  2. 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

Usage / API Endpoints

### 1. Create a Short URL

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/shorten

Successful Response (201 Created):

{
  "short_url": "http://localhost:8001/AbC1dE2f"
}

### 2. Use the Short URL

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.


Project Structure

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

About

A scalable URL shortener built with Python and FastAPI, using a containerized microservice architecture with Docker and MongoDB.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published