Giter Club home page Giter Club logo

url-shortener's Introduction

shortener

Start project locally

Run the project with Docker

export COMPOSE_FILE=local.yml
docker compose build
docker compose up -d

Apply migrations

docker compose run --rm django python manage.py migrate

We are using Postgres.

Create a superuser to access swagger and the django-admin

docker compose run --rm django python manage.py createsuperuser

Once created access Django admin:

http://localhost:8000/admin/

There you can view the created Short URLs.

Use the application

Once you are logged in with your superuser you can access Swagger and use the application.

There are three differnet endpoints:

1. Generate a shortened URL

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"
}

2. Get the real URL from a shortened URL

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"
}

3. Get the top N URL

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
  }
]

Tests

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

Project Structure

Stack

  • Django (with Django REST Framework)
  • PostgreSQL
  • Celery
  • Redis

Directories

  • compose: docker-related files
  • config: django project configs, settings, urls, etc
  • requirements: python dependendies per environment
  • shortener: the project dir containing django apps
    • urlshortener: our main django app with exposing the shortener feature

Docker services

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 database
  • redis: for message queueing
  • celeryworker: for async task processing
  • flower: dashboard to see the Celery tasks

The Django app and the Celery workers are independent so they can be scaling independently based on needs.

Next steps

  • More tests for Celery Tasks and Views
  • Use a better URL shortener algorithm

url-shortener's People

Contributors

omiguelperez avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.