项目作者: adamwojt

项目描述 :
The ultimate Django / Postgres / Redis url shortner .
高级语言: Python
项目地址: git://github.com/adamwojt/ur_l.git
创建时间: 2020-08-05T23:34:29Z
项目社区:https://github.com/adamwojt/ur_l

开源协议:Other

下载


CIBandit

Imports: isortCode style: blackCoverageSafety

UR_L shortener

Requirements

  • Docker >= 17.05
  • docker-compose >= 1.21.0
  • Python >= 3.8
  • Poetry

NOTE - Run all commands from the project root

Configuration

Most of the configuration is done via Environment variables that are passed to docker-compose (docs).
You can either set them up in your shell or create .env in project root.
See .env.template file for possible options and defaults.

NOTE - Set at least:

  1. DJANGO_SECRET_KEY
  2. TARGET_ENV (development|production)

API

Interactive docs are available under /api/docs

how it works

Once Url is created (via api or admin) - it will redirect permanently from /{token} path to original long_url.
URL can stay in high demand as backend will constantly refresh Redis cache to CACHE_TIMEOUT_READ settings.
Cache is set at create too with CACHE_TIMEOUT_READ setting. Cache is updated/deleted on PUT or DELETE.

list

  1. GET /api/urls/
  2. Auth method: Basic
  3. Return a list of all the existing urls.
  4. Query Parameters
  5. The following parameters should be included as part of a URL query string.
  6. page A page number within the paginated result set.

create

  1. POST /api/urls/
  2. Auth method: None (Public)
  3. Create a new short url.
  4. Request Body
  5. The request body should be a "application/json" encoded object, containing the following items.
  6. long_url (required)
  7. click_limit (optional, if set url is deleted after limit is reached)

read

  1. GET /api/urls/{token}/
  2. Auth method: None (Public)
  3. Return the given url using token.
  4. Path Parameters
  5. The following parameters should be included in the URL path.
  6. token required

update

  1. PUT /api/urls/{token}/
  2. Auth method: Basic
  3. Update long url (long_url body needed)
  4. The following parameters should be included in the URL path.
  5. token (required)
  6. The request body should be a "application/json" encoded object, containing the following items.
  7. long_url (required)
  8. click_limit

delete

  1. DELETE /api/urls/{token}/
  2. Auth method: Basic
  3. Delete url and clear cache
  4. The following parameters should be included in the URL path.
  5. token (required)

admin
There is admin panel available too under /admin. Make sure to createsuperuser before.


Local development

With TARGET_ENV=development -> docker-compose up. Access in browser -> localhost:8001
This loads docker-compose.override.yml and mounts app directory to container.
Any code changes will restart server dynamically.

Commands:

  • Run development server - docker-compose up
  • Run development server (with stdout for debug) - docker-compose run -p 8001:8001 ur_l
  • To run local dir tests - docker-compose run ur_l test
  • To run local dir linter docker-compose run ur_l lint_mounted_dir
  • Access to manage.py - docker-compose run manage {args}

NOTES:

  • First run will need docker-compose run ur_l manage migrate
  • Nginx is not running in dev but it’s port (8000) is reserved.
  • Redis cache is dev should be persistent - to disable, remove redis volume in docker-compose.override.yml

Production

With TARGET_ENV=production -> docker-compose -f docker-compose.yml up. Access in browser -> localhost:8000
Local code changes will not be visible.

Commands:

  • Run server - docker-compose -f docker-compose.yml up
  • To run tests - docker-compose -f docker-compose.yml run ur_l test
  • Access to manage.py - docker-compose -f docker-compose.yml run ur_l manage {args}

NOTES:

  • Local code changes will not be visible after image is built.

Poetry

If any changes to dependencies, run below before rebuilding docker images:

  1. poetry update

See the poetry docs for information on how to add/update dependencies.


Docker

The Dockerfile uses multi-stage builds to run lint before building the production stage. If linting fails the build will fail too.

Linters that run on build:

Same commands as in Dockerfile can be run in poetry shell

NOTES:


Benchmarks

To run, execute below with whole stack running:
docker exec `docker ps -qf "name=ur_l_nginx"` benchmark

Fetching url - Mean time 4.004 ms:

  1. Server Software: nginx/1.18.0
  2. Server Hostname: localhost
  3. Server Port: 80
  4. Document Path: /Lq9uJfc
  5. Concurrency Level: 10
  6. Time taken for tests: 20.020 seconds
  7. Complete requests: 5000
  8. Failed requests: 0
  9. Non-2xx responses: 5000
  10. Keep-Alive requests: 4954
  11. Total transferred: 2034770 bytes
  12. HTML transferred: 0 bytes
  13. Requests per second: 249.75 [#/sec] (mean)
  14. Time per request: 40.040 [ms] (mean)
  15. Time per request: 4.004 [ms] (mean, across all concurrent requests)
  16. Transfer rate: 99.25 [Kbytes/sec] received
  17. Connection Times (ms)
  18. min mean[+/-sd] median max
  19. Connect: 0 0 0.0 0 0
  20. Processing: 11 40 8.5 39 243
  21. Waiting: 11 40 8.5 39 243
  22. Total: 11 40 8.5 39 244
  23. Percentage of the requests served within a certain time (ms)
  24. 50% 39
  25. 66% 41
  26. 75% 42
  27. 80% 43
  28. 90% 47
  29. 95% 52
  30. 98% 60
  31. 99% 71
  32. 100% 244 (longest request)

Creating url - Mean time 3.987 ms:

  1. Server Software: nginx/1.18.0
  2. Server Hostname: localhost
  3. Server Port: 80
  4. Document Path: /api/urls/
  5. Document Length: 102 bytes
  6. Concurrency Level: 10
  7. Time taken for tests: 19.935 seconds
  8. Complete requests: 5000
  9. Failed requests: 0
  10. Total transferred: 1990000 bytes
  11. Total body sent: 845000
  12. HTML transferred: 510000 bytes
  13. Requests per second: 250.81 [#/sec] (mean)
  14. Time per request: 39.871 [ms] (mean)
  15. Time per request: 3.987 [ms] (mean, across all concurrent requests)
  16. Transfer rate: 97.48 [Kbytes/sec] received
  17. 41.39 kb/s sent
  18. 138.88 kb/s total
  19. Connection Times (ms)
  20. min mean[+/-sd] median max
  21. Connect: 0 0 0.1 0 3
  22. Processing: 10 40 5.8 39 160
  23. Waiting: 10 39 5.8 39 160
  24. Total: 11 40 5.9 39 160
  25. Percentage of the requests served within a certain time (ms)
  26. 50% 39
  27. 66% 41
  28. 75% 42
  29. 80% 43
  30. 90% 46
  31. 95% 49
  32. 98% 53
  33. 99% 57
  34. 100% 160 (longest request)

CI

CIBandit

Current workflow

  • Build Docker Image (includes Imports: isortCode style: blackSafety)
  • Run containers
  • Benchmark with ab
  • Run tests (pass >= Coverage)
  • Push Docker Image

Credits