Skip to content

🐍 A URL shortener application that follows the principles of Clean Architecture. This project provides a simple and efficient way to shorten long URLs, making them easier to share and manage. It is designed for scalability and maintainability, ensuring a robust codebase that adheres to best practices.

License

Notifications You must be signed in to change notification settings

fabianfalon/url-shortener-clean-architecture

Repository files navigation

πŸ”— URL Shortener with Clean Architecture & DDD

A ✨ simple, elegant URL shortener application built with FastAPI, following Clean Architecture and Domain-Driven Design (DDD) principles. This project demonstrates how to build a maintainable and scalable architecture while keeping things lightweight and developer-friendly.


πŸ“š Table of Contents


πŸš€ Features

  • πŸ”— Shorten long URLs for easier sharing.
  • πŸ“₯ Retrieve original URLs from short codes.
  • 🧱 Clean and modular architecture using DDD.
  • ⚑ Built on FastAPI for speed and simplicity.
  • βœ… Unit-tested for reliability.
  • 🧩 Domain Events & Value Objects support.

πŸ—οΈ Architecture

The app follows Clean Architecture, dividing responsibilities across layers for separation of concerns:

  • πŸ›£οΈ Delivery Layer: Handles HTTP requests with FastAPI.
  • 🧠 Domain Layer: Business logic, entities, value objects, and domain events.
  • βš™οΈ Application Layer: Use cases and orchestrators.
  • πŸ—„οΈ Infrastructure Layer: DB access and external APIs.

🧠 Domain-Driven Design

🧩 Aggregates

  • UrlAggregate: Manages URL lifecycle.
    • πŸ”Ή Root: Url
    • πŸ”Ή Value Objects: ShortCode, OriginalUrl

πŸ§ͺ Value Objects

  • ShortCode: Shortened URL string
  • OriginalUrl: Validated original URL

πŸ›ŽοΈ Domain Events

  • UrlShortenedEvent: Fired when a URL is shortened.
  • UrlAccessedEvent: Triggered when a URL is accessed.

πŸ“¦ Bounded Contexts

  • URL Management (current context).

πŸ“ Project Structure

src/
β”œβ”€β”€ domain/           # Core business logic
β”‚   β”œβ”€β”€ exceptions/
β”‚   β”œβ”€β”€ value_objects.py
β”‚   β”œβ”€β”€ url.py
β”‚   β”œβ”€β”€ url_repository.py
β”‚   └── events.py
β”œβ”€β”€ application/      # Use cases & services
β”‚   β”œβ”€β”€ create_short_url.py
β”‚   β”œβ”€β”€ get_original_url.py
β”‚   β”œβ”€β”€ get_url_stats.py
β”‚   └── get_all_urls.py
β”œβ”€β”€ infrastructure/   # DB and external service handling
β”‚   β”œβ”€β”€ dto/
β”‚   β”œβ”€β”€ storage/
β”‚   β”œβ”€β”€ shortener/
β”‚   └── events/
β”œβ”€β”€ delivery/         # API layer with FastAPI
β”‚   └── api/
β”‚       β”œβ”€β”€ dependencies.py
β”‚       └── routers.py
β”œβ”€β”€ config.py        # Centralized configuration
└── main.py          # Main entry point

βš™οΈ Installation

πŸ–₯️ Local Setup

  1. Clone the repository:
git clone https://github.com/fabianfalon/url-shortener-clean-architecture.git
cd url-shortener-clean-architecture
  1. Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate
  1. Install dependencies:
pip install -r requirements.txt
# or for dev and test support
pip install -r requirements-tests.txt
  1. Run the app:
uvicorn app.main:app --reload

🐳 Docker Setup

docker-compose build
docker-compose up

🌐 API Endpoints

Method Endpoint Description
POST /shorten Shorten a long URL
GET /urls List all shortened URLs
GET /{shortened_id} Retrieve the original URL
GET /{short_code}/stats Get URL statistics

πŸ” Example Requests

βž• Shorten a URL

POST /shorten
Content-Type: application/json

{
  "url": "https://example.com/some/long/url"
}

πŸ“‹ Get All Short URLs

GET /urls
Content-Type: application/json

{
  "urls": [
    {
      "id": 1,
      "short_url": "abc123"
    }
  ]
}

πŸ” Retrieve Original URL

GET /abc123
Content-Type: application/json

🀝 Contributing

Contributions are welcome! πŸ™Œ Feel free to fork the repo and submit a pull request with improvements or bug fixes.


πŸ“„ License

This project is licensed under the MIT License. See the LICENSE file for more details.

About

🐍 A URL shortener application that follows the principles of Clean Architecture. This project provides a simple and efficient way to shorten long URLs, making them easier to share and manage. It is designed for scalability and maintainability, ensuring a robust codebase that adheres to best practices.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published