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

Add optional django-silk #1829

Merged
merged 1 commit into from
Apr 21, 2022
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
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.env
.envrc
*.sqlite3
*.prof
node_modules
env/
__pycache__
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.env
.envrc
*.sqlite3
*.prof
node_modules
env/
__pycache__
Expand Down
34 changes: 23 additions & 11 deletions privaterelay/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import os, sys
from datetime import datetime

# This needs to be before markus, which imports pytest
IN_PYTEST = "pytest" in sys.modules

from decouple import config
import markus
import sentry_sdk
Expand All @@ -26,6 +29,14 @@

import dj_database_url

try:
# Silk is a live profiling and inspection tool for the Django framework
# https://github.com/jazzband/django-silk
import silk
HAS_SILK = True
except ImportError:
HAS_SILK = False

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

Expand All @@ -42,6 +53,7 @@
INTERNAL_IPS = config(
'DJANGO_INTERNAL_IPS', default=[]
)
USE_SILK = DEBUG and HAS_SILK and not IN_PYTEST

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
Expand Down Expand Up @@ -84,9 +96,8 @@
"'self'",
'https://www.google-analytics.com/',
)
# TODO: Add with silk
# if settings.DEBUG:
# CSP_SCRIPT_SRC += ("'unsafe-inline'",)
if USE_SILK:
CSP_SCRIPT_SRC += ("'unsafe-inline'",)

csp_style_values = ["'self'"]
# Next.js dynamically inserts the relevant styles when switching pages,
Expand Down Expand Up @@ -115,9 +126,6 @@
csp_style_values.append("'sha512-%s'" % hash)

CSP_STYLE_SRC = tuple(csp_style_values)
# TODO: Add with silk
# if settings.DEBUG:
# CSP_STYLE_SRC += ("'unsafe-inline'",)

CSP_IMG_SRC = ["'self'"] + AVATAR_IMG_SRC
REFERRER_POLICY = 'strict-origin-when-cross-origin'
Expand Down Expand Up @@ -204,8 +212,9 @@
INSTALLED_APPS += [
'debug_toolbar',
'drf_yasg',
# 'silk',
]
if USE_SILK:
INSTALLED_APPS.append("silk")

if ADMIN_ENABLED:
INSTALLED_APPS += [
Expand Down Expand Up @@ -233,11 +242,10 @@ def _get_initial_middleware():

MIDDLEWARE = _get_initial_middleware()

if USE_SILK:
MIDDLEWARE.append('silk.middleware.SilkyMiddleware')
if DEBUG:
MIDDLEWARE += [
# 'silk.middleware.SilkyMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware',
]
MIDDLEWARE.append('debug_toolbar.middleware.DebugToolbarMiddleware')

MIDDLEWARE += [
'django.middleware.security.SecurityMiddleware',
Expand Down Expand Up @@ -696,3 +704,7 @@ def _get_initial_middleware():
}
]
)

if USE_SILK:
SILKY_PYTHON_PROFILER=True
SILKY_PYTHON_PROFILER_BINARY=True
4 changes: 3 additions & 1 deletion privaterelay/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@
urlpatterns += [
path('__debug__/', include(debug_toolbar.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
# path('silk/', include('silk.urls', namespace='silk')),
]
if settings.USE_SILK:
import silk
urlpatterns.append(path('silk/', include('silk.urls', namespace='silk')))

if settings.ADMIN_ENABLED:
urlpatterns += [
Expand Down