Skip to content

Commit

Permalink
Separate dev and prod settings
Browse files Browse the repository at this point in the history
- Added external TOML file for secrets
- Added dev and prod settings with a common base
  • Loading branch information
AmauryCarrade committed Apr 6, 2021
1 parent d03083e commit 86caabd
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ user-generated-content
static/dist/css/*.css
static/dist/css/*.css.map

config.toml

### Django ###
*.log
Expand Down
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ django = "*"
django-ipware = "*"
python-levenshtein = "*"
requests = "*"
toml = "*"
20 changes: 14 additions & 6 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
secret_key = ""

[databases.default]
host = ""
post = ""
name = ""
user = ""
password = ""
Empty file added hawk/settings/__init__.py
Empty file.
30 changes: 3 additions & 27 deletions hawk/settings.py → hawk/settings/base.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
"""
Django settings for hawk project.
Generated by 'django-admin startproject' using Django 2.2.4.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""

import os
from pathlib import Path

from django.utils.translation import gettext_lazy as _


# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
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.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "m@6$yuc0q!z@@_!t5s#a835%agbf#uamxw11&i5-f7j8(oftnx"

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []
BASE_DIR = Path(os.path.abspath(__file__)).parent.parent.parent


# Application definition
Expand Down Expand Up @@ -82,7 +58,7 @@
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
"NAME": BASE_DIR / "db.sqlite3",
}
}

Expand Down
8 changes: 8 additions & 0 deletions hawk/settings/dev.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from .base import * # noqa

# Development settings, unsuitable for production

DEBUG = True
SECRET_KEY = "m@6$yuc0q!z@@_!t5s#a835%agbf#uamxw11&i5-f7j8(oftnx"

ALLOWED_HOSTS = []
33 changes: 33 additions & 0 deletions hawk/settings/prod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import toml

from .base import * # noqa

config_path = os.environ.get("HAWK_CONFIG", str(BASE_DIR / "config.toml"))

try:
config = toml.load(config_path)
print(f"Using the config file at {config_path!r}")
except OSError:
config = {}

DEBUG = False
ALLOWED_HOSTS = [
'hawk.carrade.eu'
]

SECRET_KEY = config["secret_key"]

DATABASES = {
"default": {
"ENGINE": "django.db.backends.mysql",
"NAME": config["databases"]["default"].get("name", "hawk"),
"USER": config["databases"]["default"].get("user", "hawk"),
"PASSWORD": config["databases"]["default"]["password"],
"HOST": config["databases"]["default"].get("host", ""),
"PORT": config["databases"]["default"].get("port", ""),
"CONN_MAX_AGE": 600,
"OPTIONS": {
"charset": "utf8mb4",
},
},
}
2 changes: 1 addition & 1 deletion manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


def main():
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hawk.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hawk.settings.dev")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
Expand Down

0 comments on commit 86caabd

Please sign in to comment.