Skip to content

alimz/url-shortener

Repository files navigation

URL Shortener

A production-ready URL shortening service built with FastAPI, PostgreSQL, and Redis.
Designed with best practices for scalability, performance, and security in mind.

Features

  • Shorten URLs with unique 5-character Base62 codes
  • Fast redirects with Redis positive and negative caching
  • Rate limiting on URL creation to prevent abuse
  • Database migrations with Alembic (automatically run on container startup)
  • Full Docker Compose setup (API, PostgreSQL, Redis)
  • End-to-End tests with Pytest and Httpx
  • Clean and modular project structure

Setup

Before running the containers, create a .env file by copying the example:

cp .env.example .env

This file contains configuration values for the database, Redis, cache, and rate limiting.

Run with Docker

docker-compose up --build

The API will be available at: http://localhost:5050/docs

Database migrations will be executed automatically when the API container starts.

Tests

docker-compose run api pytest -v

Usage

Once the containers are up, the API will be available at http://localhost:5050.

Shorten a URL

Send a POST request to create a shortened link:

curl -X POST "http://localhost:5050/shorten" \
     -H "Content-Type: application/json" \
     -d '{"target_url": "https://example.com"}'

Response:

{
  "code": "abc12",
  "target_url": "https://example.com"
}

Redirect:

Open the shortened link in your browser or via curl:

curl -i http://localhost:5050/abc12

You will be redirected (HTTP 307) to the original target_url.

API Documentation

After starting the application, you can access the interactive API documentation:

Project Structure

app/
  ├── main.py          # FastAPI application entrypoint
  ├── models.py        # SQLAlchemy models
  ├── schemas.py       # Pydantic schemas
  ├── crud.py          # Database operations
  ├── cache.py         # Redis connection
  ├── rate_limiter.py  # Request throttling logic
  └── database.py      # DB engine & session
migrations/            # Alembic migrations
tests/                 # Pytest test suite
docker-compose.yml

Author

Developed by Ali Mohammadi

About

Production-ready URL Shortener built with FastAPI, PostgreSQL, Redis, and Docker

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published