Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore/build refactor: Add django-debug-toolbar and separate debug settings #908

Merged
merged 8 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
APP_VERSION=3.2-0.12.1
COMPOSE_PROJECT_NAME=cantus

#When DEVELOPMENT is False, Django's DEBUG setting
#is False and app is served by gunicorn
# When DEVELOPMENT is False, app is served by gunicorn,
# otherwise the Django development server is used. The
# default django settings module is cantusdata.settings.
# To run with DEBUG=True and other DEBUG settings, uncomment
# the DJANGO_SETTINGS_MODULE variable and change to
# cantusdata.debug_settings
DEVELOPMENT=False
#DJANGO_SECRET_KEY=**PROVIDE SECRET KEY HERE**
#DJANGO_SETTINGS_MODULE=cantusdata.settings

#Postgres authentication variables
POSTGRES_DB=cantus_db
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The build process relies on environment variables specified in an `.env` file lo

Make a copy of `.env.sample` and name it `.env`. You must make two modifications to this file before the docker containers will build. Both `POSTGRES_PASSWORD` and `RABBIT_PASSWORD` should be uncommented and set with secure passwords.

Before launching the site, ensure that the `DEVELOPMENT` variable is set correctly. For development, it should be set to `True`; for deployment, it should be `False`. This variable configures the application's debug settings. Deploying the website with `DEVELOPMENT=True` would leak debugging information on a live server and use Django's development server rather than gunicorn and must be avoided.
Before launching the site, ensure that the `DEVELOPMENT` and `DJANGO_SETTINGS_MODULE` variables are set correctly. For development, `DEVELOPMENT` should be set to `True`; for deployment, it should be `False`. Deploying the website with `DEVELOPMENT=True` would leak debugging information on a live server and use Django's development server rather than gunicorn and must be avoided. `DJANGO_SETTINGS_MODULE` defaults to `cantusdata.settings`, which has the `DEBUG` setting set to `False` and does not include various debugging settings. To take advantage of those debugging applications, like `django_debug_toolbar`, uncomment `DJANGO_SETTINGS_MODULE` in the `.env` file and set to `cantusdata.debug_settings`.

#### Handling `postgres` authentication issues

Expand All @@ -41,22 +41,22 @@ rm -r data

### Launch in development

In the `.env.sample` file, the `DEVELOPMENT` variable is set to `False` by default. For local development, set this to `True` to turn on Django's debug mode, which allows you to access detailed traces when Django encounters errors. For deployment on a server, this should remain set to `False`.
In the `.env.sample` file, the `DEVELOPMENT` variable is set to `False` by default. For local development, set this to `True` in your local `.env` file to install development dependencies and run the development server using Django and django-extensions. For deployment on a server, this should remain set to `False`. You should also uncomment the `DJANGO_SETTINGS_MODULE` variable and set to `cantusdata.debug_settings`.

> **Windows Users:** Make sure `/app/django-config.sh` has `LF` line endings before launching. This file gets copied over into an Ubuntu container and will break the process if git automatically checked out the file using Windows (`CRLF`) line endings.

Execute the following commands from the root directory of the repo:

```sh
# Build the images and launch the containers (this will take a while)
$ docker compose build
$ docker compose up -d
$ docker compose -f compose-dev.yaml build
$ docker compose -f compose-dev.yaml up -d
```

When testing subsequent changes, you can do this in one step by including the `--build` flag:

```
docker compose up --build -d
docker compose -f compose-dev.yaml up --build -d
```

After the build process completes, the site should be available on http://localhost:8000/ in your host machine.
Expand Down Expand Up @@ -88,7 +88,7 @@ Whenever changes are made to database models, they need to be applied to the Pos
If, during development, you make a change to any models, build and run the containers, as above. Then, enter the command line in the `app` container:

```sh
$ docker compose exec -it app bash
$ docker compose -f compose-dev.yaml exec -it app bash
```

Then, run the `makemigrations` command:
Expand Down
2 changes: 1 addition & 1 deletion app/django-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ python manage.py clear_session_data
python manage.py collectstatic --noinput

