export COMPOSE_FILE=local.yml
docker compose build
docker compose up -d
docker compose run --rm django python manage.py migrate
We are using Postgres.
docker compose run --rm django python manage.py createsuperuser
Once created access Django admin:
There you can view the created Short URLs.
Once you are logged in with your superuser you can access Swagger and use the application.
There are three differnet endpoints:
Note: In swagger http://localhost:8000/api/docs/#/
Use the endpoint: /api/url-shortener/generate/
Send the payload:
{
"url": "https://en.wikipedia.org/wiki/Genghis_Khan"
}
Response:
{
"url": "https://en.wikipedia.org/wiki/Genghis_Khan",
"shortened": "1cad2db7-9e4e-459f-b535-9c979c8d768e"
}
It'll count +1 views each time you hit this endpoint
Note: In swagger: http://localhost:8000/api/docs/#/
Use the endpoint: /api/url-shortener/{shortened}/
e.g. /api/url-shortened/1cad2db7-9e4e-459f-b535-9c979c8d768e/
Fill the "shortened url" 1cad2db7-9e4e-459f-b535-9c979c8d768e in the shortened
field.
Response
{
"url": "https://en.wikipedia.org/wiki/Genghis_Khan"
}
The top 100 urls with more views.
You can configure this value via an environment variable:
TOP_N_URLS=100
Update the ./.envs/.local/.django
if you want.
Note: In Swagger http://localhost:8000/api/docs/#/
Use the endpoint /api/url-shortener/top/
Response
[
{
"title": "Genghis Khan - Wikipedia",
"views": 5
},
{
"title": "Jujutsu Kaisen - Wikipedia",
"views": 1
}
]
You can run the tests:
docker compose run --rm django pytest
Result:
Test session starts (platform: linux, Python 3.11.8, pytest 8.1.1, pytest-sugar 1.0.0)
django: version: 4.2.11, settings: config.settings.test (from option)
rootdir: /app
configfile: pyproject.toml
plugins: anyio-4.3.0, Faker-24.2.0, django-4.8.0, sugar-1.0.0
collected 38 items
shortener/urlshortener/tests/test_views.py ✓✓✓ 8% ▊
shortener/users/tests/test_admin.py ✓✓✓✓✓ 21% ██▏
shortener/users/tests/test_drf_urls.py ✓✓✓ 79% ███████▉
shortener/users/tests/test_drf_views.py ✓✓ 29% ██▉
shortener/users/tests/test_forms.py ✓ 32% ███▎
shortener/users/tests/test_models.py ✓ 34% ███▌
shortener/users/tests/test_swagger.py ✓✓✓ 42% ████▎
shortener/users/tests/test_tasks.py ✓ 45% ████▌
shortener/users/tests/test_urls.py ✓✓✓ 84% ████████▌
shortener/users/tests/test_views.py ✓✓✓✓✓✓ 63% ██████▍
shortener/urlshortener/tests/test_utils.py ✓✓✓✓ 74% ███████▍
tests/test_merge_production_dotenvs_in_dotenv.py ✓✓✓✓✓✓ 100% ██████████
Results (0.73s):
38 passed
- Django (with Django REST Framework)
- PostgreSQL
- Celery
- Redis
compose
: docker-related filesconfig
: django project configs, settings, urls, etcrequirements
: python dependendies per environmentshortener
: the project dir containing django appsurlshortener
: our main django app with exposing the shortener feature
If you check the docker compose file (local.yml
) you'll see those services:
django
: the main django app which expose the three endpoints to interact with the URL Shortener.postgres
: out databaseredis
: for message queueingceleryworker
: for async task processingflower
: dashboard to see the Celery tasks
The Django app and the Celery workers are independent so they can be scaling independently based on needs.
- More tests for Celery Tasks and Views
- Use a better URL shortener algorithm