This project is a URL Shortener, a simple tool written in Python. It is designed to convert long URLs into manageable short links that are easier to share and remember. The URL Shortener uses a unique encoding algorithm to generate short URLs and can decode them back to the original URL. The project is equipped with a comprehensive test suite ensuring its reliability and correctness.
The URL Shortener is a RESTful API written in Python. It uses the FastAPI framework to handle HTTP requests and responses. This application has been designed to be easily extensible and maintainable, and is divided in different layers, each one with a specific responsibility.
entrypoints: Currently it contains only one entrypoint: the FastAPI application. It handles HTTP requests and responses, and it is the only layer that is aware of the HTTP protocol.adapters: It contains the implementation of the application interfaces required to let the application interact with external systems. Currently it contains only the Redis adapter, which is used to store the URLs.application/ports: It contains the application interfaces, which are used to decouple the application from the external systems.application: It contains the business logic of the application, and it is isolated from external systems.
The bootstrap.py file is used to bootstrap the application, and it is responsible for the dependency injection.
The application is designed to be run as a standalone application or to be deployed in a container.
That offers the possibility to easily scale the application horizontally, by running multiple instances of the application behind a load balancer.
For that purpose, an example using kubernetes is provided in the k8s folder. It contains the kubernetes manifests to deploy the application in a kubernetes cluster, and a skaffold.yaml file to easily scaffold the application.
Install dependencies
poetry installStart redis server:
docker run -d --rm --name redis -p 6379:6379 redis:7.0-alpine redis-server --save 60 1Start the application:
poetry run gunicorn --chdir app -w 4 -k uvicorn.workers.UvicornWorker wsgi:api --bind 0.0.0.0:5001docker compose up -d --build --force-recreateRequirements:
Make sure you're in the right kubernetes context (normally it is automatically set after installing minikube or k3d):
kubectl config current-contextCreate the kubernetes cluster:
task k3d:create-clusterScaffold the application:
task skaffold:devUse the application to create a short url:
curl -X PUT 0.0.0.0:5001/encode --header "Content-Type: application/json" --data '{"url":"https://google.com"}'Try to decode the short url:
curl -X GET 0.0.0.0:5001/decode/nrfrgw