hackoregon / devops-17

deployment tools for Hack Oregon projects
4 stars 3 forks source link

Swagger UI gone missing again from Budget container #52

Open MikeTheCanuck opened 7 years ago

MikeTheCanuck commented 7 years ago

@BrianHGrant noticed the Budget container lost the pretty "Swagger UI" once again.

I'm sure among the dozens of changes I've introduced over the last two weeks (to try to stabilize the deployment of the container), I've borked something else here.

Unfortunately I don't know of any instructions we've provided to someone to figure out how this is supposed to work, or how to fix things when this happens to a previously-pretty app/container.

Brian provided these notes, which I haven't had time to review:

"These links should explain the white page instead of the Swagger frontend when Debug=True (whats being hosted at http://hacko-integration-658279555.us-west-2.elb.amazonaws.com/budget/)

https://docs.djangoproject.com/en/1.10/howto/static-files/ https://devcenter.heroku.com/articles/django-assets http://whitenoise.evans.io/en/stable/"

Acceptance Criteria

The successful resolution of this issue will include instructions on what was changed to fix this issue and why.

Thanks for any help to get us closer to this working.

BrianHGrant commented 7 years ago

Just realized meant Debug = False in the production settings.

BrianHGrant commented 7 years ago

In order to ease development tasks, Django allows the framework itself to host 'static files' or html/css/js and show them while Debug is set to true. keeping this set to true also opens the potential for env variables and other information to be shown to end user through the verbose error message pages.

As per Django doc (https://docs.djangoproject.com/en/1.10/howto/static-files/):

screen shot 2017-04-03 at 9 26 19 pm

The traditional ways of serving these files would be through a separate static server (think apache or NGINX, further info on this option found in the Django files) from Gunicorn or hosting through s3 or some sort of CDN

A simpler option, which should be adequate for the project is to use the python package 'whitenoise'

http://whitenoise.evans.io/en/stable/

Emergency Response has implemented this through:

  1. Including the pip package 'whitenoise' in the requirements.txt

  2. Adding whitenoise to middleware in the settings.py file, just after the security middleware (as per offical docs) ie:

MIDDLEWARE_CLASSES = [

    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
  1. Adding: STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' to the end of the settings.py to take advantage of the compression and minification

  2. Altering wsgi.py

...
from whitenoise.django import DjangoWhiteNoise

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "emerresponseAPI.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
application = DjangoWhiteNoise(application)