Support loading configuration from an external file

This commit is contained in:
Pierre-Alain TORET 2019-05-01 12:40:48 +02:00 committed by Tom Hacohen
parent ac474d669a
commit 3d4321e50c
3 changed files with 46 additions and 17 deletions

View File

@ -26,11 +26,13 @@ source venv/bin/activate
pip install -r requirements.txt pip install -r requirements.txt
``` ```
Edit the [settings file](etesync_server/settings.py). Please refer to the If you are familiar with Django you can just edit the [settings file](etesync_server/settings.py)
[Django deployment according to the [Django deployment checklist](https://docs.djangoproject.com/en/1.11/howto/deployment/checklist)
checklist](https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/) if you are not, we also provide a simple [configuration file](etesync-server.ini)
for full instructions on how to configure a Django app for production. Some for easy deployment which you can use. You can either edit the provided file or
particular settings that should be edited are: create one in `/etc/etesync-server`.
Some particular settings that should be edited are:
* [`ALLOWED_HOSTS`](https://docs.djangoproject.com/en/1.11/ref/settings/#std:setting-ALLOWED_HOSTS) * [`ALLOWED_HOSTS`](https://docs.djangoproject.com/en/1.11/ref/settings/#std:setting-ALLOWED_HOSTS)
-- this is the list of host/domain names or addresses on which the app -- this is the list of host/domain names or addresses on which the app
will be served will be served

10
etesync-server.ini Normal file
View File

@ -0,0 +1,10 @@
[global]
secret_file = secret.txt
debug = false
[allowed_hosts]
;allowed_host1 = host1.tld
[database]
engine = django.db.backends.sqlite3
name = db.sqlite3

View File

@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/2.0/ref/settings/
""" """
import os import os
import configparser
from .utils import get_secret_from_file from .utils import get_secret_from_file
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
@ -30,6 +31,34 @@ DEBUG = False
ALLOWED_HOSTS = [] ALLOWED_HOSTS = []
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.environ.get('ETESYNC_DB_PATH',
os.path.join(BASE_DIR, 'db.sqlite3')),
}
}
# Define where to find configuration files
config_locations = ['etesync-server.ini', '/etc/etesync-server/etesync-server.ini']
# Use config file if present
if any(os.path.isfile(x) for x in config_locations):
config = configparser.ConfigParser()
config.read(config_locations)
SECRET_FILE = config['global']['secret_file']
DEBUG = config['global'].getboolean('debug')
ALLOWED_HOSTS = [y for x, y in config.items('allowed_hosts')]
DATABASES = { 'default': { x.upper(): y for x, y in config.items('database') } }
# Application definition # Application definition
@ -78,18 +107,6 @@ TEMPLATES = [
WSGI_APPLICATION = 'etesync_server.wsgi.application' WSGI_APPLICATION = 'etesync_server.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.environ.get('ETESYNC_DB_PATH',
os.path.join(BASE_DIR, 'db.sqlite3')),
}
}
# Password validation # Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators # https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators