Compare commits

..

2 Commits

Author SHA1 Message Date
Linden Crandall
65a098d70a updated vackend readme setup guide 2023-06-19 16:49:13 +09:00
Linden Crandall
5933057e79 created requirements.txt 2023-06-19 16:49:02 +09:00
2 changed files with 81 additions and 63 deletions

View File

@ -1,40 +1,44 @@
# Selah Django backend API # Selah Django backend API
*** DJANGO ENVIRONMENT LOCAL SETUP (installing libraries/dependencies) *** *** DJANGO ENVIRONMENT LOCAL SETUP (installing libraries/dependencies) ***
- install ffmpeg for audio file processing on your system ( https://www.gyan.dev/ffmpeg/builds/ ):
- install ffmpeg for audio file processing on your system: sudo apt-get install net-tools
sudo apt-get install net-tools sudo apt install ffmpeg
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
- install postgres - install postgres
sudo apt install libpq-dev sudo apt install libpq-dev
sudo apt install postgresql sudo apt install postgresql
sudo apt install postgresql-contrib sudo apt install postgresql-contrib
- make sure python is installed (should come with ubuntu): - make sure python is installed (should come with ubuntu):
python (or python3) --version python (or python3) --version
Django setup (Using Python -v 3.10.6 and Django -v 4.2 as of April, 29, 2023): Django setup (Using Python -v 3.10.6 and Django -v 4.2 as of April, 29, 2023):
- make dir on local machine where this project will be located - make dir on local machine where this project will be located
- clone this repo from github
- clone this repo from github (ssh or https, see top level servers_ssl_db_setup digitalocean_server.txt)
- install python venv: - install python venv:
sudo apt install python3.10-venv sudo apt install python3.10-venv
- Create python virtual environment on local machine:
- Create python virtual environment on local machine (shouldn't need to do this step if cloning from git, just install venv and try to activate the env in next step):
python3 -m venv env python3 -m venv env
- Activate python virtual environment on local machine: - Activate python virtual environment on local machine:
. env/bin/activate . env/bin/activate (linux)
(you'll see '(env)' on the left-most side the terminal signature when activated)
- Once your env is activated, install Django and other dependencies (see requirements.txt): source env/Scripts/activate (Unix/Mac)
or
env\Scripts\activate (Windows)
you'll see '(env)' on the left-most side the terminal signature when activated
- Once your env is activated, install Django and other dependencies:
`pip install -r requirements.txt`
- Description of dependencies below:
pip install django (the backend framework) pip install django (the backend framework)
pip install django-rest-framework (creating the backend API, creates djangorestframework dir as well) pip install django-rest-framework (creating the backend API, creates djangorestframework dir as well)
@ -52,37 +56,51 @@ 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 (database adapter for PostgreSQL DB. Use this for local development, but in the production server, install the binary:
pip install psycopg2-binary pip install psycopg2-binary
) )
install pgadmin4 desktop tool for DB management (https://www.pgadmin.org/download/)
pip install gunicorn
*** Note: You need to create empty DB in either psql shell or pgadmin tool 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) - install pgadmin4 desktop tool for DB management (https://www.pgadmin.org/download/)
pip install gunicorn
- activate env 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:
(linux) . env/bin.activate
- create new Django project (make sure you are in dir you want project to be created): python manage.py migrate
to create the DB schemas locally
)
*** CREATING NEW PROJECT (alreay completed, leaving for notes)***
create new Django project (make sure you are in dir you want project to be created):
django-admin startproject yourprojectname 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)
*** CREATING NEW SUPERUSER AND RUN LOCAL DEV SERVER ***
create your own admin superuser:
- 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: - start Django dev server:
- set env DEV_MODE variable:
set backend env DEV_MODE var: DEV_MODE=True
DEV_MODE=True
- start Django dev server in terminal: - start Django dev server in terminal:
DJANGO_SETTINGS_MODULE=sheriff_crandy_project.dev_settings python manage.py runserver DJANGO_SETTINGS_MODULE=main_project.dev_settings python manage.py runserver
you should be able to access backend admin portal at
- you should be able to access backend at 127.0.0.1:8000/api/admin
127.0.0.1:8000
- 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) - 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 `python manage.py startapp my_app_name`
To run vs code debugger: To run vs code debugger:
In top level directory, add this launch.json file:
- 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:
``` ```
@ -110,21 +128,10 @@ In top level directory, add this launch.json file:
In main_project folder, create .env file In main_project folder, create .env file
add these env vars: add these env vars:
DOMAIN=https://sheriffcrandymusic.local:9443/api
DEV_DOMAIN=http://127.0.0.1:8000 DEV_DOMAIN=http://127.0.0.1:8000
DEV_MODE=True DEV_MODE=True
Should be it. Activate your python env:
. env/bin/activate
And start Django's dev server with the dev settings:
start dev server using dev mode settings:
DJANGO_SETTINGS_MODULE=sheriff_crandy_project.dev_settings python manage.py runserver
Now go to an API endpoint and verify the dev site is being served:
http://127.0.0.1:8000/api/sc/v1/tracks/
If you were preveiously in production mode, stop all nginx and gunicorn servers and open 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: your browser's dev tools and clear all cache and cookie data:
@ -133,7 +140,6 @@ sudo systemctl stop nginx
sudo systemctl stop gunicorn sudo systemctl stop gunicorn
sudo systemctl stop gunicorn.socket sudo systemctl stop gunicorn.socket
*** PRODUCTION SIMULATION SERVER (https self-signed SSL certs) *** *** PRODUCTION SIMULATION SERVER (https self-signed SSL certs) ***
TO START SIMULATED PROD SERVER TO START SIMULATED PROD SERVER
@ -142,7 +148,6 @@ access site over https with self-signed ssl certs
set backend env DEV_MODE var: set backend env DEV_MODE var:
DEV_MODE=False DEV_MODE=False
restart nginx and gunicorn restart nginx and gunicorn
sudo systemctl restart nginx sudo systemctl restart nginx
sudo systemctl restart gunicorn.socket sudo systemctl restart gunicorn.socket
@ -152,7 +157,6 @@ access the backend API/admin page at:
whatever you named your hosts name in nginx/gnicorn ssl setup 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 everything is working, you can begin developing and making changes to code.
After you have made a code change, restart servers to see updates: After you have made a code change, restart servers to see updates:

14
backend/requirements.txt Normal file
View File

@ -0,0 +1,14 @@
django
djangorestframework
djangorestframework-simplejwt
pyjwt
social-auth-app-django
django-cors-headers
djoser
pillow
django-imagekit
pydub
django-environ
stripe
psycopg2
gunicorn