From 9901023c7c8eb45a4d1616d807d625dbe4a63f94 Mon Sep 17 00:00:00 2001 From: thricedotted Date: Sat, 15 Feb 2020 13:36:57 -0800 Subject: [PATCH 1/2] add more useful things to ignore --- .gitignore | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/.gitignore b/.gitignore index 8ba89fe6b..9cb68f898 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,15 @@ +# Python /venv *.pyc *.swp + +# VSCode +/.vscode + +# sqlite +.db +.db-journal + +# Fedireads +.env +images/ \ No newline at end of file From 40b63ac200602de18da0bfa974b760116b7ff874 Mon Sep 17 00:00:00 2001 From: thricedotted Date: Sat, 15 Feb 2020 13:45:44 -0800 Subject: [PATCH 2/2] move some configuration into environment vars --- .env.example | 18 ++++++++++++++++++ README.md | 2 +- fedireads/settings.py | 26 ++++++++++++++++++-------- fedireads/wsgi.py | 4 ++++ manage.py | 3 +++ rebuilddb.sh | 6 ++++++ requirements.txt | 1 + 7 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 000000000..efc937001 --- /dev/null +++ b/.env.example @@ -0,0 +1,18 @@ +# SECURITY WARNING: keep the secret key used in production secret! +export SECRET_KEY="7(2w1sedok=aznpq)ta1mc4i%4h=xx@hxwx*o57ctsuml0x%fr" + +# SECURITY WARNING: don't run with debug turned on in production! +export DEBUG=true + +export DOMAIN=your.domain.here + +## Leave unset to allow all hosts +# export ALLOWED_HOSTS="localhost,127.0.0.1,[::1]" + +export OL_URL="https://openlibrary.org" + +## Database backend to use. +## Default is postgres, use sqlite is for dev quickstart only (NOT production!!!) +export FEDIREADS_DATABASE_BACKEND=postgres + +export MEDIA_ROOT="images/" \ No newline at end of file diff --git a/README.md b/README.md index 8710b9464..9de360055 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ This creates two users, `mouse` with password `password123` and `rat` with passw And go to the app at `localhost:8000` -For most testing, you'll want to use ngrok. Remember to set the DOMAIN in settings.py to your ngrok domain. +For most testing, you'll want to use ngrok. Remember to set the DOMAIN in `.env` to your ngrok domain. ## Structure diff --git a/fedireads/settings.py b/fedireads/settings.py index 3fb98936b..e4e134049 100644 --- a/fedireads/settings.py +++ b/fedireads/settings.py @@ -1,6 +1,10 @@ ''' fedireads settings and configuration ''' import os +from environs import Env + +env = Env() + # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -8,15 +12,15 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = '7(2w1sedok=aznpq)ta1mc4i%4h=xx@hxwx*o57ctsuml0x%fr' +SECRET_KEY = env('SECRET_KEY') # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = env.bool('DEBUG', True) # TODO: annoying that I keep changing and re-commiting this -DOMAIN = '26863dc9.ngrok.io' -ALLOWED_HOSTS = ['*'] -OL_URL = 'https://openlibrary.org' +DOMAIN = env('DOMAIN') +ALLOWED_HOSTS = env.list('ALLOWED_HOSTS', ['*']) +OL_URL = env('OL_URL') # Application definition @@ -66,8 +70,10 @@ WSGI_APPLICATION = 'fedireads.wsgi.application' # Database # https://docs.djangoproject.com/en/2.0/ref/settings/#databases -DATABASES = { - 'default': { +FEDIREADS_DATABASE_BACKEND = env('FEDIREADS_DATABASE', 'postgres') + +FEDIREADS_DBS = { + 'postgres': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'fedireads', 'USER': 'fedireads', @@ -77,6 +83,10 @@ DATABASES = { } } +DATABASES = { + 'default': FEDIREADS_DBS[FEDIREADS_DATABASE_BACKEND] +} + LOGIN_URL = '/login/' AUTH_USER_MODEL = 'fedireads.User' @@ -118,4 +128,4 @@ USE_TZ = True STATIC_URL = '/static/' MEDIA_URL = '/images/' -MEDIA_ROOT = os.path.join(BASE_DIR, 'images/') +MEDIA_ROOT = os.path.join(BASE_DIR, env('MEDIA_ROOT', 'images/')) diff --git a/fedireads/wsgi.py b/fedireads/wsgi.py index 06edda621..1d48ac6a8 100644 --- a/fedireads/wsgi.py +++ b/fedireads/wsgi.py @@ -9,8 +9,12 @@ https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/ import os +from environs import Env + from django.core.wsgi import get_wsgi_application +Env.read_env() + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "fedireads.settings") application = get_wsgi_application() diff --git a/manage.py b/manage.py index f9976ef67..086138392 100755 --- a/manage.py +++ b/manage.py @@ -2,7 +2,10 @@ import os import sys +from environs import Env + if __name__ == "__main__": + Env.read_env() # load environment variables from .env os.environ.setdefault("DJANGO_SETTINGS_MODULE", "fedireads.settings") try: from django.core.management import execute_from_command_line diff --git a/rebuilddb.sh b/rebuilddb.sh index fd82950ec..536288be1 100755 --- a/rebuilddb.sh +++ b/rebuilddb.sh @@ -1,5 +1,11 @@ #!/bin/bash set -e + +if [ ! -f .env ]; then + echo "No .env found -- copying .example.env to .env!" + cp .env.example .env +fi + dropdb fedireads createdb fedireads python manage.py makemigrations fedireads diff --git a/requirements.txt b/requirements.txt index 2db75c8b9..1320435c7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ Django==3.0.3 django-model-utils==4.0.0 +environs==7.2.0 Pillow==7.0.0 psycopg2==2.8.4 pycryptodome==3.9.4