if [[ $DEVELOPMENT == "True" ]]; then
python manage.py runserver_plus 0:8001
python -Xfrozen_modules=off -m debugpy --listen 0.0.0.0:3000 manage.py runserver_plus 0:8001
else
gunicorn -b 0:8001 cantusdata.wsgi --timeout 600 --workers 4
fi
2 changes: 1 addition & 1 deletion app/production-mei-files
38 changes: 38 additions & 0 deletions app/public/cantusdata/debug_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
Overwrites some main settings that are used in development.
"""

from .settings import *

DEBUG = is_development

DEBUG_TOOLBAR_CONFIG = {
dchiller marked this conversation as resolved.
Show resolved Hide resolved
"DISABLE_PANELS": {
"debug_toolbar.panels.history.HistoryPanel",
"debug_toolbar.panels.versions.VersionsPanel",
"debug_toolbar.panels.timer.TimerPanel",
"debug_toolbar.panels.settings.SettingsPanel",
"debug_toolbar.panels.headers.HeadersPanel",
"debug_toolbar.panels.request.RequestPanel",
"debug_toolbar.panels.sql.SQLPanel",
"debug_toolbar.panels.staticfiles.StaticFilesPanel",
"debug_toolbar.panels.templates.TemplatesPanel",
"debug_toolbar.panels.cache.CachePanel",
"debug_toolbar.panels.signals.SignalsPanel",
"debug_toolbar.panels.logging.LoggingPanel",
"debug_toolbar.panels.redirects.RedirectsPanel",
"debug_toolbar.panels.profiling.ProfilingPanel",
},
"SHOW_TOOLBAR_CALLBACK": lambda request: (
False if request.headers.get("x-requested-with") == "XMLHttpRequest" else True
),
}
MIDDLEWARE.insert(0, "debug_toolbar.middleware.DebugToolbarMiddleware")
INSTALLED_APPS.extend(["django_extensions", "debug_toolbar"])


SESSION_COOKIE_SECURE = False
CSRF_COOKIE_SECURE = False

SECURE_HSTS_INCLUDE_SUBDOMAINS = False
SECURE_HSTS_PRELOAD = False
24 changes: 10 additions & 14 deletions app/public/cantusdata/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@
import os

is_development = os.environ.get("DEVELOPMENT") == "True"
is_production = not is_development

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# See https://docs.djangoproject.com/en/dev/howto/deployment/checklist/
SECRET_KEY = os.environ.get("DJANGO_SECRET_KEY")

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

ALLOWED_HOSTS = [
"cantus.simssa.ca",
Expand All @@ -33,7 +31,7 @@

# Application definition

INSTALLED_APPS = (
INSTALLED_APPS = [
# Template apps
"django.contrib.admin",
"django.contrib.auth",
Expand All @@ -46,20 +44,18 @@
"rest_framework",
"rest_framework.authtoken",
"cantusdata.CantusdataConfig",
)

if DEBUG:
INSTALLED_APPS += ("django_extensions",)
]

MIDDLEWARE = (
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware", # Migration: + django 3.1
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
)
]


ROOT_URLCONF = "cantusdata.urls"

Expand Down Expand Up @@ -165,13 +161,13 @@

DEFAULT_AUTO_FIELD = "django.db.models.AutoField"

SESSION_COOKIE_SECURE = is_production
CSRF_COOKIE_SECURE = is_production
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
CSRF_TRUSTED_ORIGINS = ["https://cantus.simssa.ca", "https://cantus.staging.simssa.ca"]

SECURE_HSTS_SECONDS = 86400
SECURE_HSTS_INCLUDE_SUBDOMAINS = is_production
SECURE_HSTS_PRELOAD = is_production
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True

CELERY_BROKER_URL = f"amqp://{os.environ.get('RABBIT_USER')}:{os.environ.get('RABBIT_PASSWORD')}@cantus-rabbitmq-1:5672/{os.environ.get('RABBIT_VHOST')}"
CELERY_RESULT_BACKEND = "django-db"
Expand Down
Loading
Loading