Selah/backend/README.md

171 lines
6.6 KiB
Markdown
Raw Normal View History

2023-06-14 10:34:24 +00:00
# Selah Django backend API
2023-06-19 07:49:13 +00:00
*** DJANGO ENVIRONMENT LOCAL SETUP (installing libraries/dependencies) ***
2023-06-14 10:34:24 +00:00
2023-06-19 07:49:13 +00:00
- install ffmpeg for audio file processing on your system ( https://www.gyan.dev/ffmpeg/builds/ ):
sudo apt-get install net-tools
sudo apt install ffmpeg
NOTE:
if using Windows, download 7-zip (https://www.7-zip.org/download.html) to extract the ffmpeg binaries; after extracting the binaries, add the path wherever you placed these files to your user's PATH environment variable
2023-06-14 10:34:24 +00:00
- install postgres
2023-06-19 07:49:13 +00:00
sudo apt install libpq-dev
sudo apt install postgresql
sudo apt install postgresql-contrib
2023-06-14 10:34:24 +00:00
- make sure python is installed (should come with ubuntu):
2023-06-19 07:49:13 +00:00
python (or python3) --version
Django setup (Using Python -v 3.10.6 and Django -v 4.2 as of April, 29, 2023):
2023-06-14 10:34:24 +00:00
- make dir on local machine where this project will be located
2023-06-19 07:49:13 +00:00
- clone this repo from github
- install python venv:
sudo apt install python3.10-venv
- Create python virtual environment on local machine:
2023-06-14 10:34:24 +00:00
2023-06-20 07:12:37 +00:00
python3 -m venv env
2023-06-14 10:34:24 +00:00
2023-06-19 07:49:13 +00:00
- Activate python virtual environment on local machine:
. env/bin/activate (linux)
2023-06-14 10:34:24 +00:00
2023-06-19 07:49:13 +00:00
source env/Scripts/activate (Unix/Mac)
2023-06-14 10:34:24 +00:00
2023-06-19 07:49:13 +00:00
or
2023-06-14 10:34:24 +00:00
2023-06-19 07:49:13 +00:00
env\Scripts\activate (Windows)
2023-06-14 10:34:24 +00:00
2023-06-19 07:49:13 +00:00
you'll see '(env)' on the left-most side the terminal signature when activated
2023-06-14 10:34:24 +00:00
2023-06-19 07:49:13 +00:00
- Once your env is activated, install Django and other dependencies:
`pip install -r requirements.txt`
- Description of dependencies below:
2023-06-14 10:34:24 +00:00
pip install django (the backend framework)
pip install django-rest-framework (creating the backend API, creates djangorestframework dir as well)
pip install djangorestframework-simplejwt (Simple JWT provides a JSON Web Token authentication backend for the Django REST Framework. Updated framework from djangorestframework-jwt which is now deprecated)
pip install pyjwt (Python library which allows you to encode and decode JSON Web Tokens JWT)
pip install social-auth-app-django (for 3rd party auth Facebook, Google, LinkedIn, etc.)
pip install django-cors-headers (provides security between backend and API)
pip install djoser (assists with user auth)
pip install pillow (image processing)
pip install django-imagekit (django image processing for track image)
pip install pydub (audio file processing)
pip install django-environ (for environment variables)
pip install stripe (payment processor for handling secure payments)
pip3 install --upgrade stripe (upgrade stripe)
pip install psycopg2 (database adapter for PostgreSQL DB. Use this for local development, but in the production server, install the binary:
pip install psycopg2-binary
)
2023-06-19 07:49:13 +00:00
- install pgadmin4 desktop tool for DB management (https://www.pgadmin.org/download/)
pip install gunicorn
2023-06-14 10:34:24 +00:00
2023-06-19 07:49:13 +00:00
You need to create and setup an empty PostgreSQL DB on your machine through either the psql shell or pgadmin tool for this project before running `python manage.py makemigrations` and `python manage.py migrate` (If you want to use custom user model, **DO NOT RUN** python manage.py makemigrations/migrate until you have created your custom user model, otherwise Django will revert to its default User Model. Changing from Django's default user model to a custom user model is possible, but unsupported and prone to many errors. Do this first before anything else if that's what you want for your app. **NOTE:** I have already done this, but leaving it here for notes. So after setting up your local DB and ensuring that the .env database settings matches your local database settings, you should only need to run:
2023-06-14 10:34:24 +00:00
2023-06-19 07:49:13 +00:00
python manage.py migrate
2023-06-14 10:34:24 +00:00
2023-06-19 07:49:13 +00:00
to create the DB schemas locally
2023-06-14 10:34:24 +00:00
2023-06-19 07:49:13 +00:00
)
2023-06-14 10:34:24 +00:00
2023-06-19 07:49:13 +00:00
*** CREATING NEW PROJECT (alreay completed, leaving for notes)***
2023-06-14 10:34:24 +00:00
2023-06-19 07:49:13 +00:00
create new Django project (make sure you are in dir you want project to be created):
django-admin startproject yourprojectname
within this directory, create a new file called `.env` and add all of the environment variables (get the values from me)
2023-06-14 10:34:24 +00:00
2023-06-19 07:49:13 +00:00
*** CREATING NEW SUPERUSER AND RUN LOCAL DEV SERVER ***
2023-06-14 10:34:24 +00:00
2023-06-19 07:49:13 +00:00
create your own admin superuser:
2023-06-14 10:34:24 +00:00
2023-06-19 07:49:13 +00:00
- activate env and run
`python manage.py createsuperuser`
(if you get a TTY error in Windows and you are using MINGW/git bash terminal, exit out and use Windows command prompt or Powershell and try it again)
- start Django dev server:
- set env DEV_MODE variable:
DEV_MODE=True
- start Django dev server in terminal:
2023-06-20 07:12:37 +00:00
- linux:
`DJANGO_SETTINGS_MODULE=main_project.dev_settings python manage.py runserver`
- windows (command prompt):
`set DJANGO_SETTINGS_MODULE=main_project.dev_settings&& python -m manage runserver`
- windows (powershell):
`$env:DJANGO_SETTINGS_MODULE = "main_project.dev_settings"; python -m manage runserver`
2023-06-14 10:34:24 +00:00
2023-06-19 07:49:13 +00:00
you should be able to access backend admin portal at
127.0.0.1:8000/api/admin
- you can now create apps (for models --users, tracks, artists, etc; make sure you are in project root directory when creating apps. After creating an app, make sure you register it by adding it to the main project's settings.py file INSTALLED_APPS dict)
`python manage.py startapp my_app_name`
2023-06-14 10:34:24 +00:00
To run vs code debugger:
2023-06-19 07:49:13 +00:00
- make sure you add your env's python interpreter executable path to the python interpreter in the command palette for VS Code
- In top level directory, add this launch.json file:
2023-06-14 10:34:24 +00:00
```
{
"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
}
]
}
```
In main_project folder, create .env file
add these env vars:
DEV_DOMAIN=http://127.0.0.1:8000
DEV_MODE=True
2023-06-19 07:49:13 +00:00
If you were preveiously in production mode, stop all nginx and gunicorn servers and open
2023-06-14 10:34:24 +00:00
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) ***
2023-06-19 07:49:13 +00:00
TO START SIMULATED PROD SERVER
access site over https with self-signed ssl certs
2023-06-14 10:34:24 +00:00
(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:
2023-06-19 07:49:13 +00:00
reset nginx and gunicorn
2023-06-14 10:34:24 +00:00
sudo systemctl restart nginx
sudo systemctl restart gunicorn