How dev-aayushvats/url-shortener works
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 Database layer · app.py FastAPI app · docker-compose.yml Docker Compose · index.html Browser UI
Components 14
FastAPI service
- Browser UI
index.html - FastAPI app · Defines explicit routes before the short-code catch-all.
app.py - GET /
app.py - POST /shorten
app.py - GET /{short_code} · Resolves mappings and returns HTTP 302.
app.py - GET /database/all · Operational read of all stored mappings.
app.py - URL mapping model · Short code, target URL, and creation timestamp.
models.py - Database layer · PostgreSQL connection, sessions, and mapping access.
database.py - Python dependencies · Python 3.8+ FastAPI runtime dependencies.
requirements.txt
Runtime dependencies
- PostgreSQL · Source of truth for URL mappings.
- Redis · Disposable cache with 24-hour mapping expiry.
- Environment config · BASE_URL, DATABASE_URL, and REDIS_URL.
- Docker Compose · Runs local PostgreSQL and Redis dependencies.
docker-compose.yml
Other components
- Browser
Connections 21
- 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