This setup has been tested with Python 3.9.
- Django + Django Rest Framework :
django
djangorestframework
- Django Channels 3 :
channels
- handling websockets backend djangorestframework-simplejwt
- JSON Web Token authenticationdjango-cors-headers
- handling cross origin requestscoverage
- for code coverage reports and running unit testsmypy
+djangorestframework-stubs
- for better typing experiencepsycopg2
- needed to use Postgres (in Docker container)channels_redis
,redis
- connection to Redis database servicedaphne
- production asgi serverwhitenoise
- building static files for productioncelery
- task queue, asynchronous tasksdrf-yasg
- OpenAPI documentationdjango-filter
- search filters integrated with chosen views
Click here to see the documentation of project's file structure.
Environmental variables in .env (random values below)
SECRET_KEY=longrandomlygeneratedsecretkey
DB_NAME=postgresdbname
DB_USER=postgresusername
DB_PASSWORD=postgrespassword
[email protected]
PG_ADMIN_PASSWORD=pgadminpassword
Create a virtual environment
py -3 -m venv venv
venv/Scripts/Activate
python -m pip install --upgrade pip
pip install -r requirements.txt
Run django application
python manage.py runserver
Preparing (if there are any changes to db schema) and running migrations
python manage.py makemigrations
python manage.py migrate
Create superuser
python manage.py createsuperuser
Run tests using Coverage
coverage run manage.py test
Get report from coverage:
coverage report -m
IMPORTANT:
- Change line endings in shell scripts from CRLF to LF
- Remove twisted-iocpsupport from requirements.txt if present
- Remember about env variables
Make sure Docker Engine is running.
While in root directory, build docker images and run them with docker-compose. This might take up to few minutes. Rebuilding image is crucial after installing new packages via pip.
docker-compose up --build
Application should be up and running: backend 127.0.0.1:8000
.
If docker images are installed and no additional packages have been installed, just run to start containers:
docker-compose up
Bringing down containers
docker-compose down
To run commands in an active container:
docker exec -it <container_id/container_name> <command>
e.g
docker exec -it backend python manage.py migrate
docker exec -it backend python manage.py shell
docker exec -it backend bash
Environmental variables:
DJANGO_SETTINGS_MODULE=core.settings.prod
SECRET_KEY
STATIC_ROOT # path to dir for storing static files
MEDIA_ROOT # path to dir for storing recordings
BACKEND_HOST # e.g example.com
BACKEND_URL # e.g https://api.example.domain.com
FRONTEND_URL # e.g https://example.domain.com
COOKIE_DOMAIN # e.g .domain.com
DB_NAME
DB_USER
DB_PASSWORD
DB_HOST
DB_PORT
REDIS_HOST
REDIS_PORT
REDIS_AUTH_PASSWORD
Build static files (STATIC_ROOT has to exist first for that to succeed)
python manage.py collectstatic --no-input
Making migrations and migrating without user input
python3 manage.py makemigrations --no-input
python3 manage.py migrate --no-input
Running backend
daphne -b 0.0.0.0 -p 8000 core.asgi:application -v2
Heroku is used as a backup to our production server and maybe will be used as a staging environment in the future.
Important: Cookie authentication will not work cross-domain and with other Heroku apps because of Heroku
Environmental variables to set in Heroku application:
DJANGO_SETTINGS_MODULE=core.settings.heroku
SECRET_KEY
STATIC_ROOT=staticfiles
MEDIA_ROOT=media
BACKEND_HOST # e.g <app_name>.herokuapp.com
BACKEND_URL # e.g https://<app_name>.herokuapp.com
FRONTEND_URL # e.g https://<app_name>.vercel.app
Used addons: heroku-postgres:hobby-dev
, heroku-redis:hobby-dev
To activate worker: heroku ps:scale worker=1:Free -a <app_name>
To run bash: heroku run bash -a <app_name>