separate dev and production configs

This commit is contained in:
Tao Bojlen 2018-08-27 00:31:53 +02:00
parent 21af38a1ea
commit cd6d85ef16
7 changed files with 57 additions and 20 deletions

1
.gitignore vendored
View file

@ -1,5 +1,6 @@
*.csv *.csv
.idea/ .idea/
config.json
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files
__pycache__/ __pycache__/

View file

@ -1,2 +1,8 @@
# fediverse-space-backend # fediverse-space-backend
The Django backend for fediverse.space. Scrapes the fediverse and exposes an API to get data about it. 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`

View file

@ -11,6 +11,27 @@ https://docs.djangoproject.com/en/2.1/ref/settings/
""" """
import os 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, ...) # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 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 # Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/ # 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 = [] ALLOWED_HOSTS = []
@ -78,10 +93,10 @@ WSGI_APPLICATION = 'backend.wsgi.application'
DATABASES = { DATABASES = {
'default': { 'default': {
'ENGINE': 'django.db.backends.postgresql', 'ENGINE': get_config("DATABASE_ENGINE"),
'NAME': 'fediverse', 'NAME': get_config("DATABASE_NAME"),
'USER': 'tao', 'USER': get_config("DATABASE_USER"),
'PASSWORD': 'tao', 'PASSWORD': get_config("DATABASE_PASSWORD"),
} }
} }
@ -124,11 +139,3 @@ USE_TZ = True
STATIC_URL = '/static/' STATIC_URL = '/static/'
if DEBUG:
MIDDLEWARE += (
'silk.middleware.SilkyMiddleware',
)
INSTALLED_APPS += (
'silk',
)

13
backend/settings/dev.py Normal file
View file

@ -0,0 +1,13 @@
from .base import *
DEBUG = True
ALLOWED_HOSTS += ['localhost']
MIDDLEWARE += (
'silk.middleware.SilkyMiddleware',
)
INSTALLED_APPS += (
'silk',
)

View file

@ -0,0 +1,3 @@
from .base import *
ALLOWED_HOSTS += ['fediverse.space']

View file

@ -16,7 +16,7 @@ Including another URLconf
from django.urls import path, include from django.urls import path, include
from rest_framework import routers from rest_framework import routers
from apiv1 import views from apiv1 import views
from backend import settings from django.conf import settings
router = routers.DefaultRouter() router = routers.DefaultRouter()
router.register(r'instances', views.InstanceViewSet) router.register(r'instances', views.InstanceViewSet)
@ -26,4 +26,4 @@ urlpatterns = [
] ]
if settings.DEBUG: if settings.DEBUG:
urlpatterns += [path(r'silk/', include('silk.urls', namespace='silk'))] urlpatterns += [path(r'silk/', include('silk.urls', namespace='silk'))]

7
config.json.template Normal file
View file

@ -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": ""
}