Development setup
July 7, 2022 ยท View on GitHub
Django + React + Postgres + Docker + Heroku template
This repository serves as a starting point for developing a production-ready application using Django Rest Framework, React with Typescript and Postgres in a Dockerized environment with an option to deploy to Heroku. Running development setup without docker-compose is also possible.
Tools, libraries, frameworks:
This setup has been tested with Python 3.8/3.9 and Node 14.
Backend
Python 3.8/9django,djangorestframeworkDjango + Django Rest Frameworkdjango-cors-headers- handling cross origin requestscoverage- for code coverage reports and running unit testspsycopg2- needed to use Postgres (in Docker container)gunicorn- production wsgi http serverwhitenoise- building static filesblack,isort,flake8- for code quality (optional)
Suggested packages:
drf-yasg- open api documentation (swagger and redoc)django-rest-auth,django-allauth,djoser- making auth easierdjango-filter- enables filtering querysets with url parameters and moredjango-import-export- import/export data from admin pagedjango-debug-toolbar- useful debugging tool
Frontend
Node 14react- Reacttypescript- Typescriptreact-router-dom- frontend routingaxios- for making HTTP requests
Suggested packages:
- UI libraries such as
Chakra-UI,Material-UI,Reactstrap,TailwindCSSetc. react-query- very useful package which handles data fetching logic for youformik+yup- handling form state and validation@reduxjs/toolkit+ required packages (react-redux, redux etc.) - library which makes Redux much easier to understand and usecypress- for e2e testing
Development setup
Without Docker
Backend
Create a virtual environment from cmd (or do it in Pycharm manually)
cd backend
python -m venv venv
Note: please adjust the command with the
pythonexecutable on your computer, because sometimes (example: on Ubuntu or macOS) Python 3 can only be executed usingpython3, notpython.
Activate the virtual environment that was just created On Windows:
venv\Scripts\activate
On Linux/macOS:
source venv/bin/activate
If successful, there should be (venv) in your cmd/terminal prompt.
Install the required packages with the following command.
python -m pip install --upgrade pip
pip install -r requirements.txt
Run django application from cmd (or add new Django configuration if using Pycharm)
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 createsuper user
Frontend
Install node dependencies.
cd frontend
npm install
Run development server in second terminal
npm start
Backend tests coverage
cd backend
Run tests using Coverage (unit tests + report) instead of python manage.py test
coverage run manage.py test
Get report from coverage:
coverage report -m
With Docker
First define environmental variables in .env in root directory:
DB_NAME
DB_USERNAME
DB_PASSWORD
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 or npm.
docker-compose -f docker-compose.dev.yml up --build
Application should be up and running: backend 127.0.0.1:8000, frontend 127.0.0.1:3000, and nginx 127.0.0.1:8080.
If images had been installed and no additional packages have been installed, just run to start containers:
docker-compose -f docker-compose.dev.yml up
Bringing down containers with optional -v flag removes all attached volumes and invalidates caches.
docker-compose -f docker-compose.dev.yml down
To run commands in active container:
docker exec -it <CONTAINER_ID/CONTAINER_NAME> <command>
In case there is no bash installed, try:
docker exec -it <CONTAINER_ID/CONTAINER_NAME> /bin/sh
e.g
docker exec -it backend python manage.py createsuperuser
docker exec -it backend coverage run manage.py test
docker exec -it frontend /bin/sh
CI/CD
This repository uses Github Actions to run test and deployment pipeline.
tnd.yml - runs backend-frontend test separately.
There's also have automatic deploys application to Heroku. You need the autorization token for the deployment.
heroku create {your-app-name}
Make sure you are logged in to Heroku. Generate an API token for your Heroku profile
heroku authorizations:create
Furthermore, There's success/failure notification discord action that make teammates or another developer's work easier to handles mis-communication about merging PRs, as projects become more complex, so too does the process of building and testing the code.
code-quality.yml - runs backend and frontend code quality separately.
Production Deployment
-
Download/Install/Setup Heroku CLI
- After install, log into Heroku CLI:
heroku login
- After install, log into Heroku CLI:
-
Run:
heroku create <app name>to create the Heroku application -
Set your environment variables for your production environment by running:
heroku config:set VARIABLE=valueVariables to set:
DJANGO_SETTINGS_MODULE=core.settings.prod,DJANGO_SUPERUSER_USERNAME,DJANGO_SUPERUSER_PASSWORD,PRODUCTION_HOST=<app name>.herokuapp.com,SECRET_KEY, -
Run:
heroku stack:set containerso Heroku knows this is a containerized application -
Run:
heroku addons:create heroku-postgresql:hobby-devwhich creates the postgres add-on for Heroku -
Deploy app by running:
git push heroku master/main,
or manually in Heroku dashboard
or by pushing to your github repository, having Automatic Deploys set up -
Go to
<app name>.herokuapp.comto see the published website.
Run commands in Heroku:
heroku run bash --app <your_app_name>
e.g
heroku run bash --app django-react-heroku-test
~ $ python backend/manage.py createsuperuser