A functional, asynchronous URL shortening service built as a learning project to explore modern backend infrastructure, containerization, and monitoring.
Instead of just building a basic CRUD app, this project focuses on production-like practices:
- Asynchronous API: Built with FastAPI and
asyncpgfor non-blocking database operations. - Caching & Rate Limiting: Uses Redis to cache hot links and protect the API from spam (10 req/min per IP).
- Containerization: Packaged using multi-stage Docker builds to keep the image lightweight and secure.
- Observability: Integrated Prometheus to scrape internal metrics and Grafana to visualize traffic and latency.
- Backend: Python 3.13+, FastAPI, Uvicorn
- Data: PostgreSQL, Redis
- DevOps & Monitoring: Docker, Nginx, Prometheus, Grafana
-
Clone the repository:
git clone https://github.com/Barden-dev/URLShortener.git cd URLShortener -
Environment Variables: Rename
.env.exampleto.envand fill in your local credentials for Postgres and Redis. By default the domain islocalhost, you can change it for example toexample.com -
Run with Docker:
docker compose up -d --build
- The API will be available at
http://localhost:8080. The Grafana dashboard is routed through Nginx at/grafana/.
Full interactive documentation (Swagger UI) is automatically generated and available at /docs once the application is running. Here is a quick overview of the main endpoints:
POST /shorten
Expects a JSON payload with the destination URL.
-
Request:
{"target_url": "https://www.google.com"} -
Response:
{"target_url": "https://www.google.com/", "secret_key": "XyZ123ab", "is_active": true, "clicks": 0}
GET /{secret_key}
- Navigating to this URL performs an HTTP redirect to the original destination.
- Note: The click counter is incremented asynchronously in the background so the redirect happens instantly.
GET /stats/{secret_key}
Returns the original URL and the total number of times the short link was accessed.
- Response:
{"target_url": "https://www.google.com/", "clicks": 42}