Skip to main content

Flask extension that applies common configurationsto all of webteam's flask apps.

Project description

Canonical Webteam Flask-Base

Flask extension that applies common configurations to all of webteam's flask apps.

Usage

from canonicalwebteam.flask_base.app import FlaskBase

app = FlaskBase(__name__, "app.name")

Or:

from canonicalwebteam.flask_base.app import FlaskBase

app = FlaskBase(
    __name__,
    "app.name",
    template_404="404.html",
    template_500="500.html",
    favicon_url="/static/favicon.ico",
)

Local development

For local development, it's best to test this module with one of our website projects like ubuntu.com. For more information, follow this guide (internal only).

Features

ProxyFix

FlaskBase includes ProxyFix to avoid SSL stripping on redirects.

Redirects and deleted paths

FlaskBase uses yaml-responses to allow easy configuration of redirects and return of deleted responses, by creating redirects.yaml, permanent-redirects.yaml and deleted.yaml in the site root directory.

Error templates

FlaskBase can optionally use templates to generate the 404 and 500 error responses:

app = FlaskBase(
    __name__,
    "app.name",
    template_404="404.html",
    template_500="500.html",
)

This will lead to e.g. http://localhost/non-existent-path returning a 404 status with the contents of templates/404.html.

Redirect /favicon.ico

FlaskBase can optionally provide redirects for the commonly queried paths /favicon.ico, /robots.txt and /humans.txt to sensible locations:

from canonicalwebteam.flask_base.app import FlaskBase

app = FlaskBase(
    __name__,
    "app.name",
    template_404="404.html",
    template_500="500.html",
    favicon_url="/static/favicon.ico",
    robots_url="/static/robots.txt",
    humans_url="/static/humans.txt"
)

This will lead to e.g. http://localhost/favicon.ico returning a 302 redirect to http://localhost/static/favicon.ico.

Clear trailing slashes

Automatically clears all trailing slashes from all routes.

Jinja2 helpers

You get two jinja2 helpers to use in your templates from flask-base:

  • now is a function that outputs the current date in the passed format - {{ now('%Y') }} -> YYYY
  • versioned_static is a function that fingerprints the passed asset - {{ versioned_static('asset.js') }} -> static/asset?v=asset-hash

HTTP headers

You get the following headers automatically set:

  • X-Content-Type-Options: NOSNIFF
  • Permissions-Policy: interest-cohort=()
  • X-Frame-Options: SAMEORIGIN, which can be excluded with exclude_xframe_options_header decorator
  • Cache-Control if response.cache_control.* not set and according to static asset versioning (see versioned_static above)

security.txt, robots.txt and humans.txt

If you create a security.txt, robots.txt or humans.txt in the root of your project, these will be served at /.well-known/security.txt, /robots.txt and /humans.txt respectively.

/_status/check endpoint

Automatically adds the /_status/check endpoint which is used by content-caches for backend health checking or e.g. by k8s for checking the status of pods.

Custom gunicorn gevent worker

Included is a custom gunicorn gevent worker designed to handle SIGINT and SIGTERM gracefully, by closing all client connections and logging the stacktrace before exiting.

Usage

Run gunicorn in the usual way, but specify the worker class as LogWorker.

talisker.gunicorn.gevent webapp.app:app \
    -k canonicalwebteam.flask_base.worker.LogWorker

Tests

To run the tests execute SECRET_KEY=fake python3 -m unittest discover tests.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

canonicalwebteam_flask_base-2.5.0.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

canonicalwebteam_flask_base-2.5.0-py3-none-any.whl (19.5 kB view details)

Uploaded Python 3

File details

Details for the file canonicalwebteam_flask_base-2.5.0.tar.gz.

File metadata

File hashes

Hashes for canonicalwebteam_flask_base-2.5.0.tar.gz
Algorithm Hash digest
SHA256 dbcb2a6b408da39b37a8a75d920e5840bdaf1811564e5fc38c62af176c0d6341
MD5 d7b7b9d770fd808b35ed78d63075b57f
BLAKE2b-256 52b236aed7e1c172210f65268b19f937976fc7c0a194722f8bf97cbfec980ab2

See more details on using hashes here.

File details

Details for the file canonicalwebteam_flask_base-2.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for canonicalwebteam_flask_base-2.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0af0c820a67a1a15c54c06bb9ef3fc66b4a82003407df57fe3fe402b572fe085
MD5 6e3931b6a9aa179407ab191132b23530
BLAKE2b-256 446d2d4e49122ccb0b51a2e64aa79cd3693fc85224106a5634a5d5a35d5f2bed

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page