hackoregon / backend-examplar-2018

an example dockerized geo-aware django backend
MIT License
4 stars 5 forks source link

Is it possible to bake the SETTINGS.PY enhancements into an exemplar - or should we just doc how to add to new app? #53

Open MikeTheCanuck opened 6 years ago

MikeTheCanuck commented 6 years ago

While digging through last year's projects to understand env var usage, I got back into the settings.py file, which includes artifacts like:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/

STATIC_URL = "/budget/static/"

# This seems to be necessary to enable the Django app to correctly style
# the Swagger wrapper when the Django app runs inside a Docker container
STATIC_ROOT = 'staticfiles'

# This allows any site to call these API endpoints, which is entirely the point of making them available
CORS_ORIGIN_ALLOW_ALL = True

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

I believe these items don't come standard with a new Django app, and I'm not sure if things like the "STATIC*" variables are populated automatically when you setup whitenoise in such a project.

But we definitely have things like CORS_ORIGIN_ALLOW_ALL that we had to bake in later once we understood the CORS problem.

Or stuff like this:

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = project_config.DJANGO_SECRET_KEY

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

DEBUG=False

ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '192.168.99.100']

AWS_LOAD_BALANCER = 'hacko-integration-658279555.us-west-2.elb.amazonaws.com'
CIVIC_PDX_ORG_HOST = 'service.civicpdx.org'
CIVIC_PDX_COM_HOST = 'service.civicpdx.com'

ALLOWED_HOSTS.append(AWS_LOAD_BALANCER)
ALLOWED_HOSTS.append(CIVIC_PDX_ORG_HOST)
ALLOWED_HOSTS.append(CIVIC_PDX_COM_HOST)
# Get the IPV4 address we're working with on AWS
# The Loadbalancer uses this ip address for healthchecks
EC2_PRIVATE_IP = None
try:
    EC2_PRIVATE_IP = requests.get('http://169.254.169.254/latest/meta-data/local-ipv4', timeout=0.01).text
except requests.exceptions.RequestException:
    pass

if EC2_PRIVATE_IP:
    ALLOWED_HOSTS.append(EC2_PRIVATE_IP)

I'm 99% sure that SECRET_KEY and ALLOWED_HOSTS are part of a default Django app, but our enhancements to them definitely are not.

And I'm pretty sure some of this is non-default:

# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES = {
    'default': {
        'ENGINE': project_config.AWS['ENGINE'],
        'NAME': project_config.AWS['NAME'],
        'HOST': project_config.AWS['HOST'],
        'PORT': project_config.AWS['PORT'],
        'USER': project_config.AWS['USER'],
        'PASSWORD': project_config.AWS['PASSWORD'],
    }
}

# Application definition
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'whitenoise.runserver_nostatic',
    'django.contrib.staticfiles',
    # *******************************
    # non-default 3rd party packages
    'rest_framework',
    'rest_framework_swagger',
    'corsheaders',
    # project-specific code
    'budget_app'
    # *******************************
]
bhgrant8 commented 6 years ago

If one uses the quickstart script to create a new project, the original default settings.py is replaced by the example-settings.py located in the project bin folder.

Manual creation steps also mention to replace the default settings.py with this one the replacing the string with your actual project name. We can probably improve on this by using the env var in place of the string, and then can cut out the sed/find and replace magic.

For example the database vars: https://github.com/hackoregon/backend-examplar-2018/blob/staging/bin/example-settings.py#L101

as well as the staticfiles/whitenoise setup and CORS: https://github.com/hackoregon/backend-examplar-2018/blob/staging/bin/example-settings.py#L165

I did not put in any of the allowed hosts stuff yet, cause didn't know values for this year