Skip to content

Commit

Permalink
refactor: add production settings
Browse files Browse the repository at this point in the history
  • Loading branch information
engineervix committed Oct 17, 2020
1 parent ba7410e commit 0267a26
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 31 deletions.
21 changes: 20 additions & 1 deletion {{cookiecutter.project_slug}}/#envs/env_prod.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@
# Command to create a new secret key:
# $ python -c 'import random; import string; print("".join([random.SystemRandom().choice(string.digits + string.ascii_letters + string.punctuation) for i in range(100)]))'
DJANGO_SECRET_KEY=CHANGEME!!!

DATABASE_URL=postgis://db_user:db_password@host:port/db_name
# DATABASE_URL=postgres://db_user:db_password@host:port/db_name
CONN_MAX_AGE=60

DEBUG=False

# EMAIL_RECIPIENTS=Full Name <email-with-name@{{cookiecutter.domain_name}}>,anotheremailwithoutname@{{cookiecutter.domain_name}}
EMAIL_RECIPIENTS=Change This <email@{{cookiecutter.domain_name}}>
# DEFAULT_FROM_EMAIL=Full Name <email-with-name@{{cookiecutter.domain_name}}>
DEFAULT_FROM_EMAIL={{cookiecutter.project_name}} <{{ cookiecutter.author_name.lower()|replace(' ', '-') }}@{{cookiecutter.domain_name}}>

ALLOWED_HOSTS={{cookiecutter.domain_name}}
BASE_URL=https://www.{{cookiecutter.domain_name}}

Expand All @@ -14,7 +23,7 @@ RECAPTCHA_PUBLIC_KEY=ADDRECAPTCHAPUBKEY!!!
RECAPTCHA_PRIVATE_KEY=ADDRECAPTCHAPRIVKEY!!!

# ipinfo.io IP Address Data API
IPINFO_ACCESS_TOKEN=ADDIPINFOTOKEN!!!
IPINFO_ACCESS_TOKEN=ADDIPINFOTOKEN_OR_REMOVE_THIS!!!

# Sendgrid
SENDGRID_API_KEY=CONFIGUREMAIL!!!
Expand All @@ -30,3 +39,13 @@ NEXMO_DEFAULT_FROM=
CLOUDFLARE_EMAIL_ADDRESS=
CLOUDFLARE_API_TOKEN=
CLOUDFLARE_DOMAIN_ZONE_ID=

# Sentry
SENTRY_DSN=CONFIGURESENTRY!!!
DJANGO_SENTRY_LOG_LEVEL=20
SENTRY_ENVIRONMENT=production
SENTRY_TRACES_SAMPLE_RATE=0.5

# Redis
REDIS_URL=CONFIGUREREDIS!!!
REDIS_KEY_PREFIX=CHANGEME!!!
25 changes: 1 addition & 24 deletions {{cookiecutter.project_slug}}/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,30 +106,7 @@
"django_user_agents.middleware.UserAgentMiddleware",
]

TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [str(APPS_DIR.path("templates"))],
# 'APP_DIRS': True, # default setting
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
"django.template.context_processors.i18n", # non-default
"django.template.context_processors.media", # non-default
"django.template.context_processors.static", # non-default
"django.template.context_processors.tz", # non-default
# "maintenance_mode.context_processors.maintenance_mode"
],
"loaders": [ # <-- this wasn't there in default config
"django.template.loaders.filesystem.Loader",
"django.template.loaders.app_directories.Loader",
],
},
}
]
# TEMPLATES settings moved to dev.py & production.py

ROOT_URLCONF = "config.urls"

Expand Down
25 changes: 25 additions & 0 deletions {{cookiecutter.project_slug}}/config/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,31 @@
# ALLOWED_HOSTS = ['*']
INTERNAL_IPS = ["127.0.0.1", "::1"]

TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [str(APPS_DIR.path("templates"))],
# 'APP_DIRS': True, # default setting
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
"django.template.context_processors.i18n", # non-default
"django.template.context_processors.media", # non-default
"django.template.context_processors.static", # non-default
"django.template.context_processors.tz", # non-default
# "maintenance_mode.context_processors.maintenance_mode"
],
"loaders": [ # <-- this wasn't there in default config
"django.template.loaders.filesystem.Loader",
"django.template.loaders.app_directories.Loader",
],
},
}
]

# LOGGING
# -----------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#logging
Expand Down
159 changes: 153 additions & 6 deletions {{cookiecutter.project_slug}}/config/settings/production.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,157 @@
"""See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/"""

from .base import *
import logging

DEBUG = False
from email.utils import getaddresses

