diff --git a/.gitignore b/.gitignore index bf20302..a55734f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *.csv .idea/ +config.json # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/README.md b/README.md index 95ae0de..f8c14eb 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,8 @@ # fediverse-space-backend The Django backend for fediverse.space. Scrapes the fediverse and exposes an API to get data about it. + +## Running it +* `cp config.json.template config.json` and enter your configuration details. +* Set the environment variable `FEDIVERSE_CONFIG` to point to the path of this file. +* For development, run `python manage.py runserver --settings=backend.settings.dev` +* In production, set the environment variable `DJANGO_SETTINGS_MODULE=backend.settings.production` \ No newline at end of file diff --git a/backend/settings.py b/backend/settings/base.py similarity index 79% rename from backend/settings.py rename to backend/settings/base.py index da4f9b2..8773932 100644 --- a/backend/settings.py +++ b/backend/settings/base.py @@ -11,6 +11,27 @@ https://docs.djangoproject.com/en/2.1/ref/settings/ """ import os +import json +from django.core.exceptions import ImproperlyConfigured + +with open(os.environ.get('FEDIVERSE_CONFIG')) as f: + configs = json.loads(f.read()) + + +def get_config(setting): + try: + val = configs[setting] + if val == 'True': + val = True + elif val == 'False': + val = False + return val + except KeyError: + error_msg = "ImproperlyConfigured: Set {0} environment variable".format(setting) + raise ImproperlyConfigured(error_msg) + + +SECRET_KEY = get_config("SECRET_KEY") # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -19,12 +40,6 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/ -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'df4p)6h(#ktn*#ckh2m^^(-)w6_9mn$b%^!+$02vnb&e3sz&!r' - -# SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True - ALLOWED_HOSTS = [] @@ -78,10 +93,10 @@ WSGI_APPLICATION = 'backend.wsgi.application' DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.postgresql', - 'NAME': 'fediverse', - 'USER': 'tao', - 'PASSWORD': 'tao', + 'ENGINE': get_config("DATABASE_ENGINE"), + 'NAME': get_config("DATABASE_NAME"), + 'USER': get_config("DATABASE_USER"), + 'PASSWORD': get_config("DATABASE_PASSWORD"), } } @@ -124,11 +139,3 @@ USE_TZ = True STATIC_URL = '/static/' -if DEBUG: - MIDDLEWARE += ( - 'silk.middleware.SilkyMiddleware', - ) - - INSTALLED_APPS += ( - 'silk', - ) diff --git a/backend/settings/dev.py b/backend/settings/dev.py new file mode 100644 index 0000000..55d09be --- /dev/null +++ b/backend/settings/dev.py @@ -0,0 +1,13 @@ +from .base import * + +DEBUG = True + +ALLOWED_HOSTS += ['localhost'] + +MIDDLEWARE += ( + 'silk.middleware.SilkyMiddleware', +) + +INSTALLED_APPS += ( + 'silk', +) diff --git a/backend/settings/production.py b/backend/settings/production.py new file mode 100644 index 0000000..20db018 --- /dev/null +++ b/backend/settings/production.py @@ -0,0 +1,3 @@ +from .base import * + +ALLOWED_HOSTS += ['fediverse.space'] \ No newline at end of file diff --git a/backend/urls.py b/backend/urls.py index 78f9c3f..bd10e69 100644 --- a/backend/urls.py +++ b/backend/urls.py @@ -16,7 +16,7 @@ Including another URLconf from django.urls import path, include from rest_framework import routers from apiv1 import views -from backend import settings +from django.conf import settings router = routers.DefaultRouter() router.register(r'instances', views.InstanceViewSet) @@ -26,4 +26,4 @@ urlpatterns = [ ] if settings.DEBUG: - urlpatterns += [path(r'silk/', include('silk.urls', namespace='silk'))] \ No newline at end of file + urlpatterns += [path(r'silk/', include('silk.urls', namespace='silk'))] diff --git a/config.json.template b/config.json.template new file mode 100644 index 0000000..8ca461e --- /dev/null +++ b/config.json.template @@ -0,0 +1,7 @@ +{ + "SECRET_KEY": "df4p)6h(#ktn*#ckh2m^^(-)w6_9mn$b%^!+$02vnb&e3sz&!r", + "DATABASE_ENGINE": "django.db.backends.sqlite", + "DATABASE_NAME": "os.path.join(BASE_DIR, 'db.sqlite3')", + "DATABASE_USER": "", + "DATABASE_PASSWORD": "" +} \ No newline at end of file