# dev-aayushvats/url-shortener architecture

> Architecture diagram of the GitHub repository dev-aayushvats/url-shortener, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dev-aayushvats/url-shortener
- Repository: https://github.com/dev-aayushvats/url-shortener

## Overview

#### Purpose and Entry Points
This is a FastAPI URL-shortener service with a small browser UI. It creates persistent short-code-to-target-URL mappings and resolves short codes through an HTTP redirect.

Primary entry points:
- `app.py` — FastAPI application, HTTP routes, URL creation and redirect flow.
- `index.html` — browser interface served from the root route.
- `docker-compose.yml` — local PostgreSQL and Redis runtime dependencies.

#### HTTP/API Boundary
`app.py` exposes four externally visible flows:
- `GET /` serves `index.html`.
- `POST /shorten` accepts a target URL and returns its generated short code and absolute short URL.
- `GET /{short_code}` resolves the mapping and returns an HTTP 302 redirect.
- `GET /database/all` exposes all persisted mappings.

The redirect catch-all must remain ordered/defined so explicit routes such as `/shorten` and `/database/all` are not treated as short codes.

#### URL Creation Stage
The shortening path accepts a validated request model, generates a unique short identifier, persists the mapping, and builds the returned URL using the configured base URL.
- `app.py`
- `models.py`
- `database.py`

The persistent record aligns with the documented `urls` schema: short-code primary key, target URL, and creation timestamp.

#### Resolution and Cache Stage
Redirect resolution should use Redis as the fast lookup layer before querying PostgreSQL. On a cache miss, the service reads the durable mapping from the database, populates Redis, and redirects. Cached mappings expire after 24 hours.
- `app.py`
- `database.py`

This establishes Redis as disposable acceleration and PostgreSQL as the source of truth.

#### Persistence Boundary
Database integration is isolated from HTTP routing through the database/model layer:
- `database.py` — PostgreSQL connection/session and database access setup.
- `models.py` — URL mapping schema/model definitions.
- `app.py` — consumes persistence operations to implement endpoint behavior.

The `/database/all` endpoint crosses this boundary to return the complete stored dataset, making it an operational/debug-style read API rather than part of redirect hot-path behavior.

#### Runtime and Infrastructure
The application runs on Python 3.8+ with FastAPI and dependencies declared in:
- `requirements.txt`

Local infrastructure consists of:
- PostgreSQL for durable URL mappings.
- Redis for expiring resolution cache.
- Docker Compose orchestration in `docker-compose.yml`.

Runtime configuration is environment-driven: `BASE_URL` determines emitted short URLs, while `DATABASE_URL` and `REDIS_URL` define external service connectivity.