try:
from .local import *
except ImportError:
pass
from .base import * # noqa
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.logging import LoggingIntegration

DEBUG = False # just to make sure!

DATABASES["default"]["CONN_MAX_AGE"] = env.int("CONN_MAX_AGE", default=60) # noqa F405

TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [str(APPS_DIR.path("templates"))], # noqa F405
# 'APP_DIRS': True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
"django.template.context_processors.i18n",
"django.template.context_processors.media",
"django.template.context_processors.static",
"django.template.context_processors.tz",
],
"loaders": [ # <-- this wasn't there in default config
(
"django.template.loaders.cached.Loader",
[
"django.template.loaders.filesystem.Loader",
"django.template.loaders.app_directories.Loader",
],
),
],
},
},
]

CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": env("REDIS_URL"), # noqa F405
"KEY_PREFIX": env("REDIS_KEY_PREFIX"), # noqa F405
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"SERIALIZER": "django_redis.serializers.msgpack.MSGPackSerializer",
# "COMPRESSOR": "django_redis.compressors.zlib.ZlibCompressor",
"COMPRESSOR": "django_redis.compressors.lz4.Lz4Compressor",
},
}
}
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "default"

WAGTAILFRONTENDCACHE = {
"cloudflare": {
"BACKEND": "wagtail.contrib.frontend_cache.backends.CloudflareBackend",
"EMAIL": env("CLOUDFLARE_EMAIL_ADDRESS"), # noqa F405
"TOKEN": env("CLOUDFLARE_API_TOKEN"), # noqa F405
"ZONEID": env("CLOUDFLARE_DOMAIN_ZONE_ID"), # noqa F405
},
}

USER_AGENTS_CACHE = "default"

# Obviously these three settings are needed when SSL is on
# SECURE_PROXY_SSL_HEADER is controlled by nginx so no need
# SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True

# setup email backend
EMAIL_BACKEND = "sendgrid_backend.SendgridBackend"
SENDGRID_API_KEY = env("SENDGRID_API_KEY") # noqa F405

if len(getaddresses([env("EMAIL_RECIPIENTS")])) == 1: # noqa F405
LIST_OF_EMAIL_RECIPIENTS.append( # noqa F405
getaddresses([env("EMAIL_RECIPIENTS")])[0] # noqa F405
)
else:
LIST_OF_EMAIL_RECIPIENTS += getaddresses([env("EMAIL_RECIPIENTS")]) # noqa F405

if len(getaddresses([env("DEFAULT_FROM_EMAIL")])) == 1: # noqa F405
DEFAULT_FROM_EMAIL = getaddresses([env("DEFAULT_FROM_EMAIL")])[0] # noqa F405
else:
DEFAULT_FROM_EMAIL = getaddresses([env("DEFAULT_FROM_EMAIL")]) # noqa F405

# try:
# from .local import *
# except ImportError:
# pass

# enable SSL flag, if the data exchange needs to be secure.
# Not needed for small apps
RECAPTCHA_USE_SSL = True

# LOGGING
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#logging
# See https://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.

LOGGING = {
"version": 1,
"disable_existing_loggers": True,
"formatters": {
"verbose": {
"format": "%(levelname)s %(asctime)s %(module)s "
"%(process)d %(thread)d %(message)s"
}
},
"handlers": {
"console": {
"level": "DEBUG",
"class": "logging.StreamHandler",
"formatter": "verbose",
}
},
"root": {"level": "INFO", "handlers": ["console"]},
"loggers": {
"django.db.backends": {
"level": "ERROR",
"handlers": ["console"],
"propagate": False,
},
# Errors logged by the SDK itself
"sentry_sdk": {"level": "ERROR", "handlers": ["console"], "propagate": False},
"django.security.DisallowedHost": {
"level": "ERROR",
"handlers": ["console"],
"propagate": False,
},
},
}

# Sentry
# ------------------------------------------------------------------------------
SENTRY_DSN = env("SENTRY_DSN") # noqa F405
SENTRY_LOG_LEVEL = env.int("DJANGO_SENTRY_LOG_LEVEL", logging.INFO) # noqa F405

sentry_logging = LoggingIntegration(
level=SENTRY_LOG_LEVEL, # Capture info and above as breadcrumbs
event_level=logging.ERROR, # Send errors as events
)
integrations = [sentry_logging, DjangoIntegration()]
sentry_sdk.init(
dsn=SENTRY_DSN,
integrations=integrations,
environment=env("SENTRY_ENVIRONMENT", default="production"), # noqa F405
traces_sample_rate=env.float("SENTRY_TRACES_SAMPLE_RATE", default=0.0), # noqa F405
)

0 comments on commit 0267a26

Please sign in to comment.