2023-08-17 03:57:46 +00:00
# Selah Django backend API
2023-08-17 05:00:00 +00:00
*** LOCAL DJANGO ENVIRONMENT SETUP (installing libraries/dependencies) ** *
2023-08-17 03:57:46 +00:00
2023-08-17 04:33:34 +00:00
- install ffmpeg for audio file processing on your system (ubuntu):
```
2023-08-17 03:57:46 +00:00
sudo apt-get install net-tools
sudo apt install ffmpeg
2023-08-17 04:33:34 +00:00
```
2023-08-17 03:57:46 +00:00
- install postgres
2023-08-17 04:33:34 +00:00
```
2023-08-17 03:57:46 +00:00
sudo apt install libpq-dev
sudo apt install postgresql
sudo apt install postgresql-contrib
2023-08-17 04:33:34 +00:00
```
2023-08-17 03:57:46 +00:00
- make sure python is installed (should come with ubuntu):
2023-08-17 04:33:34 +00:00
`python --version`
2023-08-17 03:57:46 +00:00
2023-08-17 04:33:34 +00:00
Django setup (Using Python -v 3.10.6 and Django -v 4.2 as of April, 29, 2023):
2023-08-17 03:57:46 +00:00
2023-08-17 04:33:34 +00:00
- create dir on local machine where this project will be stored
`mkdir selah`
2023-08-17 03:57:46 +00:00
- install python venv:
2023-08-17 04:33:34 +00:00
`sudo apt install python3.10-venv`
- Create python virtual environment on local machine:
`python3 -m venv env`
- clone this repo via https or ssh in the directory you created above; your python env folder should be next to the repo, not within it
- Activate python virtual environment on local machine. You'll see '(env)' on the left-most side the terminal signature when activated
- Ubuntu:
`. env/bin/activate `
- windows:
``
- Once your env is activated, install Django and other dependencies (see requirements.txt or just copy/paste this in the terminal):
```
pip install django
pip install django-rest-framework
pip install djangorestframework-simplejwt
pip install pyjwt
pip install social-auth-app-django
pip install django-cors-headers
pip install djoser
pip install pillow
pip install django-imagekit
pip install pydub
pip install django-environ
pip install stripe
pip3 install --upgrade stripe
pip install psycopg2
2023-08-17 03:57:46 +00:00
pip install gunicorn
2023-08-17 04:33:34 +00:00
```
(For production server, install the binary:
2023-08-17 03:57:46 +00:00
2023-08-17 04:33:34 +00:00
`pip install psycopg2-binary`
)
- install pgadmin4 desktop GUI tool for DB management (https://www.pgadmin.org/download/):
2023-08-17 03:57:46 +00:00
2023-08-17 05:00:00 +00:00
*** Note: You need to create and configure an empty DB in either psql shell or pgadmin tool and give it the name `selah_dev_db` before running `python manage.py makemigrations` and/or `python manage.py migrate`
2023-08-17 04:33:34 +00:00
2023-08-17 05:00:00 +00:00
- in the `main_project` directory, create a new `.env` file with the following environmnt variables. Get actual values from me:
2023-08-17 04:33:34 +00:00
```
DEV_MODE=
DOMAIN=
DEV_DOMAIN=
FRONTEND_DOMAIN=
FRONTEND_DEV_DOMAIN=
EMAIL_HOST=
EMAIL_PORT=
EMAIL_HOST_USER=
EMAIL_HOST_PASSWORD=
SECRET_KEY=
2023-08-17 05:00:00 +00:00
DBENGINE=django.db.backends.postgresql
DBNAME=selah_dev_db
DBUSER=< YOUR LOCAL POSTGRES USER >
DBPASSWORD=< YOUR LOCAL POSTGRES PASSWORD >
DBHOST=127.0.0.1
2023-08-17 04:33:34 +00:00
STRIPE_DOMAIN=
STRIPEPK=
STRIPESK=
STRIPE_WEBHOOK_SECRET=
2023-08-17 05:00:00 +00:00
SECURE_SSL_REDIRECT=False
SESSION_COOKIE_SECURE=False
CSRF_COOKIE_SECURE=False
2023-08-17 04:33:34 +00:00
ADMIN_URL=
```
2023-08-17 03:57:46 +00:00
2023-08-17 05:00:00 +00:00
- make sure env is activated and set backend env DEV_MODE var:
`DEV_MODE=True`
2023-08-17 03:57:46 +00:00
2023-08-17 05:00:00 +00:00
- cd to `/backned` directory where the `manage.py` file is and start Django dev server in terminal:
`DJANGO_SETTINGS_MODULE=main_project.dev_settings python manage.py runserver`
2023-08-17 03:57:46 +00:00
- you should be able to access backend at
2023-08-17 05:00:00 +00:00
`127.0.0.1:8000/api/selah`
- to create a superuser for youself so you can sign into Django's built-in admin portal to manage database models, activate env and run:
`python manage.py createsuperuser`
- follow the prompts to create your superuser and try to access the admin portal at
2023-08-17 05:05:39 +00:00
`127.0.0.1:8000/api/admin`
- finally migrate models to your empty db that should have already been created:
`python manage.py makemigrations`
and then
`python manage.py migrate`
2023-08-17 03:57:46 +00:00
2023-08-17 05:00:00 +00:00
- To run vs code debugger:
2023-08-17 03:57:46 +00:00
In top level directory, add this launch.json file:
```
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "/your/path/to/manage.py",
"args": [
"runserver"
],
"django": true,
"env": {
"DJANGO_SETTINGS_MODULE": "main_project.dev_settings"
},
"justMyCode": true
}
]
}
```
If you were preveiously in production mode, stop all nginx and gunicorn servers and open
your browser's dev tools and clear all cache and cookie data:
sudo systemctl stop nginx
sudo systemctl stop gunicorn
sudo systemctl stop gunicorn.socket
*** PRODUCTION SIMULATION SERVER (https self-signed SSL certs) ** *
TO START SIMULATED PROD SERVER
access site over https with self-signed ssl certs
(see servers_ssl_db_setup folder and complete those steps first if you haven't set up the servers locally in your machine yet):
set backend env DEV_MODE var:
DEV_MODE=False
restart nginx and gunicorn
sudo systemctl restart nginx
sudo systemctl restart gunicorn.socket
sudo systemctl restart gunicorn
access the backend API/admin page at:
whatever you named your hosts name in nginx/gnicorn ssl setup
After everything is working, you can begin developing and making changes to code.
After you have made a code change, restart servers to see updates:
reset nginx and gunicorn
sudo systemctl restart nginx
sudo systemctl restart gunicorn