Main entry points: [`database.py`](https://github.com/dev-aayushvats/url-shortener/blob/main/database.py) (Database layer), [`app.py`](https://github.com/dev-aayushvats/url-shortener/blob/main/app.py) (FastAPI app), [`docker-compose.yml`](https://github.com/dev-aayushvats/url-shortener/blob/main/docker-compose.yml) (Docker Compose), [`index.html`](https://github.com/dev-aayushvats/url-shortener/blob/main/index.html) (Browser UI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["FastAPI service"]
  node_ui["Browser UI<br/>HTML page<br/>[index.html]"]
  node_api["FastAPI app<br/>HTTP routing<br/>[app.py]"]
  node_root["GET /<br/>page route<br/>[app.py]"]
  node_shorten["POST /shorten<br/>creation route<br/>[app.py]"]
  node_redirect["GET /&#123;short_code&#125;<br/>redirect route<br/>[app.py]"]
  node_all_mappings["GET /database/all<br/>read route<br/>[app.py]"]
  node_url_model["URL mapping model<br/>schema/model<br/>[models.py]"]
  node_database_layer["Database layer<br/>persistence access<br/>[database.py]"]
  node_requirements["Python dependencies<br/>package manifest<br/>[requirements.txt]"]
end

subgraph group_infra["Runtime dependencies"]
  node_postgres[("PostgreSQL<br/>durable store")]
  node_redis[("Redis<br/>resolution cache")]
  node_runtime_config["Environment config<br/>runtime settings"]
  node_compose["Docker Compose<br/>local orchestration<br/>[docker-compose.yml]"]
end

node_browser(("Browser<br/>client"))

node_browser -->|"HTTP requests"| node_api
node_api -->|"handles"| node_root
node_api -->|"handles"| node_shorten
node_api -->|"handles last"| node_redirect
node_api -->|"handles"| node_all_mappings
node_root -->|"serves"| node_ui
node_ui -->|"submits URL"| node_shorten
node_shorten -->|"validates and creates mapping"| node_url_model
node_shorten -->|"persists mapping"| node_database_layer
node_shorten -.->|"uses BASE_URL"| node_runtime_config
node_redirect -->|"lookup first"| node_redis
node_redis -.->|"cache miss"| node_database_layer
node_database_layer -->|"read/write mappings"| node_postgres
node_database_layer -.->|"populate after miss"| node_redis
node_all_mappings -->|"lists mappings"| node_database_layer
node_database_layer -->|"maps records"| node_url_model
node_runtime_config -.->|"DATABASE_URL"| node_database_layer
node_runtime_config -.->|"REDIS_URL"| node_redis
node_requirements -.->|"provides runtime packages"| node_api
node_compose -->|"orchestrates"| node_postgres
node_compose -->|"orchestrates"| node_redis

click node_ui "https://github.com/dev-aayushvats/url-shortener/blob/main/index.html"
click node_api "https://github.com/dev-aayushvats/url-shortener/blob/main/app.py"
click node_root "https://github.com/dev-aayushvats/url-shortener/blob/main/app.py"
click node_shorten "https://github.com/dev-aayushvats/url-shortener/blob/main/app.py"
click node_redirect "https://github.com/dev-aayushvats/url-shortener/blob/main/app.py"
click node_all_mappings "https://github.com/dev-aayushvats/url-shortener/blob/main/app.py"
click node_url_model "https://github.com/dev-aayushvats/url-shortener/blob/main/models.py"
click node_database_layer "https://github.com/dev-aayushvats/url-shortener/blob/main/database.py"
click node_requirements "https://github.com/dev-aayushvats/url-shortener/blob/main/requirements.txt"
click node_compose "https://github.com/dev-aayushvats/url-shortener/blob/main/docker-compose.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_ui,node_api,node_root,node_shorten,node_redirect,node_all_mappings,node_url_model,node_database_layer,node_requirements toneBlue
class node_postgres,node_redis,node_runtime_config,node_compose toneAmber
class node_browser toneNeutral
```

## Components

### FastAPI service

- [Browser UI](https://github.com/dev-aayushvats/url-shortener/blob/main/index.html): HTML page · `index.html`
- [FastAPI app](https://github.com/dev-aayushvats/url-shortener/blob/main/app.py): HTTP routing · Defines explicit routes before the short-code catch-all. · `app.py`
- [GET /](https://github.com/dev-aayushvats/url-shortener/blob/main/app.py): page route · `app.py`
- [POST /shorten](https://github.com/dev-aayushvats/url-shortener/blob/main/app.py): creation route · `app.py`
- [GET /{short_code}](https://github.com/dev-aayushvats/url-shortener/blob/main/app.py): redirect route · Resolves mappings and returns HTTP 302. · `app.py`
- [GET /database/all](https://github.com/dev-aayushvats/url-shortener/blob/main/app.py): read route · Operational read of all stored mappings. · `app.py`
- [URL mapping model](https://github.com/dev-aayushvats/url-shortener/blob/main/models.py): schema/model · Short code, target URL, and creation timestamp. · `models.py`
- [Database layer](https://github.com/dev-aayushvats/url-shortener/blob/main/database.py): persistence access · PostgreSQL connection, sessions, and mapping access. · `database.py`
- [Python dependencies](https://github.com/dev-aayushvats/url-shortener/blob/main/requirements.txt): package manifest · Python 3.8+ FastAPI runtime dependencies. · `requirements.txt`

### Runtime dependencies

- **PostgreSQL**: durable store · Source of truth for URL mappings.
- **Redis**: resolution cache · Disposable cache with 24-hour mapping expiry.
- **Environment config**: runtime settings · BASE_URL, DATABASE_URL, and REDIS_URL.
- [Docker Compose](https://github.com/dev-aayushvats/url-shortener/blob/main/docker-compose.yml): local orchestration · Runs local PostgreSQL and Redis dependencies. · `docker-compose.yml`

### Other components

- **Browser**: client

## Connections

- Browser → FastAPI app: HTTP requests
- FastAPI app → GET /: handles
- FastAPI app → POST /shorten: handles
- FastAPI app → GET /{short_code}: handles last
- FastAPI app → GET /database/all: handles
- GET / → Browser UI: serves
- Browser UI → POST /shorten: submits URL
- POST /shorten → URL mapping model: validates and creates mapping
- POST /shorten → Database layer: persists mapping
- POST /shorten → Environment config: uses BASE_URL
- GET /{short_code} → Redis: lookup first
- Redis → Database layer: cache miss
- Database layer → PostgreSQL: read/write mappings
- Database layer → Redis: populate after miss
- GET /database/all → Database layer: lists mappings
- Database layer → URL mapping model: maps records
- Environment config → Database layer: DATABASE_URL
- Environment config → Redis: REDIS_URL
- Python dependencies → FastAPI app: provides runtime packages
- Docker Compose → PostgreSQL: orchestrates
- Docker Compose → Redis: orchestrates
