diff --git a/debian/control b/debian/control index 5b90c8b73..40349ab27 100644 --- a/debian/control +++ b/debian/control @@ -1,38 +1,41 @@ Source: swh-web Maintainer: Software Heritage developers Section: python Priority: optional Build-Depends: curl, debhelper (>= 9), dh-python (>= 2), python3-all, + python3-bs4, python3-django (>= 1.10.7~), python3-djangorestframework (>= 3.4.0~), python3-django-webpack-loader, python3-django-js-reverse, python3-docutils, + python3-htmlmin, python3-magic (>= 0.3.0~), + python3-lxml, python3-nose, python3-pygments, python3-setuptools, python3-sphinx, python3-sphinxcontrib.httpdomain, python3-yaml, python3-swh.core (>= 0.0.20~), python3-swh.model (>= 0.0.15~), python3-swh.storage (>= 0.0.95~), python3-swh.indexer.storage (>= 0.0.46~), python3-swh.vault (>= 0.0.1~) Standards-Version: 3.9.6 Homepage: https://forge.softwareheritage.org/diffusion/DWUI/ Package: python3-swh.web Architecture: all Depends: python3-swh.core (>= 0.0.20~), python3-swh.model (>= 0.0.15~), python3-swh.storage (>= 0.0.95~), python3-swh.indexer.storage (>= 0.0.46~), python3-swh.vault (>= 0.0.1~), ${misc:Depends}, ${python3:Depends} Description: Software Heritage Web Applications diff --git a/requirements.txt b/requirements.txt index 603cd7004..4738d91bf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,21 +1,24 @@ # Add here external Python modules dependencies, one per line. Module names # should match https://pypi.python.org/pypi names. For the full spec or # dependency lines, see https://pip.readthedocs.org/en/1.1/requirements.html # Runtime dependencies +beautifulsoup4 django >= 1.10.7 djangorestframework >= 3.4.0 django_webpack_loader django_js_reverse docutils file_magic >= 0.3.0 +htmlmin +lxml pygments python-dateutil pyyaml #Doc dependencies sphinx sphinxcontrib-httpdomain # Test dependencies diff --git a/swh/web/common/middlewares.py b/swh/web/common/middlewares.py new file mode 100644 index 000000000..f8ede89c2 --- /dev/null +++ b/swh/web/common/middlewares.py @@ -0,0 +1,46 @@ +# Copyright (C) 2018 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU Affero General Public License version 3, or any later version +# See top-level LICENSE file for more information + +from bs4 import BeautifulSoup +from htmlmin import minify + + +class HtmlPrettifyMiddleware(object): + """ + Django middleware for prettifying generated HTML in + development mode. + """ + + def __init__(self, get_response): + self.get_response = get_response + + def __call__(self, request): + response = self.get_response(request) + if 'text/html' in response.get('Content-Type', ''): + response.content = \ + BeautifulSoup(response.content, 'lxml').prettify() + + return response + + +class HtmlMinifyMiddleware(object): + """ + Django middleware for minifying generated HTML in + production mode. + """ + + def __init__(self, get_response=None): + self.get_response = get_response + + def __call__(self, request): + response = self.get_response(request) + if 'text/html' in response.get('Content-Type', ''): + try: + minified_html = minify(response.content.decode('utf-8'), + convert_charrefs=False) + response.content = minified_html.encode('utf-8') + except Exception: + pass + return response diff --git a/swh/web/settings/development.py b/swh/web/settings/development.py index 193a6bb62..ae4156a03 100644 --- a/swh/web/settings/development.py +++ b/swh/web/settings/development.py @@ -1,21 +1,23 @@ # Copyright (C) 2017-2018 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information # flake8: noqa """ Django development settings for swh-web. """ import os # guard to avoid side effects on the django settings when building the # Debian package for swh-web if os.environ['DJANGO_SETTINGS_MODULE'] == 'swh.web.settings.development': from .common import * + MIDDLEWARE += ['swh.web.common.middlewares.HtmlPrettifyMiddleware'] + from django.core.cache import cache cache.clear() diff --git a/swh/web/settings/production.py b/swh/web/settings/production.py index 7ca670f42..09303d5b5 100644 --- a/swh/web/settings/production.py +++ b/swh/web/settings/production.py @@ -1,46 +1,46 @@ # Copyright (C) 2017-2018 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information # flake8: noqa """ Django production settings for swh-web. """ import os # guard to avoid side effects on the django settings when building the # Debian package for swh-web if os.environ['DJANGO_SETTINGS_MODULE'] == 'swh.web.settings.production': from .common import * from .common import swh_web_config from .common import REST_FRAMEWORK # activate per-site caching - MIDDLEWARE += ['django.middleware.cache.UpdateCacheMiddleware', - 'swh.web.common.middlewares.CommonMiddleware', + MIDDLEWARE.insert(0, 'django.middleware.cache.UpdateCacheMiddleware') + MIDDLEWARE += ['swh.web.common.middlewares.HtmlMinifyMiddleware', 'django.middleware.cache.FetchFromCacheMiddleware'] CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'LOCATION': swh_web_config['throttling']['cache_uri'], } } # Setup support for proxy headers USE_X_FORWARDED_HOST = True SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') # We're going through seven (or, in that case, 2) proxies thanks to Varnish REST_FRAMEWORK['NUM_PROXIES'] = 2 ALLOWED_HOSTS += [ 'archive.softwareheritage.org', 'base.softwareheritage.org', 'archive.internal.softwareheritage.org', ]