production.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import os
  2. import random
  3. import string
  4. import dj_database_url
  5. import django_cache_url
  6. from .base import * # noqa: F403
  7. DEBUG = os.getenv('DJANGO_DEBUG', 'off') == 'on'
  8. # DJANGO_SECRET_KEY *should* be specified in the environment. If it's not, generate an ephemeral key.
  9. if 'DJANGO_SECRET_KEY' in os.environ:
  10. SECRET_KEY = os.environ['DJANGO_SECRET_KEY']
  11. else:
  12. # Use if/else rather than a default value to avoid calculating this if we don't need it
  13. print("WARNING: DJANGO_SECRET_KEY not found in os.environ. Generating ephemeral SECRET_KEY.")
  14. SECRET_KEY = ''.join([random.SystemRandom().choice(string.printable) for i in range(50)])
  15. # Make sure Django can detect a secure connection properly on Heroku:
  16. SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
  17. # Redirect all requests to HTTPS
  18. SECURE_SSL_REDIRECT = os.getenv('DJANGO_SECURE_SSL_REDIRECT', 'off') == 'on'
  19. # Accept all hostnames, since we don't know in advance which hostname will be used for any given Heroku instance.
  20. # IMPORTANT: Set this to a real hostname when using this in production!
  21. # See https://docs.djangoproject.com/en/1.10/ref/settings/#allowed-hosts
  22. ALLOWED_HOSTS = os.getenv('DJANGO_ALLOWED_HOSTS', '*').split(';')
  23. EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
  24. # BASE_URL required for notification emails
  25. BASE_URL = 'http://localhost:8000'
  26. db_from_env = dj_database_url.config(conn_max_age=500)
  27. DATABASES['default'].update(db_from_env)
  28. # AWS creds may be used for S3 and/or Elasticsearch
  29. AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID', '')
  30. AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY', '')
  31. AWS_REGION = os.getenv('AWS_REGION', '')
  32. # configure CACHES from CACHE_URL environment variable (defaults to locmem if no CACHE_URL is set)
  33. CACHES = {'default': django_cache_url.config()}
  34. # Configure Elasticsearch, if present in os.environ
  35. ELASTICSEARCH_ENDPOINT = os.getenv('ELASTICSEARCH_ENDPOINT', '')
  36. if ELASTICSEARCH_ENDPOINT:
  37. from elasticsearch import RequestsHttpConnection
  38. WAGTAILSEARCH_BACKENDS = {
  39. 'default': {
  40. 'BACKEND': 'wagtail.search.backends.elasticsearch2',
  41. 'HOSTS': [{
  42. 'host': ELASTICSEARCH_ENDPOINT,
  43. 'port': int(os.getenv('ELASTICSEARCH_PORT', '9200')),
  44. 'use_ssl': os.getenv('ELASTICSEARCH_USE_SSL', 'off') == 'on',
  45. 'verify_certs': os.getenv('ELASTICSEARCH_VERIFY_CERTS', 'off') == 'on',
  46. }],
  47. 'OPTIONS': {
  48. 'connection_class': RequestsHttpConnection,
  49. },
  50. }
  51. }
  52. if AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY:
  53. from aws_requests_auth.aws_auth import AWSRequestsAuth
  54. WAGTAILSEARCH_BACKENDS['default']['HOSTS'][0]['http_auth'] = AWSRequestsAuth(
  55. aws_access_key=AWS_ACCESS_KEY_ID,
  56. aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
  57. aws_token=os.getenv('AWS_SESSION_TOKEN', ''),
  58. aws_host=ELASTICSEARCH_ENDPOINT,
  59. aws_region=AWS_REGION,
  60. aws_service='es',
  61. )
  62. elif AWS_REGION:
  63. # No API keys in the environ, so attempt to discover them with Boto instead, per:
  64. # http://boto3.readthedocs.io/en/latest/guide/configuration.html#configuring-credentials
  65. # This may be useful if your credentials are obtained via EC2 instance meta data.
  66. from aws_requests_auth.boto_utils import BotoAWSRequestsAuth
  67. WAGTAILSEARCH_BACKENDS['default']['HOSTS'][0]['http_auth'] = BotoAWSRequestsAuth(
  68. aws_host=ELASTICSEARCH_ENDPOINT,
  69. aws_region=AWS_REGION,
  70. aws_service='es',
  71. )
  72. # Simplified static file serving.
  73. # https://warehouse.python.org/project/whitenoise/
  74. MIDDLEWARE.append('whitenoise.middleware.WhiteNoiseMiddleware')
  75. STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
  76. if 'AWS_STORAGE_BUCKET_NAME' in os.environ:
  77. AWS_STORAGE_BUCKET_NAME = os.getenv('AWS_STORAGE_BUCKET_NAME')
  78. AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
  79. AWS_AUTO_CREATE_BUCKET = True
  80. INSTALLED_APPS.append('storages')
  81. MEDIA_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN
  82. DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3Boto3Storage'
  83. LOGGING = {
  84. 'version': 1,
  85. 'disable_existing_loggers': False,
  86. 'handlers': {
  87. 'console': {
  88. 'class': 'logging.StreamHandler',
  89. },
  90. },
  91. 'loggers': {
  92. 'django': {
  93. 'handlers': ['console'],
  94. 'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
  95. },
  96. },
  97. }