Source Code: https://github.com/juanesquintero/url-shortener
This project scaffolding for FastAPI framework with Python 3.11 contains:
- Development Guidelines
- Clean folder tree
- FastAPI advanced config & features
- Docker containerization
- SQL database connection
- Testing
- Linting
All commands in this project must be run on a BASH type terminal. (You can use git-bash in windows)
Docker
https://www.docker.com/resources/what-container
https://www.docker.com/get-started
TechStack
Python 3.11
https://www.python.org/
FastAPI 0.105.0
https://fastapi.tiangolo.com/
PostgreSQL 16
https://www.postgresql.org/
Clone this repo
Create .env file based on the .env.template.
ADMIN_EMAIL[email protected]
DB_PASSWORD=admin123*
Containerized
After install Docker Desktop, create the images & instance the containers.
$ docker-compose up
FastAPI app now is running on http://localhost:8000
PostgreSQL database on localhost:5432
pgAdmin GUI on localhost:8080
The database will be created automatically with docker volumes using the following file db/sql/creates.sql and the inserts.sql will populate it with initial test data.
Changes
If some DDL changes are included to the database is preferred to remove the db container & the data folder (postgres_data)
Run tests
$ docker exec url-shortener-api pytest -v tests
Run tests & generate html report
$ docker exec url-shortener-api pytest -v --html=tests/report.html --self-contained-html tests
Run coverage w/ html Report
$ docker exec url-shortener-api pytest --cov=app --cov-report html:tests/coverage --cov-report term-missing
Now go to http://localhost:8000/docs
You will see the automatic interactive API documentation (provided by Swagger UI):
Will list the all urls shortened inserted on the db automatically Also it has 2 query params to get one specific url by original url address or the shorten one
?short_url=
string (query)
?original_url=
string (query)
Will list the urls more accesed more clicks in descending order and following th limit given on the query params
?limit=100
integer (query)
Will post and create the new url with the shorted version of the url using the selected algorithm "Base Conversion Algorithm" for resulition based on PK autoincrement
Base Conversion Algorithm Convert the unique ID of the long URL into a shorter representation using base conversion techniques. For example, you can convert the decimal representation of the ID into a base58 or base62 encoding, excluding easily confused characters like ‘0’, ‘O’, ‘1’, ‘I’, etc.
?original_url=
integer (string)
This project is licensed under the terms of the MIT license.